#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */
typedef struct TYPE_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int e_shoff;
  int e_shnum;
  size_t e_shstrndx;
};
struct TYPE_3__ {
  int sh_offset;
  int sh_name;
};
typedef TYPE_1__ Elf32_Shdr;
typedef TYPE_2__ Elf32_Ehdr;

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
char *mmap(int /*<<< orphan*/, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *const, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, int, char const *const);
int print_shdr(const char *const fname, size_t size) {
  int fd = open(fname, O_RDONLY);
  char *p = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
  Elf32_Ehdr *ehdr = (Elf32_Ehdr *)p;
  Elf32_Shdr *shdr = (Elf32_Shdr *)(p + ehdr->e_shoff);
  int shnum = ehdr->e_shnum;
  Elf32_Shdr *sh_strtab = &shdr[ehdr->e_shstrndx];
  const char *const sh_strtab_p = p + sh_strtab->sh_offset;
  for (int i = 0; i < shnum; ++i) {
    printf("%2d: %4d '%s'\n", i, shdr[i].sh_name,
           sh_strtab_p + shdr[i].sh_name);
  }
  return 0;
}


x86 assembly (clang, -O0):
<print_shdr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
mov    0x2e98(%rip),%rax
mov    (%rax),%edx
mov    0x2e67(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    0x8(%rcx),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x40(%rbp),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    121b <print_shdr+0xeb>
mov    -0x4c(%rbp),%esi
mov    -0x30(%rbp),%rax
movslq -0x4c(%rbp),%rcx
mov    0x4(%rax,%rcx,8),%edx
mov    -0x48(%rbp),%rcx
mov    -0x30(%rbp),%rax
movslq -0x4c(%rbp),%rdi
movslq 0x4(%rax,%rdi,8),%rax
add    %rax,%rcx
lea    0xdf8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    11d2 <print_shdr+0xa2>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_shdr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    0x2ea7(%rip),%rcx
mov    (%rcx),%edx
mov    0x2e76(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x4(%rax),%r15d
test   %r15d,%r15d
jle    11be <print_shdr+0x8e>
movslq (%rax),%rcx
lea    (%rax,%rcx,1),%rdx
mov    0x8(%rax),%rsi
movslq (%rdx,%rsi,8),%r12
add    %rax,%r12
lea    (%rcx,%rax,1),%r13
add    $0x4,%r13
xor    %ebx,%ebx
lea    0xe68(%rip),%r14
nopl   0x0(%rax,%rax,1)
movslq 0x0(%r13,%rbx,8),%rdx
mov    %rdx,%rcx
add    %r12,%rcx
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11a0 <print_shdr+0x70>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i32, i64 }
%struct.TYPE_3__ = type { i32, i32 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str = [15 x i8] c"%2d: %4d '%s'\0A\00"
define i32 @print_shdr(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.TYPE_4__*
%8 = alloca %struct.TYPE_3__*
%9 = alloca i32
%10 = alloca %struct.TYPE_3__*
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%13 = load i8*, i8** %3
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %5
%16 = load i64, i64* %4
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @MAP_PRIVATE
%19 = load i32, i32* %5
%20 = call i8* @mmap(i32 0, i64 %16, i32 %17, i32 %18, i32 %19, i32 0)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = bitcast i8* %21 to %struct.TYPE_4__*
store %struct.TYPE_4__* %22, %struct.TYPE_4__** %7
%23 = load i8*, i8** %6
%24 = load %struct.TYPE_4__*, %struct.TYPE_4__** %7
%25 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %23, i64 %27
%29 = bitcast i8* %28 to %struct.TYPE_3__*
store %struct.TYPE_3__* %29, %struct.TYPE_3__** %8
%30 = load %struct.TYPE_4__*, %struct.TYPE_4__** %7
%31 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %30, i32 0, i32 1
%32 = load i32, i32* %31
store i32 %32, i32* %9
%33 = load %struct.TYPE_3__*, %struct.TYPE_3__** %8
%34 = load %struct.TYPE_4__*, %struct.TYPE_4__** %7
%35 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %34, i32 0, i32 2
%36 = load i64, i64* %35
%37 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %33, i64 %36
store %struct.TYPE_3__* %37, %struct.TYPE_3__** %10
%38 = load i8*, i8** %6
%39 = load %struct.TYPE_3__*, %struct.TYPE_3__** %10
%40 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %38, i64 %42
store i8* %43, i8** %11
store i32 0, i32* %12
br label %44
44: ; preds = %66, %2
%45 = load i32, i32* %12
%46 = load i32, i32* %9
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %69
48: ; preds = %44
%49 = load i32, i32* %12
%50 = load %struct.TYPE_3__*, %struct.TYPE_3__** %8
%51 = load i32, i32* %12
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %50, i64 %52
%54 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = load i8*, i8** %11
%57 = load %struct.TYPE_3__*, %struct.TYPE_3__** %8
%58 = load i32, i32* %12
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %57, i64 %59
%61 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %60, i32 0, i32 1
%62 = load i32, i32* %61
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %56, i64 %63
%65 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %49, i32 %55, i8* %64)
br label %66
66: ; preds = %48
%67 = load i32, i32* %12
%68 = add nsw i32 %67, 1
store i32 %68, i32* %12
br label %44
69: ; preds = %44
ret i32 0
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str = [15 x i8] c"%2d: %4d '%s'\0A\00"
define i32 @print_shdr(i8* nocapture %0, i64 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @MAP_PRIVATE
%7 = tail call i8* @mmap(i32 0, i64 %1, i32 %5, i32 %6, i32 %4, i32 0)
%8 = bitcast i8* %7 to i32*
%9 = load i32, i32* %8
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %7, i64 %10
%12 = bitcast i8* %11 to %struct.TYPE_3__*
%13 = getelementptr inbounds i8, i8* %7, i64 4
%14 = bitcast i8* %13 to i32*
%15 = load i32, i32* %14
%16 = getelementptr inbounds i8, i8* %7, i64 8
%17 = bitcast i8* %16 to i64*
%18 = load i64, i64* %17
%19 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %12, i64 %18, i32 0
%20 = load i32, i32* %19
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %7, i64 %21
%23 = icmp sgt i32 %15, 0
br i1 %23, label %24, label %26
24: ; preds = %2
%25 = zext i32 %15 to i64
br label %27
26: ; preds = %27, %2
ret i32 0
27: ; preds = %24, %27
%28 = phi i64 [ 0, %24 ], [ %35, %27 ]
%29 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %12, i64 %28, i32 1
%30 = load i32, i32* %29
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %22, i64 %31
%33 = trunc i64 %28 to i32
%34 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %33, i32 %30, i8* %32)
%35 = add nuw nsw i64 %28, 1
%36 = icmp eq i64 %35, %25
br i1 %36, label %26, label %27
}