#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_1__;

/* Type definitions */
struct TYPE_4__ {
  int e_phnum;
  int e_phentsize;
  int /*<<< orphan*/ e_phoff;
};
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ Elf64_Phdr;
typedef TYPE_1__ Elf64_Ehdr;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int _do_phdr64(char const *, int /*<<< orphan*/ *, TYPE_1__ *,
               int /*<<< orphan*/ *, char const *);
int _error(char const *, char *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *malloc(int);
char *strerror(int /*<<< orphan*/);
int _do_ldd64(char const *filename, FILE *fp, Elf64_Ehdr *ehdr,
              char const *ldpath) {
  int ret;
  Elf64_Phdr *phdr;
  if (ehdr->e_phnum == 0)
    return -_error(filename, "No program header found", 1);
  if (ehdr->e_phentsize != sizeof(*phdr))
    return -_error(filename, "Unexpected program header size", 1);
  if (fseek(fp, ehdr->e_phoff, SEEK_SET) != 0)
    return -_error(filename, strerror(errno), 1);
  if ((phdr = malloc(sizeof(*phdr) * ehdr->e_phnum)) == NULL)
    return -_error(filename, strerror(errno), 1);
  ret = _do_phdr64(filename, fp, ehdr, phdr, ldpath);
  free(phdr);
  return ret;
}


x86 assembly (clang, -O0):
<_do_ldd64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jne    11a8 <_do_ldd64+0x48>
mov    -0x10(%rbp),%rdi
lea    0xe70(%rip),%rsi
mov    $0x1,%edx
call   1060 <_error@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    12b5 <_do_ldd64+0x155>
mov    -0x20(%rbp),%rax
movslq 0x4(%rax),%rax
cmp    $0x4,%rax
je     11dd <_do_ldd64+0x7d>
mov    -0x10(%rbp),%rdi
lea    0xe53(%rip),%rsi
mov    $0x1,%edx
call   1060 <_error@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    12b5 <_do_ldd64+0x155>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%esi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
cmp    $0x0,%rax
je     1235 <_do_ldd64+0xd5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   1060 <_error@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    12b5 <_do_ldd64+0x155>
mov    -0x20(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    128a <_do_ldd64+0x12a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   1060 <_error@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    12b5 <_do_ldd64+0x155>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1040 <_do_phdr64@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_do_ldd64>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r15
cmpl   $0x0,(%rdx)
je     11af <_do_ldd64+0x4f>
mov    %rdx,%rbx
cmpl   $0x4,0x4(%rdx)
jne    11b8 <_do_ldd64+0x58>
mov    %rcx,%r14
mov    %rsi,%r12
mov    0x8(%rbx),%esi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
call   1070 <fseek@plt>
test   %rax,%rax
je     11d8 <_do_ldd64+0x78>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %r15,%rdi
mov    %rax,%rsi
jmp    11c2 <_do_ldd64+0x62>
lea    0xe4a(%rip),%rsi
jmp    11bf <_do_ldd64+0x5f>
lea    0xe59(%rip),%rsi
mov    %r15,%rdi
mov    $0x1,%edx
call   1060 <_error@plt>
neg    %eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    (%rbx),%edi
shl    $0x2,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1199 <_do_ldd64+0x39>
mov    %rax,%r13
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
mov    %r14,%r8
call   1040 <_do_phdr64@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
jmp    11ce <_do_ldd64+0x6e>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i32, i32 }
@.str = [24 x i8] c"No program header found\00"
@.str.1 = [31 x i8] c"Unexpected program header size\00"
@SEEK_SET = global i32 0
@errno = global i32 0
define i32 @_do_ldd64(i8* %0, i32* %1, %struct.TYPE_4__* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca %struct.TYPE_4__*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32*
store i8* %0, i8** %6
store i32* %1, i32** %7
store %struct.TYPE_4__* %2, %struct.TYPE_4__** %8
store i8* %3, i8** %9
%12 = load %struct.TYPE_4__*, %struct.TYPE_4__** %8
%13 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %4
%17 = load i8*, i8** %6
%18 = call i32 @_error(i8* %17, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 1)
%19 = sub nsw i32 0, %18
store i32 %19, i32* %5
br label %69
20: ; preds = %4
%21 = load %struct.TYPE_4__*, %struct.TYPE_4__** %8
%22 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = sext i32 %23 to i64
%25 = icmp ne i64 %24, 4
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = load i8*, i8** %6
%28 = call i32 @_error(i8* %27, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 1)
%29 = sub nsw i32 0, %28
store i32 %29, i32* %5
br label %69
30: ; preds = %20
%31 = load i32*, i32** %7
%32 = load %struct.TYPE_4__*, %struct.TYPE_4__** %8
%33 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %32, i32 0, i32 2
%34 = load i32, i32* %33
%35 = load i32, i32* @SEEK_SET
%36 = call i64 @fseek(i32* %31, i32 %34, i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %30
%39 = load i8*, i8** %6
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 @_error(i8* %39, i8* %41, i32 1)
%43 = sub nsw i32 0, %42
store i32 %43, i32* %5
br label %69
44: ; preds = %30
%45 = load %struct.TYPE_4__*, %struct.TYPE_4__** %8
%46 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = sext i32 %47 to i64
%49 = mul i64 4, %48
%50 = trunc i64 %49 to i32
%51 = call i32* @malloc(i32 %50)
store i32* %51, i32** %11
%52 = icmp eq i32* %51, null
br i1 %52, label %53, label %59
53: ; preds = %44
%54 = load i8*, i8** %6
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = call i32 @_error(i8* %54, i8* %56, i32 1)
%58 = sub nsw i32 0, %57
store i32 %58, i32* %5
br label %69
59: ; preds = %44
%60 = load i8*, i8** %6
%61 = load i32*, i32** %7
%62 = load %struct.TYPE_4__*, %struct.TYPE_4__** %8
%63 = load i32*, i32** %11
%64 = load i8*, i8** %9
%65 = call i32 @_do_phdr64(i8* %60, i32* %61, %struct.TYPE_4__* %62, i32* %63, i8* %64)
store i32 %65, i32* %10
%66 = load i32*, i32** %11
%67 = call i32 @free(i32* %66)
%68 = load i32, i32* %10
store i32 %68, i32* %5
br label %69
69: ; preds = %59, %53, %38, %26, %16
%70 = load i32, i32* %5
ret i32 %70
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i32, i32 }
@.str = [24 x i8] c"No program header found\00"
@.str.1 = [31 x i8] c"Unexpected program header size\00"
@SEEK_SET = global i32 0
@errno = global i32 0
define i32 @_do_ldd64(i8* %0, i32* %1, %struct.TYPE_4__* %2, i8* %3) {
%5 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 0
%6 = load i32, i32* %5
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %4
%9 = tail call i32 @_error(i8* %0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 1)
%10 = sub nsw i32 0, %9
br label %42
11: ; preds = %4
%12 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 1
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, 4
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = tail call i32 @_error(i8* %0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 1)
%17 = sub nsw i32 0, %16
br label %42
18: ; preds = %11
%19 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 2
%20 = load i32, i32* %19
%21 = load i32, i32* @SEEK_SET
%22 = tail call i64 @fseek(i32* %1, i32 %20, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %18
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 @_error(i8* %0, i8* %26, i32 1)
%28 = sub nsw i32 0, %27
br label %42
29: ; preds = %18
%30 = load i32, i32* %5
%31 = shl i32 %30, 2
%32 = tail call i32* @malloc(i32 %31)
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = load i32, i32* @errno
%36 = tail call i8* @strerror(i32 %35)
%37 = tail call i32 @_error(i8* %0, i8* %36, i32 1)
%38 = sub nsw i32 0, %37
br label %42
39: ; preds = %29
%40 = tail call i32 @_do_phdr64(i8* %0, i32* %1, %struct.TYPE_4__* %2, i32* %32, i8* %3)
%41 = tail call i32 @free(i32* %32)
br label %42
42: ; preds = %39, %34, %24, %15, %8
%43 = phi i32 [ %10, %8 ], [ %17, %15 ], [ %28, %24 ], [ %38, %34 ], [ %40, %39 ]
ret i32 %43
}