/* This file was generated by the Hex-Rays decompiler version 8.3.0.230608.
   Copyright (c) 2007-2021 Hex-Rays <info@hex-rays.com>

   Detected compiler: GNU C++
*/

#include <defs.h>


//-------------------------------------------------------------------------
// Function declarations

void *init_proc();
int sub_80484B0();
// void setbuf(FILE *stream, char *buf);
// ssize_t read(int fd, void *buf, size_t nbytes);
// int printf(const char *format, ...);
// int fflush(FILE *stream);
// char *strcpy(char *dest, const char *src);
// int puts(const char *s);
// int system(const char *command);
// void exit(int status);
// size_t strlen(const char *s);
// int __cdecl __libc_start_main(int (__cdecl *main)(int, char **, char **), int argc, char **ubp_av, void (*init)(void), void (*fini)(void), void (*rtld_fini)(void), void *stack_end);
// void *memset(void *s, int c, size_t n);
// int __isoc99_scanf(const char *, ...); weak
// int _gmon_start__(void); weak
// void __usercall __noreturn start(int a1@<eax>, void (*a2)(void)@<edx>);
void _x86_get_pc_thunk_bx();
int deregister_tm_clones();
int register_tm_clones();
int _do_global_dtors_aux();
int frame_dummy();
int what_is_this();
char *__cdecl check_passwd(char *s);
char *login();
int __cdecl main(int argc, const char **argv, const char **envp);
void _libc_csu_init(void); // idb
void _libc_csu_fini(void); // idb
void term_proc();

//-------------------------------------------------------------------------
// Data declarations

int (*_frame_dummy_init_array_entry[2])() = { &frame_dummy, &_do_global_dtors_aux }; // weak
int (*_do_global_dtors_aux_fini_array_entry)() = &_do_global_dtors_aux; // weak
Elf32_Dyn *GLOBAL_OFFSET_TABLE_ = &DYNAMIC; // weak
int (*dword_804A008)(void) = NULL; // weak
_UNKNOWN _bss_start; // weak
_UNKNOWN unk_804A047; // weak
FILE *stderr; // idb
FILE *stdin; // idb
FILE *stdout; // idb
char completed_7209; // weak
// extern _UNKNOWN __gmon_start__; weak


//----- (08048484) --------------------------------------------------------
void *init_proc()
{
  void *result; // eax

  result = &__gmon_start__;
  if ( &__gmon_start__ )
    return (void *)_gmon_start__();
  return result;
}
// 8048580: using guessed type int _gmon_start__(void);

//----- (080484B0) --------------------------------------------------------
int sub_80484B0()
{
  return dword_804A008();
}
// 804A008: using guessed type int (*dword_804A008)(void);

//----- (08048590) --------------------------------------------------------
// positive sp value has been detected, the output may be wrong!
void __usercall __noreturn start(int a1@<eax>, void (*a2)(void)@<edx>)
{
  int v2; // esi
  int v3; // [esp-4h] [ebp-4h] BYREF
  char *retaddr; // [esp+0h] [ebp+0h] BYREF

  v2 = v3;
  v3 = a1;
  __libc_start_main((int (__cdecl *)(int, char **, char **))main, v2, &retaddr, _libc_csu_init, _libc_csu_fini, a2, &v3);
  __halt();
}
// 8048593: positive sp value 4 has been found

//----- (080485C0) --------------------------------------------------------
void _x86_get_pc_thunk_bx()
{
  ;
}

//----- (080485D0) --------------------------------------------------------
int deregister_tm_clones()
{
  int result; // eax

  result = &unk_804A047 - &_bss_start;
  if ( (unsigned int)(&unk_804A047 - &_bss_start) > 6 )
    return 0;
  return result;
}
// 80485D0: could not find valid save-restore pair for ebp

//----- (08048600) --------------------------------------------------------
int register_tm_clones()
{
  return 0;
}
// 8048600: could not find valid save-restore pair for ebp

//----- (08048640) --------------------------------------------------------
int _do_global_dtors_aux()
{
  int result; // eax

  if ( !completed_7209 )
  {
    result = deregister_tm_clones();
    completed_7209 = 1;
  }
  return result;
}
// 8048640: could not find valid save-restore pair for ebp
// 804A088: using guessed type char completed_7209;

//----- (08048660) --------------------------------------------------------
int frame_dummy()
{
  return register_tm_clones();
}
// 8048660: could not find valid save-restore pair for ebp

//----- (0804868B) --------------------------------------------------------
int what_is_this()
{
  return system("cat flag");
}

//----- (080486A4) --------------------------------------------------------
char *__cdecl check_passwd(char *s)
{
  char dest[11]; // [esp+4h] [ebp-14h] BYREF
  unsigned __int8 v3; // [esp+Fh] [ebp-9h]

  v3 = strlen(s);
  if ( v3 <= 3u || v3 > 8u )
  {
    puts("Invalid Password");
    return (char *)fflush(stdout);
  }
  else
  {
    puts("Success");
    fflush(stdout);
    return strcpy(dest, s);
  }
}
// 80486A4: using guessed type char dest[11];

//----- (08048720) --------------------------------------------------------
char *login()
{
  char buf[512]; // [esp+0h] [ebp-228h] BYREF
  char s[40]; // [esp+200h] [ebp-28h] BYREF

  memset(s, 0, 0x20u);
  memset(buf, 0, sizeof(buf));
  puts("Please input your username:");
  read(0, s, 0x19u);
  printf("Hello %s\n", s);
  puts("Please input your passwd:");
  read(0, buf, 0x199u);
  return check_passwd(buf);
}

//----- (080487CA) --------------------------------------------------------
int __cdecl main(int argc, const char **argv, const char **envp)
{
  int v4; // [esp+Ch] [ebp-Ch] BYREF

  setbuf(stdin, 0);
  setbuf(stdout, 0);
  setbuf(stderr, 0);
  puts("---------------------");
  puts("~~ Welcome to CTF! ~~");
  puts("       1.Login       ");
  puts("       2.Exit        ");
  puts("---------------------");
  printf("Your choice:");
  __isoc99_scanf("%d", &v4);
  if ( v4 == 1 )
  {
    login();
  }
  else
  {
    if ( v4 == 2 )
    {
      puts("Bye~");
      exit(0);
    }
    puts("Invalid Choice!");
  }
  return 0;
}
// 8048570: using guessed type int __isoc99_scanf(const char *, ...);
// 8048720: using guessed type int login(void);

//----- (080488E0) --------------------------------------------------------
void _libc_csu_init(void)
{
  int v0; // esi
  int i; // edi

  init_proc();
  v0 = ((char *)&_do_global_dtors_aux_fini_array_entry
      - ((char *)&_frame_dummy_init_array_entry[-33630208]
       + (_DWORD)&GLOBAL_OFFSET_TABLE_)) >> 2;
  if ( v0 )
  {
    for ( i = 0; i != v0; ++i )
      _frame_dummy_init_array_entry[i]();
  }
}
// 8049F08: using guessed type int (*_frame_dummy_init_array_entry[2])();
// 8049F0C: using guessed type int (*_do_global_dtors_aux_fini_array_entry)();
// 804A000: using guessed type Elf32_Dyn *GLOBAL_OFFSET_TABLE_;

//----- (08048944) --------------------------------------------------------
void term_proc()
{
  ;
}

// nfuncs=41 queued=14 decompiled=14 lumina nreq=0 worse=0 better=0
// ALL OK, 14 function(s) have been successfully decompiled
