//
// This file was generated by the Retargetable Decompiler
// Website: https://retdec.com
// Copyright (c) 2019 Retargetable Decompiler <info@retdec.com>
//

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

// ------------------------ Structures ------------------------

struct _IO_FILE {
    int32_t e0;
};

// ------------------- Function Prototypes --------------------

int64_t __do_global_dtors_aux(void);
int64_t __libc_csu_fini(void);
int64_t __libc_csu_init(int64_t a1, int64_t a2, int64_t a3);
int64_t _fini(void);
int64_t _init(void);
int64_t _start(int64_t a1, int64_t a2, int64_t a3, int64_t a4);
int64_t deregister_tm_clones(void);
int64_t frame_dummy(void);
int32_t function_1030(char * s);
int32_t function_1040(int64_t * ptr, int32_t size, int32_t n, struct _IO_FILE * stream);
int32_t function_1050(struct _IO_FILE * stream);
void function_1060(void);
int32_t function_1070(int32_t c, struct _IO_FILE * stream);
struct _IO_FILE * function_1080(char * filename, char * modes);
void function_1090(int32_t status);
void function_10a0(int64_t * d);
int64_t register_tm_clones(void);

// --------------------- Global Variables ---------------------

int64_t g1 = 0x1190;
int64_t g2 = 0x1150;
int64_t g3 = 0;
int32_t g4;

// ------------------------ Functions -------------------------

// Address range: 0x1000 - 0x1017
int64_t _init(void) {
    // 0x1000
    int64_t result; // 0x1016
    if (*(int64_t *)0x3fe8 != 0) {
        // 0x1010
        __gmon_start__();
        result = &g4;
    } else {
        result = 0;
    }
    // 0x1012
    return result;
}

// Address range: 0x1030 - 0x1036
int32_t function_1030(char * s) {
    // 0x1030
    return puts(s);
}

// Address range: 0x1040 - 0x1046
int32_t function_1040(int64_t * ptr, int32_t size, int32_t n, struct _IO_FILE * stream) {
    // 0x1040
    return fread(ptr, size, n, stream);
}

// Address range: 0x1050 - 0x1056
int32_t function_1050(struct _IO_FILE * stream) {
    // 0x1050
    return fclose(stream);
}

// Address range: 0x1060 - 0x1066
void function_1060(void) {
    // 0x1060
    __stack_chk_fail();
}

// Address range: 0x1070 - 0x1076
int32_t function_1070(int32_t c, struct _IO_FILE * stream) {
    // 0x1070
    return fputc(c, stream);
}

// Address range: 0x1080 - 0x1086
struct _IO_FILE * function_1080(char * filename, char * modes) {
    // 0x1080
    return fopen(filename, modes);
}

// Address range: 0x1090 - 0x1096
void function_1090(int32_t status) {
    // 0x1090
    exit(status);
}

// Address range: 0x10a0 - 0x10a6
void function_10a0(int64_t * d) {
    // 0x10a0
    __cxa_finalize(d);
}

// Address range: 0x10b0 - 0x10db
int64_t _start(int64_t a1, int64_t a2, int64_t a3, int64_t a4) {
    // 0x10b0
    int64_t v1;
    __libc_start_main(0x1195, (int32_t)a4, (char **)&v1, (void (*)())0x13a0, (void (*)())0x1400, (void (*)())a3);
    __asm_hlt();
    // UNREACHABLE
}

// Address range: 0x10e0 - 0x1109
int64_t deregister_tm_clones(void) {
    // 0x10e0
    return &g3;
}

// Address range: 0x1110 - 0x1149
int64_t register_tm_clones(void) {
    // 0x1148
    return 0;
}

// Address range: 0x1150 - 0x1189
int64_t __do_global_dtors_aux(void) {
    // 0x1150
    if (*(char *)&g3 != 0) {
        // 0x1188
        int64_t result; // rax
        return result;
    }
    if (*(int64_t *)0x3ff8 != 0) {
        // 0x1167
        __cxa_finalize((int64_t *)*(int64_t *)0x4008);
    }
    int64_t result2 = deregister_tm_clones(); // 0x1173
    *(char *)&g3 = 1;
    return result2;
}

// Address range: 0x1190 - 0x1195
int64_t frame_dummy(void) {
    // 0x1190
    return register_tm_clones();
}

// Address range: 0x1195 - 0x1392
int main(int argc, char ** argv) {
    int64_t v1 = __readfsqword(40); // 0x119d
    struct _IO_FILE * file = fopen("flag.txt", "r"); // 0x11ba
    struct _IO_FILE * file2 = fopen("mystery.png", "a"); // 0x11d1
    if (file == NULL) {
        // 0x11e1
        puts("No flag found, please make sure this is run on the server");
    }
    if (file2 == NULL) {
        // 0x11f4
        puts("mystery.png is missing, please run this on the server");
    }
    // 0x1200
    char data; // bp-56
    int32_t items_read = fread((int64_t *)&data, 26, 1, file); // 0x1218
    if (items_read >= 0 != items_read != 0) {
        // 0x1226
        exit(0);
        // UNREACHABLE
    }
    // 0x1230
    puts("at insert");
    fputc((int32_t)data, file2);
    unsigned char c;
    fputc((int32_t)c, file2);
    unsigned char c2;
    fputc((int32_t)c2, file2);
    unsigned char c3;
    fputc((int32_t)c3, file2);
    unsigned char c4;
    fputc((int32_t)c4, file2);
    unsigned char c5;
    fputc((int32_t)c5, file2);
    int64_t v2; // rbp
    for (int64_t i = 6; i < 15; i++) {
        // 0x12db
        fputc((int32_t)*(char *)(i - 48 + v2) + 5, file2);
    }
    // 0x130e
    int32_t v3;
    fputc(v3 - 3, file2);
    for (int64_t i = 16; i < 26; i++) {
        // 0x133a
        fputc((int32_t)*(char *)(i - 48 + v2), file2);
    }
    // 0x1363
    fclose(file2);
    fclose(file);
    int64_t result; // 0x1391
    if (__readfsqword(40) != v1) {
        // 0x138b
        __stack_chk_fail();
        result = &g4;
    } else {
        result = 0;
    }
    // 0x1390
    return result;
}

// Address range: 0x13a0 - 0x13fd
int64_t __libc_csu_init(int64_t a1, int64_t a2, int64_t a3) {
    int64_t result = _init(); // 0x13c8
    if (((int64_t)&g2 - (int64_t)&g1) / 8 != 0) {
        int64_t v1 = 0; // 0x13e52
        while (v1 + 1 != ((int64_t)&g2 - (int64_t)&g1) / 8) {
            // 0x13d8
            v1++;
        }
    }
    // 0x13ee
    return result;
}

// Address range: 0x1400 - 0x1401
int64_t __libc_csu_fini(void) {
    // 0x1400
    int64_t result; // rax
    return result;
}

// Address range: 0x1404 - 0x140d
int64_t _fini(void) {
    // 0x1404
    int64_t result; // rax
    return result;
}

// --------------- Dynamically Linked Functions ---------------

// void __cxa_finalize(void * d);
// void __gmon_start__(void);
// int __libc_start_main(int *(main)(int, char **, char **), int argc, char ** ubp_av, void(* init)(void), void(* fini)(void), void(* rtld_fini)(void), void(* stack_end));
// void __stack_chk_fail(void);
// void exit(int status);
// int fclose(FILE * stream);
// FILE * fopen(const char * restrict filename, const char * restrict modes);
// int fputc(int c, FILE * stream);
// size_t fread(void * restrict ptr, size_t size, size_t n, FILE * restrict stream);
// int puts(const char * s);

// --------------------- Meta-Information ---------------------

// Detected compiler/packer: gcc (8.2.0)
// Detected functions: 18
// Decompilation date: 2019-09-30 18:36:40
