/* 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();
__int64 sub_400600(); // weak
// int puts(const char *s);
// int fclose(FILE *stream);
// int printf(const char *format, ...);
// int __fastcall __libc_start_main(int (__fastcall *main)(int, char **, char **), int argc, char **ubp_av, void (*init)(void), void (*fini)(void), void (*rtld_fini)(void), void *stack_end);
// int fflush(FILE *stream);
// int _IO_getc(_IO_FILE *fp);
// FILE *fopen(const char *filename, const char *modes);
// __int64 __isoc99_scanf(const char *, ...); weak
// __int64 _gmon_start__(void); weak
void __fastcall __noreturn start(__int64 a1, __int64 a2, void (*a3)(void));
__int64 deregister_tm_clones(void); // weak
__int64 register_tm_clones(void); // weak
__int64 _do_global_dtors_aux(); // weak
__int64 __fastcall frame_dummy(_QWORD, _QWORD, _QWORD); // weak
int __fastcall main(int argc, const char **argv, const char **envp);
void _libc_csu_fini(void); // idb
void term_proc();

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

_UNKNOWN _libc_csu_init;
__int64 (__fastcall *_frame_dummy_init_array_entry[2])() = { &frame_dummy, &_do_global_dtors_aux }; // weak
__int64 (__fastcall *_do_global_dtors_aux_fini_array_entry)() = &_do_global_dtors_aux; // weak
__int64 (*qword_601010)(void) = NULL; // weak
FILE *_bss_start; // idb
char completed_7585; // weak
// extern _UNKNOWN __gmon_start__; weak


//----- (00000000004005E0) ----------------------------------------------------
void *init_proc()
{
  void *result; // rax

  result = &__gmon_start__;
  if ( &__gmon_start__ )
    return (void *)_gmon_start__();
  return result;
}
// 4006A0: using guessed type __int64 _gmon_start__(void);

//----- (0000000000400600) ----------------------------------------------------
__int64 sub_400600()
{
  return qword_601010();
}
// 400600: using guessed type __int64 sub_400600();
// 601010: using guessed type __int64 (*qword_601010)(void);

//----- (00000000004006B0) ----------------------------------------------------
// positive sp value has been detected, the output may be wrong!
void __fastcall __noreturn start(__int64 a1, __int64 a2, void (*a3)(void))
{
  __int64 v3; // rax
  int v4; // esi
  __int64 v5; // [rsp-8h] [rbp-8h] BYREF
  char *retaddr; // [rsp+0h] [rbp+0h] BYREF

  v4 = v5;
  v5 = v3;
  __libc_start_main(
    (int (__fastcall *)(int, char **, char **))main,
    v4,
    &retaddr,
    (void (*)(void))_libc_csu_init,
    _libc_csu_fini,
    a3,
    &v5);
  __halt();
}
// 4006B6: positive sp value 8 has been found
// 4006BD: variable 'v3' is possibly undefined

//----- (00000000004006E0) ----------------------------------------------------
__int64 deregister_tm_clones()
{
  __int64 result; // rax

  result = 6295671LL - (_QWORD)&_bss_start;
  if ( (unsigned __int64)(6295671LL - (_QWORD)&_bss_start) > 0xE )
    return 0LL;
  return result;
}
// 4006E0: using guessed type __int64 deregister_tm_clones();

//----- (0000000000400720) ----------------------------------------------------
__int64 register_tm_clones()
{
  return 0LL;
}
// 400720: using guessed type __int64 register_tm_clones();

//----- (0000000000400760) ----------------------------------------------------
__int64 _do_global_dtors_aux()
{
  __int64 result; // rax

  if ( !completed_7585 )
  {
    result = deregister_tm_clones();
    completed_7585 = 1;
  }
  return result;
}
// 4006E0: using guessed type __int64 deregister_tm_clones(void);
// 400760: using guessed type __int64 _do_global_dtors_aux();
// 601078: using guessed type char completed_7585;

//----- (0000000000400780) ----------------------------------------------------
__int64 frame_dummy()
{
  return register_tm_clones();
}
// 400780: could not find valid save-restore pair for rbp
// 400720: using guessed type __int64 register_tm_clones(void);
// 400780: using guessed type __int64 frame_dummy();

//----- (00000000004007A6) ----------------------------------------------------
int __fastcall main(int argc, const char **argv, const char **envp)
{
  char v4; // [rsp+3h] [rbp-3Dh]
  int i; // [rsp+4h] [rbp-3Ch]
  int j; // [rsp+4h] [rbp-3Ch]
  char *format; // [rsp+8h] [rbp-38h] BYREF
  _IO_FILE *fp; // [rsp+10h] [rbp-30h]
  char *v9; // [rsp+18h] [rbp-28h]
  char v10[24]; // [rsp+20h] [rbp-20h] BYREF
  unsigned __int64 v11; // [rsp+38h] [rbp-8h]

  v11 = __readfsqword(0x28u);
  fp = fopen("flag.txt", "r");
  for ( i = 0; i <= 21; ++i )
    v10[i] = _IO_getc(fp);
  fclose(fp);
  v9 = v10;
  puts("what's the flag");
  fflush(_bss_start);
  format = 0LL;
  __isoc99_scanf("%ms", &format);
  for ( j = 0; j <= 21; ++j )
  {
    v4 = format[j];
    if ( !v4 || v10[j] != v4 )
    {
      puts("You answered:");
      printf(format);
      puts("\nBut that was totally wrong lol get rekt");
      fflush(_bss_start);
      return 0;
    }
  }
  printf("That's right, the flag is %s\n", v9);
  fflush(_bss_start);
  return 0;
}
// 400690: using guessed type __int64 __isoc99_scanf(const char *, ...);
// 4007A6: using guessed type char var_20[24];

//----- (0000000000400900) ----------------------------------------------------
void __fastcall _libc_csu_init(unsigned int a1, __int64 a2, __int64 a3)
{
  signed __int64 v4; // rbp
  __int64 i; // rbx

  v4 = &_do_global_dtors_aux_fini_array_entry - _frame_dummy_init_array_entry;
  init_proc();
  if ( v4 )
  {
    for ( i = 0LL; i != v4; ++i )
      ((void (__fastcall *)(_QWORD, __int64, __int64))_frame_dummy_init_array_entry[i])(a1, a2, a3);
  }
}
// 400780: using guessed type __int64 __fastcall frame_dummy(_QWORD, _QWORD, _QWORD);
// 600E10: using guessed type __int64 (__fastcall *_frame_dummy_init_array_entry[2])();
// 600E18: using guessed type __int64 (__fastcall *_do_global_dtors_aux_fini_array_entry)();

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

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