/* 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_8048480();
// ssize_t read(int fd, void *buf, size_t nbytes);
// int printf(const char *format, ...);
// void _exit(int status);
// void free(void *ptr);
// void *malloc(size_t size);
// int puts(const char *s);
// int system(const char *command);
// void exit(int status);
// 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 setvbuf(FILE *stream, char *buf, int modes, size_t n);
// int atoi(const char *nptr);
// 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 __cdecl print_note_content(int a1);
unsigned int add_note();
unsigned int del_note();
unsigned int print_note();
int magic();
int menu();
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 *stdin; // idb
FILE *stdout; // idb
char completed_7200; // weak
int count; // weak
void *notelist; // idb
// extern _UNKNOWN __gmon_start__; weak


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

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

//----- (08048480) --------------------------------------------------------
int sub_8048480()
{
  return dword_804A008();
}
// 804A008: using guessed type int (*dword_804A008)(void);

//----- (08048560) --------------------------------------------------------
// 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();
}
// 8048563: positive sp value 4 has been found

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

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

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

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

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

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

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

//----- (0804865B) --------------------------------------------------------
int __cdecl print_note_content(int a1)
{
  return puts(*(const char **)(a1 + 4));
}

//----- (08048676) --------------------------------------------------------
unsigned int add_note()
{
  int v0; // ebx
  int i; // [esp+Ch] [ebp-1Ch]
  int size; // [esp+10h] [ebp-18h]
  char buf[8]; // [esp+14h] [ebp-14h] BYREF
  unsigned int v5; // [esp+1Ch] [ebp-Ch]

  v5 = __readgsdword(0x14u);
  if ( count <= 5 )
  {
    for ( i = 0; i <= 4; ++i )
    {
      if ( !*(&notelist + i) )
      {
        *(&notelist + i) = malloc(8u);
        if ( !*(&notelist + i) )
        {
          puts("Alloca Error");
          exit(-1);
        }
        *(_DWORD *)*(&notelist + i) = print_note_content;
        printf("Note size :");
        read(0, buf, 8u);
        size = atoi(buf);
        v0 = (int)*(&notelist + i);
        *(_DWORD *)(v0 + 4) = malloc(size);
        if ( !*((_DWORD *)*(&notelist + i) + 1) )
        {
          puts("Alloca Error");
          exit(-1);
        }
        printf("Content :");
        read(0, *((void **)*(&notelist + i) + 1), size);
        puts("Success !");
        ++count;
        return __readgsdword(0x14u) ^ v5;
      }
    }
  }
  else
  {
    puts("Full");
  }
  return __readgsdword(0x14u) ^ v5;
}
// 804A06C: using guessed type int count;

//----- (08048804) --------------------------------------------------------
unsigned int del_note()
{
  int v1; // [esp+4h] [ebp-14h]
  char buf[4]; // [esp+8h] [ebp-10h] BYREF
  unsigned int v3; // [esp+Ch] [ebp-Ch]

  v3 = __readgsdword(0x14u);
  printf("Index :");
  read(0, buf, 4u);
  v1 = atoi(buf);
  if ( v1 < 0 || v1 >= count )
  {
    puts("Out of bound!");
    _exit(0);
  }
  if ( *(&notelist + v1) )
  {
    free(*((void **)*(&notelist + v1) + 1));
    free(*(&notelist + v1));
    puts("Success");
  }
  return __readgsdword(0x14u) ^ v3;
}
// 804A06C: using guessed type int count;
// 8048804: using guessed type char buf[4];

//----- (080488D5) --------------------------------------------------------
unsigned int print_note()
{
  int v1; // [esp+4h] [ebp-14h]
  char buf[4]; // [esp+8h] [ebp-10h] BYREF
  unsigned int v3; // [esp+Ch] [ebp-Ch]

  v3 = __readgsdword(0x14u);
  printf("Index :");
  read(0, buf, 4u);
  v1 = atoi(buf);
  if ( v1 < 0 || v1 >= count )
  {
    puts("Out of bound!");
    _exit(0);
  }
  if ( *(&notelist + v1) )
    (*(void (__cdecl **)(_DWORD))*(&notelist + v1))(*(&notelist + v1));
  return __readgsdword(0x14u) ^ v3;
}
// 804A06C: using guessed type int count;
// 80488D5: using guessed type char buf[4];

//----- (08048986) --------------------------------------------------------
int magic()
{
  return system("cat flag");
}

//----- (0804899F) --------------------------------------------------------
int menu()
{
  puts("----------------------");
  puts("       HackNote       ");
  puts("----------------------");
  puts(" 1. Add note          ");
  puts(" 2. Delete note       ");
  puts(" 3. Print note        ");
  puts(" 4. Exit              ");
  puts("----------------------");
  return printf("Your choice :");
}

//----- (08048A38) --------------------------------------------------------
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
  int v3; // eax
  char buf[4]; // [esp+8h] [ebp-10h] BYREF
  unsigned int v5; // [esp+Ch] [ebp-Ch]

  v5 = __readgsdword(0x14u);
  setvbuf(stdout, 0, 2, 0);
  setvbuf(stdin, 0, 2, 0);
  while ( 1 )
  {
    while ( 1 )
    {
      menu();
      read(0, buf, 4u);
      v3 = atoi(buf);
      if ( v3 != 2 )
        break;
      del_note();
    }
    if ( v3 > 2 )
    {
      if ( v3 == 3 )
      {
        print_note();
      }
      else
      {
        if ( v3 == 4 )
          exit(0);
LABEL_13:
        puts("Invalid choice");
      }
    }
    else
    {
      if ( v3 != 1 )
        goto LABEL_13;
      add_note();
    }
  }
}
// 8048676: using guessed type int add_note(void);
// 8048804: using guessed type int del_note(void);
// 80488D5: using guessed type int print_note(void);
// 804899F: using guessed type int menu(void);
// 8048A38: using guessed type char buf[4];

//----- (08048B00) --------------------------------------------------------
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_;

//----- (08048B64) --------------------------------------------------------
void term_proc()
{
  ;
}

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