//
// 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);
int32_t function_1060(int32_t c, struct _IO_FILE * stream);
struct _IO_FILE * function_1070(char * filename, char * modes);
void function_1080(int32_t status);
void function_1090(int64_t * d);
int64_t register_tm_clones(void);

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

int64_t g1 = 0x1180;
int64_t g2 = 0x1140;
int64_t g3 = 0;
int64_t g4 = 0;
int32_t g5;

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

// Address range: 0x1000 - 0x1017
int64_t _init(void) {
    // 0x1000
    int64_t result; // 0x1016
    if (*(int64_t *)0x3fe8 != 0) {
        // 0x1010
        __gmon_start__();
        result = &g5;
    } 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
int32_t function_1060(int32_t c, struct _IO_FILE * stream) {
    // 0x1060
    return fputc(c, stream);
}

// Address range: 0x1070 - 0x1076
struct _IO_FILE * function_1070(char * filename, char * modes) {
    // 0x1070
    return fopen(filename, modes);
}

// Address range: 0x1080 - 0x1086
void function_1080(int32_t status) {
    // 0x1080
    exit(status);
}

// Address range: 0x1090 - 0x1096
void function_1090(int64_t * d) {
    // 0x1090
    __cxa_finalize(d);
}

// Address range: 0x10a0 - 0x10cb
int64_t _start(int64_t a1, int64_t a2, int64_t a3, int64_t a4) {
    // 0x10a0
    int64_t v1;
    __libc_start_main(0x1185, (int32_t)a4, (char **)&v1, (void (*)())0x12d0, (void (*)())0x1330, (void (*)())a3);
    __asm_hlt();
    // UNREACHABLE
}

// Address range: 0x10d0 - 0x10f9
int64_t deregister_tm_clones(void) {
    // 0x10d0
    return &g4;
}

// Address range: 0x1100 - 0x1139
int64_t register_tm_clones(void) {
    // 0x1138
    return 0;
}

// Address range: 0x1140 - 0x1179
int64_t __do_global_dtors_aux(void) {
    // 0x1140
    if (*(char *)&g4 != 0) {
        // 0x1178
        int64_t result; // rax
        return result;
    }
    if (g3 != 0) {
        // 0x1157
        __cxa_finalize((int64_t *)*(int64_t *)0x4050);
    }
    int64_t result2 = deregister_tm_clones(); // 0x1163
    *(char *)&g4 = 1;
    return result2;
}

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

// Address range: 0x1185 - 0x12c9
int main(int argc, char ** argv) {
    struct _IO_FILE * file = fopen("flag.txt", "r"); // 0x119b
    struct _IO_FILE * file2 = fopen("rev_this", "a"); // 0x11b2
    if (file == NULL) {
        // 0x11c2
        puts("No flag found, please make sure this is run on the server");
    }
    if (file2 == NULL) {
        // 0x11d5
        puts("please run this on the server");
    }
    // 0x11e1
    int64_t data; // bp-88
    int32_t items_read = fread(&data, 24, 1, file); // 0x11f9
    if (items_read >= 0 != items_read != 0) {
        // 0x1207
        exit(0);
        // UNREACHABLE
    }
    int64_t v1; // rbp
    for (int64_t i = 0; i < 8; i++) {
        // 0x121a
        fputc((int32_t)*(char *)(i - 80 + v1), file2);
    }
    for (int64_t i = 8; i < 23; i++) {
        unsigned char v2 = *(char *)(i - 80 + v1); // 0x1251
        fputc((int32_t)v2 + (i % 2 != 0 ? -2 : 5), file2);
    }
    // 0x1295
    int32_t c;
    fputc(c, file2);
    fclose(file2);
    return fclose(file);
}

// Address range: 0x12d0 - 0x132d
int64_t __libc_csu_init(int64_t a1, int64_t a2, int64_t a3) {
    int64_t result = _init(); // 0x12f8
    if (((int64_t)&g2 - (int64_t)&g1) / 8 != 0) {
        int64_t v1 = 0; // 0x13152
        while (v1 + 1 != ((int64_t)&g2 - (int64_t)&g1) / 8) {
            // 0x1308
            v1++;
        }
    }
    // 0x131e
    return result;
}

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

// Address range: 0x1334 - 0x133d
int64_t _fini(void) {
    // 0x1334
    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 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: 17
// Decompilation date: 2019-09-29 16:43:12
