/* 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 putchar(int c);
// char *strcpy(char *dest, const char *src);
// int puts(const char *s);
// ssize_t write(int fd, const void *buf, size_t n);
// void setbuf(FILE *stream, char *buf);
// int system(const char *command);
// int printf(const char *format, ...);
// void *memset(void *s, int c, size_t n);
// ssize_t read(int fd, void *buf, size_t nbytes);
// 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);
// __int64 _gmon_start__(void); weak
// void *memcpy(void *dest, const void *src, size_t n);
// int fflush(FILE *stream);
// __int64 atol(const char *nptr);
void __fastcall __noreturn start(__int64 a1, __int64 a2, void (*a3)(void));
__int64 sub_4007E0(void); // weak
__int64 sub_400820(void); // weak
__int64 sub_400860(); // weak
__int64 sub_400880(); // weak
int sub_4008A6();
int sub_4008BB();
_QWORD *__fastcall sub_400903(_QWORD *a1, int a2, int a3, int a4, int a5, int a6, __int64 buf, __int64 a8, __int64 a9, __int64 a10, __int64 a11);
__int64 sub_400A75(void); // weak
char *__fastcall sub_400AE5(const char *a1);
int __fastcall sub_400B07(int a1, int a2, int a3, int a4, int a5, int a6, char format, int a8, __int64 a9);
__int64 __fastcall sub_400B41(__int64 a1, int a2, int a3, int a4, int a5, int a6, __int64 s, __int64 a8, __int128 dest, __int64 a10);
int sub_400D1A();
int __fastcall sub_400D2B(int a1, int a2, int a3, int a4, int a5, int a6, __int64 format, __int64 a8, __int128 dest, __int64 a10);
__int64 __fastcall main(int a1, char **a2, char **a3);
void __fastcall init(unsigned int a1, __int64 a2, __int64 a3);
void fini(void); // idb
void term_proc();

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

__int64 (__fastcall *off_601DA0[2])() = { &sub_400880, &sub_400860 }; // weak
__int64 (__fastcall *off_601DA8)() = &sub_400860; // weak
FILE *stdout; // idb
FILE *stdin; // idb
char byte_602028; // weak
// extern _UNKNOWN __gmon_start__; weak


//----- (0000000000400708) ----------------------------------------------------
void *init_proc()
{
  void *result; // rax

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

//----- (00000000004007B0) ----------------------------------------------------
// 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(main, v4, &retaddr, init, fini, a3, &v5);
  __halt();
}
// 4007B6: positive sp value 8 has been found
// 4007BD: variable 'v3' is possibly undefined

//----- (00000000004007E0) ----------------------------------------------------
__int64 sub_4007E0()
{
  __int64 result; // rax

  result = 6299671LL - (_QWORD)&stdout;
  if ( (unsigned __int64)(6299671LL - (_QWORD)&stdout) > 0xE )
    return 0LL;
  return result;
}
// 4007E0: using guessed type __int64 sub_4007E0();

//----- (0000000000400820) ----------------------------------------------------
__int64 sub_400820()
{
  return 0LL;
}
// 400820: using guessed type __int64 sub_400820();

//----- (0000000000400860) ----------------------------------------------------
__int64 sub_400860()
{
  __int64 result; // rax

  if ( !byte_602028 )
  {
    result = sub_4007E0();
    byte_602028 = 1;
  }
  return result;
}
// 4007E0: using guessed type __int64 sub_4007E0(void);
// 400860: using guessed type __int64 sub_400860();
// 602028: using guessed type char byte_602028;

//----- (0000000000400880) ----------------------------------------------------
__int64 sub_400880()
{
  return sub_400820();
}
// 400880: could not find valid save-restore pair for rbp
// 400820: using guessed type __int64 sub_400820(void);
// 400880: using guessed type __int64 sub_400880();

//----- (00000000004008A6) ----------------------------------------------------
// positive sp value has been detected, the output may be wrong!
int sub_4008A6()
{
  return system("/bin/sh");
}
// 4008B7: positive sp value 10 has been found

//----- (00000000004008BB) ----------------------------------------------------
int sub_4008BB()
{
  puts("**********************************************");
  puts("*                                            *");
  puts("*Welcome to sangebaimao,Pwnn me and have fun!*");
  puts("*                                            *");
  puts("**********************************************");
  return fflush(stdout);
}

//----- (0000000000400903) ----------------------------------------------------
_QWORD *__fastcall sub_400903(
        _QWORD *a1,
        int a2,
        int a3,
        int a4,
        int a5,
        int a6,
        __int64 buf,
        __int64 a8,
        __int64 a9,
        __int64 a10,
        __int64 a11)
{
  unsigned __int8 v12; // [rsp+1Fh] [rbp-1h]

  puts("Register Account first!");
  puts("Input your username(max lenth:20): ");
  fflush(stdout);
  v12 = read(0, &buf, 0x14uLL);
  if ( v12 && v12 <= 0x14u )
  {
    puts("Input your password(max lenth:20): ");
    fflush(stdout);
    read(0, (char *)&a9 + 4, 0x14uLL);
    fflush(stdout);
    *a1 = buf;
    a1[1] = a8;
    a1[2] = a9;
    a1[3] = a10;
    a1[4] = a11;
  }
  else
  {
    LOBYTE(buf) = 48;
    puts("error lenth(username)!try again");
    fflush(stdout);
    *a1 = buf;
    a1[1] = a8;
    a1[2] = a9;
    a1[3] = a10;
    a1[4] = a11;
  }
  return a1;
}

//----- (0000000000400A75) ----------------------------------------------------
__int64 sub_400A75()
{
  int buf; // [rsp+8h] [rbp-8h] BYREF

  buf = 0;
  puts("1.Sh0w Account Infomation!");
  puts("2.Ed1t Account Inf0mation!");
  puts("3.QUit sangebaimao:(");
  putchar(62);
  fflush(stdout);
  read(0, &buf, 5uLL);
  return (unsigned int)atol((const char *)&buf);
}
// 400A75: using guessed type __int64 sub_400A75();

//----- (0000000000400AE5) ----------------------------------------------------
char *__fastcall sub_400AE5(const char *a1)
{
  char dest[32]; // [rsp+10h] [rbp-20h] BYREF

  return strcpy(dest, a1);
}
// 400AE5: using guessed type char dest[32];

//----- (0000000000400B07) ----------------------------------------------------
int __fastcall sub_400B07(int a1, int a2, int a3, int a4, int a5, int a6, char format, int a8, __int64 a9)
{
  write(0, "Welc0me to sangebaimao!\n", 0x1AuLL);
  printf(&format);
  return printf((const char *)&a9 + 4);
}

//----- (0000000000400B41) ----------------------------------------------------
__int64 __fastcall sub_400B41(
        __int64 a1,
        int a2,
        int a3,
        int a4,
        int a5,
        int a6,
        __int64 s,
        __int64 a8,
        __int128 dest,
        __int64 a10)
{
  char buf[304]; // [rsp+10h] [rbp-260h] BYREF
  char src[302]; // [rsp+140h] [rbp-130h] BYREF
  unsigned __int8 v13; // [rsp+26Eh] [rbp-2h]
  char v14; // [rsp+26Fh] [rbp-1h]

  puts("please input new username(max lenth:20): ");
  fflush(stdout);
  v14 = read(0, buf, 0x12CuLL);
  if ( v14 <= 0 || v14 > 20 )
  {
    puts("len error(max lenth:20)!try again..");
    fflush(stdout);
    *(_QWORD *)a1 = s;
    *(_QWORD *)(a1 + 8) = a8;
    *(_OWORD *)(a1 + 16) = dest;
    *(_QWORD *)(a1 + 32) = a10;
  }
  else
  {
    memset(&s, 0, 0x14uLL);
    strcpy((char *)&s, buf);
    puts("please input new password(max lenth:20): ");
    fflush(stdout);
    v13 = read(0, src, 0x12CuLL);
    if ( v13 && v13 <= 0x14u )
    {
      memset((char *)&dest + 4, 0, 0x14uLL);
      sub_400AE5(src);
      memcpy((char *)&dest + 4, src, v13);
      fflush(stdout);
      *(_QWORD *)a1 = s;
      *(_QWORD *)(a1 + 8) = a8;
      *(_OWORD *)(a1 + 16) = dest;
      *(_QWORD *)(a1 + 32) = a10;
    }
    else
    {
      puts("len error(max lenth:10)!try again..");
      fflush(stdout);
      *(_QWORD *)a1 = s;
      *(_QWORD *)(a1 + 8) = a8;
      *(_OWORD *)(a1 + 16) = dest;
      *(_QWORD *)(a1 + 32) = a10;
    }
  }
  return a1;
}

//----- (0000000000400D1A) ----------------------------------------------------
int sub_400D1A()
{
  return puts("byebyeT.T");
}

//----- (0000000000400D2B) ----------------------------------------------------
int __fastcall sub_400D2B(
        int a1,
        int a2,
        int a3,
        int a4,
        int a5,
        int a6,
        __int64 format,
        __int64 a8,
        __int128 dest,
        __int64 a10)
{
  int v10; // edi
  int v11; // eax
  int v12; // edx
  int v13; // ecx
  int v14; // r8d
  int v15; // r9d

  v10 = (int)stdin;
  setbuf(stdin, 0LL);
  while ( 1 )
  {
    v11 = sub_400A75();
    switch ( v11 )
    {
      case 2:
        sub_400B41((__int64)&format, 0, v12, v13, v14, v15, format, a8, dest, a10);
        break;
      case 3:
        return sub_400D1A();
      case 1:
        sub_400B07(v10, 0, v12, v13, v14, v15, format, a8, dest);
        break;
      default:
        puts("error options");
        fflush(stdout);
        break;
    }
    v10 = (int)stdout;
    fflush(stdout);
  }
}
// 400D6F: variable 'v12' is possibly undefined
// 400D6F: variable 'v13' is possibly undefined
// 400D6F: variable 'v14' is possibly undefined
// 400D6F: variable 'v15' is possibly undefined
// 400A75: using guessed type __int64 sub_400A75(void);

//----- (0000000000400DD8) ----------------------------------------------------
__int64 __fastcall main(int a1, char **a2, char **a3)
{
  int v3; // edx
  int v4; // ecx
  int v5; // r8d
  int v6; // r9d
  int v7; // edi
  int v8; // edx
  int v9; // ecx
  int v10; // r8d
  int v11; // r9d
  char format[8]; // [rsp+10h] [rbp-60h] BYREF
  __int64 v14; // [rsp+18h] [rbp-58h]
  __int128 dest; // [rsp+20h] [rbp-50h]
  __int64 v16; // [rsp+30h] [rbp-40h]
  __int64 buf; // [rsp+40h] [rbp-30h]
  __int64 v18; // [rsp+48h] [rbp-28h]
  __int64 v19[3]; // [rsp+50h] [rbp-20h]

  buf = 48LL;
  v18 = 0LL;
  LODWORD(v19[0]) = 0;
  *(__int64 *)((char *)v19 + 4) = 48LL;
  *(__int64 *)((char *)&v19[1] + 4) = 0LL;
  HIDWORD(v19[2]) = 0;
  sub_4008BB();
  while ( 1 )
  {
    sub_400903(format, (int)a2, v3, v4, v5, v6, buf, v18, v19[0], v19[1], v19[2]);
    if ( format[0] != 48 )
      break;
    puts("Register failure,try again...");
    fflush(stdout);
  }
  puts("Register Success!!");
  v7 = (int)stdout;
  fflush(stdout);
  sub_400D2B(v7, (int)a2, v8, v9, v10, v11, *(__int64 *)format, v14, dest, v16);
  return 0LL;
}
// 400E39: variable 'v3' is possibly undefined
// 400E39: variable 'v4' is possibly undefined
// 400E39: variable 'v5' is possibly undefined
// 400E39: variable 'v6' is possibly undefined
// 400E93: variable 'v8' is possibly undefined
// 400E93: variable 'v9' is possibly undefined
// 400E93: variable 'v10' is possibly undefined
// 400E93: variable 'v11' is possibly undefined

//----- (0000000000400EB0) ----------------------------------------------------
void __fastcall init(unsigned int a1, __int64 a2, __int64 a3)
{
  __int64 v4; // rbx
  signed __int64 v5; // rbp

  v4 = 0LL;
  v5 = &off_601DA8 - off_601DA0;
  init_proc();
  if ( v5 )
  {
    do
      ((void (__fastcall *)(_QWORD, __int64, __int64))off_601DA0[v4++])(a1, a2, a3);
    while ( v4 != v5 );
  }
}
// 601DA0: using guessed type __int64 (__fastcall *off_601DA0[2])();
// 601DA8: using guessed type __int64 (__fastcall *off_601DA8)();

//----- (0000000000400F24) ----------------------------------------------------
void term_proc()
{
  ;
}

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