// File: /workspace/github_cpp_makefile_star2_O2/coduin[P]epiphany-bin-info/epiphany-bin-info.cpp
// Function: parseProgramHeader(Elf32_Phdr*, unsigned long)
void parseProgramHeader(Elf32_Phdr* phdr, size_t count) {
    int localsize = 0, extmemsize = 0;
    for (size_t ihdr = 0; ihdr < count; ihdr++) {
        if (isExtmem(phdr[ihdr].p_vaddr))
            extmemsize += phdr[ihdr].p_memsz;
        else
            localsize += phdr[ihdr].p_memsz;
    }

    cout << "Total size in local memory:    " << ashex(localsize) << " = "
         << localsize / 1024 << " KB" << endl;
    cout << "Total size in external memory: " << ashex(extmemsize) << " = "
         << extmemsize / 1024 << " KB" << endl;

    if (showProgramHeaders) {
        cout << "\nProgram header table (runtime info):\n";
        cout << "VADDR       SIZE\t\t\tFILESIZE\n";
        cout << setfill('0') << hex;
        for (size_t ihdr = 0; ihdr < count; ihdr++) {
            cout << addr(phdr[ihdr].p_vaddr);
            cout << "  " << ashex(phdr[ihdr].p_memsz);
            cout << " (" << phdr[ihdr].p_memsz / 1024 << " KB)";
            cout << "\t" << ashex(phdr[ihdr].p_filesz) << endl;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x14(%rdi), %rax
xorl %ecx, %ecx
movq %rsi, %rdx
xorl %ebp, %ebp
xorl %r15d, %r15d
subq $0x1, %rdx
jb 0x262f
movl (%rax), %esi
cmpl $0x100000, -0xc(%rax)   # imm = 0x100000
movl $0x0, %edi
cmovael %esi, %edi
cmovael %ecx, %esi
addl %edi, %ebp
addl %esi, %r15d
addq $0x20, %rax
jmp 0x260a
movq 0x499a(%rip), %rdi      # 0x6fd0
leaq 0x19e0(%rip), %rsi      # 0x401d
callq 0x2170
leaq 0x4(%rsp), %r12
movl %r15d, (%r12)
movq %rax, %rdi
movq %r12, %rsi
callq 0x249d
leaq 0x19e0(%rip), %r13      # 0x403d
movq %rax, %rdi
movq %r13, %rsi
callq 0x2170
movq %rax, %rdi
movl $0x400, %ecx            # imm = 0x400
movl %r15d, %eax
cltd
idivl %ecx
movl %eax, %esi
callq 0x2270
leaq 0x19bd(%rip), %r15      # 0x4041
movq %rax, %rdi
movq %r15, %rsi
callq 0x2170
movq %rax, %rdi
callq 0x20d0
leaq 0x19a7(%rip), %rsi      # 0x4045
movq 0x492b(%rip), %rdi      # 0x6fd0
callq 0x2170
movl %ebp, (%r12)
leaq 0x4(%rsp), %rsi
movq %rax, %rdi
callq 0x249d
movq %rax, %rdi
movq %r13, %rsi
callq 0x2170
movq %rax, %rdi
movl %ebp, %eax
cltd
movl $0x400, %ecx            # imm = 0x400
idivl %ecx
movl %eax, %esi
callq 0x2270
movq %rax, %rdi
movq %r15, %rsi
callq 0x2170
movq %rax, %rdi
callq 0x20d0
cmpb $0x0, 0x4aa6(%rip)      # 0x719a
je 0x27eb
movq 0x48cf(%rip), %r15      # 0x6fd0
leaq 0x195d(%rip), %rsi      # 0x4065
movq %r15, %rdi
callq 0x2170
leaq 0x1975(%rip), %rsi      # 0x408c
movq %r15, %rdi
callq 0x2170
pushq $0x30
popq %rsi
movq %r15, %rdi
callq 0x2250
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
addq $0x14, %r14
leaq 0x4(%rsp), %r12
leaq 0x195d(%rip), %r13      # 0x40ac
leaq 0x195b(%rip), %rbp      # 0x40b1
subq $0x1, %rbx
jb 0x27eb
movl -0xc(%r14), %eax
movl %eax, 0x4(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x24fb
movq %r15, %rdi
leaq 0x197b(%rip), %rsi      # 0x40f8
callq 0x2170
movl (%r14), %ecx
movl %ecx, 0x4(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x249d
movq %r15, %rdi
leaq 0x190b(%rip), %rsi      # 0x40a9
callq 0x2170
movl (%r14), %esi
shrl $0xa, %esi
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
movq %r13, %rsi
callq 0x2170
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2170
movl -0x4(%r14), %ecx
movl %ecx, 0x4(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x249d
movq %rax, %rdi
callq 0x20d0
addq $0x20, %r14
jmp 0x2756
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq