// File: /workspace/github_cpp_makefile_star2_O1/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
testq %rsi, %rsi
je 0x2702
leaq 0x14(%r14), %rax
xorl %ecx, %ecx
movq %rbx, %rdx
xorl %r15d, %r15d
xorl %ebp, %ebp
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
decq %rdx
jne 0x26de
jmp 0x2707
xorl %ebp, %ebp
xorl %r15d, %r15d
movq 0x58c2(%rip), %r12      # 0x7fd0
leaq 0x2908(%rip), %rsi      # 0x501d
movl $0x1f, %edx
movq %r12, %rdi
callq 0x21a0
leaq 0x4(%rsp), %rsi
movl %r15d, (%rsi)
movq %r12, %rdi
callq 0x24cc
movq %rax, %r13
leaq 0x2901(%rip), %rsi      # 0x503d
movl $0x3, %edx
movq %rax, %rdi
callq 0x21a0
movl $0x3ff, %r12d           # imm = 0x3FF
leal (%r15,%r12), %esi
testl %r15d, %r15d
cmovnsl %r15d, %esi
movq 0x586f(%rip), %r15      # 0x7fd0
sarl $0xa, %esi
movq %r13, %rdi
callq 0x2250
movq %rax, %r13
leaq 0x28cb(%rip), %rsi      # 0x5041
movl $0x3, %edx
movq %rax, %rdi
callq 0x21a0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2100
leaq 0x2893(%rip), %rsi      # 0x5045
movl $0x1f, %edx
movq %r15, %rdi
callq 0x21a0
leaq 0x4(%rsp), %rax
movl %ebp, (%rax)
leaq 0x4(%rsp), %rsi
movq %r15, %rdi
callq 0x24cc
movq %rax, %r13
movl $0x3, %edx
movq %rax, %rdi
leaq 0x2858(%rip), %rsi      # 0x503d
callq 0x21a0
addl %ebp, %r12d
testl %ebp, %ebp
cmovnsl %ebp, %r12d
sarl $0xa, %r12d
movq %r13, %rdi
movl %r12d, %esi
callq 0x2250
movq %rax, %r12
movl $0x3, %edx
movq %rax, %rdi
leaq 0x282d(%rip), %rsi      # 0x5041
callq 0x21a0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2100
cmpb $0x0, 0x593a(%rip)      # 0x8182
je 0x29b3
leaq 0x2810(%rip), %rsi      # 0x5065
movl $0x26, %edx
movq %r15, %rdi
callq 0x21a0
leaq 0x2823(%rip), %rsi      # 0x508c
movl $0x1c, %edx
movq %r15, %rdi
callq 0x21a0
movq (%r15), %rax
movq -0x18(%rax), %rax
leaq (%r15,%rax), %r12
cmpb $0x0, 0xe1(%r15,%rax)
jne 0x28aa
movq %r12, %rdi
movl $0x20, %esi
callq 0x2160
movb %al, 0xe0(%r12)
movb $0x1, 0xe1(%r12)
movb $0x30, 0xe0(%r12)
movq (%r15), %rax
movq -0x18(%rax), %rax
movl 0x18(%r15,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r15,%rax)
testq %rbx, %rbx
je 0x29b3
addq $0x14, %r14
movq 0x56f2(%rip), %r15      # 0x7fd0
leaq 0x4(%rsp), %r12
leaq 0x27c7(%rip), %rbp      # 0x50b1
movl -0xc(%r14), %eax
movl %eax, 0x4(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x2569
movl $0x2, %edx
movq %r15, %rdi
leaq 0x27ec(%rip), %rsi      # 0x50f8
callq 0x21a0
movl (%r14), %eax
movl %eax, 0x4(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x24cc
movl $0x2, %edx
movq %r15, %rdi
leaq 0x2777(%rip), %rsi      # 0x50a9
callq 0x21a0
movl (%r14), %esi
shrl $0xa, %esi
movq %r15, %rdi
callq 0x20f0
movl $0x4, %edx
movq %rax, %rdi
leaq 0x2758(%rip), %rsi      # 0x50ac
callq 0x21a0
movl $0x1, %edx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x21a0
movl -0x4(%r14), %eax
movl %eax, 0x4(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x24cc
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2100
addq $0x20, %r14
decq %rbx
jne 0x28ea
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq