#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>

int main(int argc, char *argv[]) {
    FILE *backup_file;

    int file_header;
    char file_header_buf[25];
    struct stat file_stat;

    backup_file = fopen(argv[1], "r");
    if (!backup_file) {
        printf("Could not open %s\n", argv[1]);
        return -1;
    };

    file_header = fread(file_header_buf, 1, 24, backup_file);
    if (!file_header) {
        printf("Could not read %s\n", argv[1]);
        return -1;
    };

    if (strncmp("ALCATEL BACKUP FILE HEAD", file_header_buf, 24)) {
        printf("Could not find \"ALCATEL BACKUP FILE HEAD\" header");
        return -1;
    };

    fstat(fileno(backup_file), &file_stat);

    // I'm to lazy to fix the rest, so let's just copy paste the rest from Ghidra
    size_t file_size;
    size_t file_size_buf [2];
    char *file_name;
    size_t sVar2;
    size_t sVar3;
    FILE *__s;
    char output_file [256];
    size_t file_size_2;

    while (file_header + 4 < file_stat.st_blocks) {
        file_size = fread(file_size_buf,1,4,backup_file);
        if ((int)file_size_buf[0] < 1) {
            printf("Could not get size");
            return -1;
        };
        file_name = malloc(file_size_buf[0] + 1);
        if (file_name != (void *)0x0) {
            memset(file_name,0,file_size_buf[0] + 1);
        }
        sVar2 = fread(file_name,1,file_size_buf[0],backup_file);
        snprintf(output_file, 0x100, "%s", file_name);
        if (file_name != (void *)0x0) {
            free(file_name);
        }
        __s = fopen(output_file,"w");
        if (__s == (FILE *)0x0) {
            printf("Could not open output file");
            return -1;
        };
        file_size_2 = fread(file_size_buf,1,4,backup_file);
        if ((int)file_size_buf[0] < 1) {
            printf("Could not get size");
            return -1;
        };
        file_name = malloc(file_size_buf[0]);
        if (file_name != (void *)0x0) {
            memset(file_name,0,file_size_buf[0]);
        }
        sVar3 = fread(file_name,1,file_size_buf[0],backup_file);
        file_header = file_header + file_size + sVar2 + file_size_2 + sVar3;
        fwrite(file_name,file_size_buf[0],1,__s);
        fclose(__s);
        if (file_name != (void *)0x0) {
            free(file_name);
        }
    };
    fclose(backup_file);
    return 0;
}
