/* 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_8049020();
// 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);
// int mprotect(void *addr, size_t len, int prot);
// char *gets(char *s);
// void perror(const char *s);
// char *strcpy(char *dest, const char *src);
// int puts(const char *s);
// void exit(int status);
// size_t strlen(const char *s);
// int getpagesize(void);
// void __usercall __noreturn start(int a1@<eax>, void (*a2)(void)@<edx>);
void sub_80490E9();
void _x86_get_pc_thunk_bx();
void *deregister_tm_clones();
int register_tm_clones();
void *_do_global_dtors_aux();
int frame_dummy();
int __cdecl check(unsigned __int8 a1, char *src);
int validate_passwd();
int __cdecl main(int argc, const char **argv, const char **envp);
void term_proc();

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

int (*dword_804BFFC)(void) = NULL; // weak
_UNKNOWN _bss_start; // weak
char completed_0; // weak
char passwd_buf[500]; // weak
// extern _UNKNOWN _gmon_start__; weak


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

  result = &_gmon_start__;
  if ( &_gmon_start__ )
    return (void *)((int (*)(void))_gmon_start__)();
  return result;
}

//----- (08049020) --------------------------------------------------------
int sub_8049020()
{
  return dword_804BFFC();
}
// 804BFFC: using guessed type int (*dword_804BFFC)(void);

//----- (080490C0) --------------------------------------------------------
// 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, 0, 0, a2, &v3);
  __halt();
}
// 80490C3: positive sp value 4 has been found

//----- (080490E9) --------------------------------------------------------
void sub_80490E9()
{
  ;
}

//----- (08049100) --------------------------------------------------------
void _x86_get_pc_thunk_bx()
{
  ;
}

//----- (08049110) --------------------------------------------------------
void *deregister_tm_clones()
{
  return &_bss_start;
}
// 8049110: could not find valid save-restore pair for ebp

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

//----- (08049190) --------------------------------------------------------
void *_do_global_dtors_aux()
{
  void *result; // eax

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

//----- (080491C0) --------------------------------------------------------
int frame_dummy()
{
  return register_tm_clones();
}

//----- (080491C6) --------------------------------------------------------
int __cdecl check(unsigned __int8 a1, char *src)
{
  int v2; // eax
  int result; // eax
  void *addr; // [esp+1Ch] [ebp-Ch]

  if ( a1 <= 3u || a1 > 8u )
    return puts("bad!");
  puts("good!");
  strcpy(passwd_buf, src);
  addr = (void *)(-getpagesize() & (unsigned int)passwd_buf);
  v2 = getpagesize();
  result = mprotect(addr, v2, 7);
  if ( result == -1 )
  {
    perror("mprotect");
    exit(1);
  }
  return result;
}

//----- (08049277) --------------------------------------------------------
int validate_passwd()
{
  char s[50]; // [esp+Dh] [ebp-3Bh] BYREF
  unsigned __int8 v2; // [esp+3Fh] [ebp-9h]

  gets(s);
  v2 = strlen(s);
  return check(v2, s);
}

//----- (080492C4) --------------------------------------------------------
int __cdecl main(int argc, const char **argv, const char **envp)
{
  puts("##### Welcome to LLM4CTF #####");
  puts("Plz input your passwd:");
  validate_passwd(&argc);
  return 0;
}
// 8049277: using guessed type int __cdecl validate_passwd(_DWORD);

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

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