// File: /workspace/github_c_makefile_star2_O2/LeeKyuHyuk[P]gpt-parser/src/gpt-parser.c
// Function: main
int main(int argc, char const *argv[]) {
        int partition, name;

        if(argc != 2) {
                fprintf(stderr,"Usage: %s <file name>\n",argv[0]);
                return EXIT_FAILURE;
        }

        FILE *gptFile = fopen(argv[1], "r");
        if(gptFile == NULL) {
                fprintf(stderr, "Error opening gpt file\n");
                return EXIT_FAILURE;
        }
        gpt_t gpt;
        if(fread((void *)&gpt, 1, sizeof(gpt_t), gptFile) != sizeof(gpt_t)) {
                fprintf(stderr,"Error reading gpt\n");
                return EXIT_FAILURE;
        }

        printf("##### Primary GPT Header #####\n");
        printf("Signature : %s\n", gpt.primary_gpt_header.signature);
        printf("GPT Revision : %d\n", gpt.primary_gpt_header.gpt_revision);
        printf("Header Size : %d\n", gpt.primary_gpt_header.header_size);
        printf("Header CRC32 : 0x%08X\n", gpt.primary_gpt_header.header_crc32);
        printf("Current LBA : 0x%08lX\n", gpt.primary_gpt_header.current_lba);
        printf("Backup LBA : 0x%08lX\n", gpt.primary_gpt_header.backup_lba);
        printf("First Usable Partitions : 0x%08lX\n", gpt.primary_gpt_header.first_usable_partitions);
        printf("Last Usable Partitions : 0x%08lX\n", gpt.primary_gpt_header.last_usable_partitions);
        printf("Start Partition Entries : 0x%08lX\n", gpt.primary_gpt_header.start_partition_entries);
        printf("Number of Partition Entries : %d\n", gpt.primary_gpt_header.number_of_partition_entries);
        printf("Partition Entry Size : 0x%08x\n", gpt.primary_gpt_header.partition_entry_size);
        printf("Partition Array CRC32 : %#08x\n\n", gpt.primary_gpt_header.partition_array_crc32);

        for(partition = 0; partition < gpt.primary_gpt_header.number_of_partition_entries; partition++) {
                if(gpt.guid_partition_entries[partition].first_lba != 0x0 &&
                   gpt.guid_partition_entries[partition].last_lba != 0x00) {
                        printf("##### GUID Partition Entries [%d] #####\n", partition);
                        printf("First LBA : %ld\n", gpt.guid_partition_entries[partition].first_lba);
                        printf("\t=> Disk Offset : 0x%08lX\n", gpt.guid_partition_entries[partition].first_lba * LBA_SIZE);
                        printf("Last LBA : %ld\n", gpt.guid_partition_entries[partition].last_lba);
                        printf("\t=> Disk Offset : 0x%08lX\n", gpt.guid_partition_entries[partition].last_lba * LBA_SIZE);
                        printf("Partition Name : ");
                        for (name = 0; name < 72; name++)
                                printf("%c", gpt.guid_partition_entries[partition].partition_name[name]);
                        printf("\n\n");
                }
        }

        return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4408, %rsp           # imm = 0x4408
cmpl $0x2, %edi
jne 0x401959
movq 0x8(%rsi), %rdi
leaq 0xadf17(%rip), %rsi     # 0x4af667
callq 0x412260
testq %rax, %rax
je 0x401976
leaq -0x4428(%rbp), %rdi
pushq $0x1
popq %rsi
movl $0x4400, %edx           # imm = 0x4400
movq %rax, %rcx
callq 0x412360
cmpq $0x4400, %rax           # imm = 0x4400
jne 0x40198b
leaq 0x96a77(%rip), %rdi     # 0x4981ff
callq 0x412730
leaq -0x4228(%rbp), %rbx
leaq 0x968ab(%rip), %rdi     # 0x498046
movq %rbx, %rsi
xorl %eax, %eax
callq 0x40b8d0
movl 0x8(%rbx), %esi
leaq 0x968a7(%rip), %rdi     # 0x498056
xorl %eax, %eax
callq 0x40b8d0
movl 0xc(%rbx), %esi
leaq 0x968a9(%rip), %rdi     # 0x498069
xorl %eax, %eax
callq 0x40b8d0
movl 0x10(%rbx), %esi
leaq 0x968aa(%rip), %rdi     # 0x49807b
xorl %eax, %eax
callq 0x40b8d0
movq 0x18(%rbx), %rsi
leaq 0x968af(%rip), %rdi     # 0x498092
xorl %eax, %eax
callq 0x40b8d0
movq 0x20(%rbx), %rsi
leaq 0x968b4(%rip), %rdi     # 0x4980a9
xorl %eax, %eax
callq 0x40b8d0
movq 0x28(%rbx), %rsi
leaq 0x968b8(%rip), %rdi     # 0x4980bf
xorl %eax, %eax
callq 0x40b8d0
movq 0x30(%rbx), %rsi
leaq 0x968c9(%rip), %rdi     # 0x4980e2
xorl %eax, %eax
callq 0x40b8d0
movq 0x48(%rbx), %rsi
leaq 0x968d9(%rip), %rdi     # 0x498104
xorl %eax, %eax
callq 0x40b8d0
movl 0x50(%rbx), %esi
leaq 0x968eb(%rip), %rdi     # 0x498127
xorl %eax, %eax
callq 0x40b8d0
movl 0x54(%rbx), %esi
leaq 0x968fc(%rip), %rdi     # 0x498149
xorl %eax, %eax
callq 0x40b8d0
movl 0x58(%rbx), %esi
leaq 0x9690a(%rip), %rdi     # 0x498168
xorl %eax, %eax
callq 0x40b8d0
leaq -0x4028(%rbp), %r13
leaq -0x3ff0(%rbp), %rbx
leaq 0x96948(%rip), %r12     # 0x4981c2
xorl %r14d, %r14d
movl -0x41d8(%rbp), %eax
cmpq %rax, %r14
jae 0x401945
movq %r14, %r15
shlq $0x7, %r15
cmpq $0x0, 0x20(%r13,%r15)
je 0x401939
addq %r13, %r15
cmpq $0x0, 0x28(%r15)
je 0x401939
leaq 0x968d4(%rip), %rdi     # 0x498188
movl %r14d, %esi
xorl %eax, %eax
callq 0x40b8d0
movq 0x20(%r15), %rsi
leaq 0x968e8(%rip), %rdi     # 0x4981b1
xorl %eax, %eax
callq 0x40b8d0
movq 0x20(%r15), %rsi
shlq $0x9, %rsi
movq %r12, %rdi
xorl %eax, %eax
callq 0x40b8d0
movq 0x28(%r15), %rsi
leaq 0x968f0(%rip), %rdi     # 0x4981dd
xorl %eax, %eax
callq 0x40b8d0
movq 0x28(%r15), %rsi
shlq $0x9, %rsi
movq %r12, %rdi
xorl %eax, %eax
callq 0x40b8d0
leaq 0x968e0(%rip), %rdi     # 0x4981ed
xorl %eax, %eax
callq 0x40b8d0
pushq $-0x48
popq %r15
testq %r15, %r15
je 0x40192d
movzbl 0x48(%rbx,%r15), %edi
callq 0x4128d0
incq %r15
jmp 0x401918
leaq 0x96e56(%rip), %rdi     # 0x49878a
callq 0x412730
incq %r14
subq $-0x80, %rbx
jmp 0x40187d
xorl %eax, %eax
addq $0x4408, %rsp           # imm = 0x4408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x4c86e8, %rax         # imm = 0x4C86E8
movq (%rax), %rdi
movq (%rsi), %rdx
leaq 0x96697(%rip), %rsi     # 0x498004
xorl %eax, %eax
callq 0x40b810
jmp 0x4019a7
movq $0x4c86e8, %rax         # imm = 0x4C86E8
movq (%rax), %rcx
leaq 0x96694(%rip), %rdi     # 0x49801b
pushq $0x17
jmp 0x40199e
movq $0x4c86e8, %rax         # imm = 0x4C86E8
movq (%rax), %rcx
leaq 0x96697(%rip), %rdi     # 0x498033
pushq $0x12
popq %rsi
pushq $0x1
popq %rdx
callq 0x412460
pushq $0x1
popq %rax
jmp 0x401947
nopl (%rax)