#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_7__ TYPE_2__;
typedef struct TYPE_6__ TYPE_1__;

/* Type definitions */
struct TYPE_7__ {
  int e_phnum;
  int /*<<< orphan*/ e_phoff;
};
struct TYPE_6__ {
  scalar_t__ p_type;
  scalar_t__ p_filesz;
};
typedef TYPE_1__ Elf32_Phdr;
typedef TYPE_2__ Elf32_Ehdr;

/* Variables and functions */
scalar_t__ PT_DYNAMIC;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, TYPE_1__ *, int);
int /*<<< orphan*/ stderr;
int elf_find_dynamic_section(int fd, Elf32_Ehdr *ehdr, Elf32_Phdr *phdr) {
  int i;
  if (lseek(fd, ehdr->e_phoff, SEEK_SET) == -1) {
    perror("positioning for sections");
    return 1;
  }
  for (i = 0; i < ehdr->e_phnum; i++) {
    if (read(fd, phdr, sizeof(*phdr)) != sizeof(*phdr)) {
      perror("reading section header");
      return 1;
    }
    if (phdr->p_type == PT_DYNAMIC)
      break;
  }
  if (i == ehdr->e_phnum) {
    fprintf(stderr, "No dynamic section found.\n");
    return 2;
  }
  if (0 == phdr->p_filesz) {
    fprintf(stderr, "Length of dynamic section is zero.\n");
    return 3;
  }
  return 0;
}


x86 assembly (clang, -O0):
<elf_find_dynamic_section>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0xffffffff,%eax
jne    118c <elf_find_dynamic_section+0x4c>
lea    0xe85(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    126e <elf_find_dynamic_section+0x12e>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jge    1206 <elf_find_dynamic_section+0xc6>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1040 <read@plt>
cltq
cmp    $0x10,%rax
je     11d7 <elf_find_dynamic_section+0x97>
lea    0xe53(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    126e <elf_find_dynamic_section+0x12e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2de3(%rip),%rcx
cmp    (%rcx),%rax
jne    11f3 <elf_find_dynamic_section+0xb3>
jmp    1206 <elf_find_dynamic_section+0xc6>
jmp    11f8 <elf_find_dynamic_section+0xb8>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1193 <elf_find_dynamic_section+0x53>
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jne    1236 <elf_find_dynamic_section+0xf6>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    126e <elf_find_dynamic_section+0x12e>
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    0x8(%rcx),%rax
jne    1267 <elf_find_dynamic_section+0x127>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x3,-0x4(%rbp)
jmp    126e <elf_find_dynamic_section+0x12e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<elf_find_dynamic_section>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x4(%rsi),%esi
mov    0x2e7d(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0xffffffff,%eax
je     11f4 <elf_find_dynamic_section+0xb4>
mov    (%r14),%eax
xor    %ebp,%ebp
test   %eax,%eax
jle    11a8 <elf_find_dynamic_section+0x68>
mov    0x2e4d(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1040 <read@plt>
cmp    $0x10,%eax
jne    11fd <elf_find_dynamic_section+0xbd>
mov    (%rbx),%rax
cmp    (%r12),%rax
je     11c8 <elf_find_dynamic_section+0x88>
add    $0x1,%ebp
mov    (%r14),%eax
cmp    %eax,%ebp
jl     1180 <elf_find_dynamic_section+0x40>
cmp    %eax,%ebp
jne    11cf <elf_find_dynamic_section+0x8f>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%eax
jmp    120e <elf_find_dynamic_section+0xce>
mov    (%r14),%eax
cmp    %eax,%ebp
je     11ac <elf_find_dynamic_section+0x6c>
xor    %eax,%eax
cmpq   $0x0,0x8(%rbx)
jne    120e <elf_find_dynamic_section+0xce>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x3,%eax
jmp    120e <elf_find_dynamic_section+0xce>
lea    0xe05(%rip),%rdi
jmp    1204 <elf_find_dynamic_section+0xc4>
lea    0xe15(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_7__ = type { i32, i32 }
%struct.TYPE_6__ = type { i64, i64 }
@SEEK_SET = global i32 0
@.str = [25 x i8] c"positioning for sections\00"
@.str.1 = [23 x i8] c"reading section header\00"
@PT_DYNAMIC = global i64 0
@stderr = global i32 0
@.str.2 = [27 x i8] c"No dynamic section found.\0A\00"
@.str.3 = [36 x i8] c"Length of dynamic section is zero.\0A\00"
define i32 @elf_find_dynamic_section(i32 %0, %struct.TYPE_7__* %1, %struct.TYPE_6__* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.TYPE_7__*
%7 = alloca %struct.TYPE_6__*
%8 = alloca i32
store i32 %0, i32* %5
store %struct.TYPE_7__* %1, %struct.TYPE_7__** %6
store %struct.TYPE_6__* %2, %struct.TYPE_6__** %7
%9 = load i32, i32* %5
%10 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%11 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %10, i32 0, i32 1
%12 = load i32, i32* %11
%13 = load i32, i32* @SEEK_SET
%14 = call i32 @lseek(i32 %9, i32 %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %62
18: ; preds = %3
store i32 0, i32* %8
br label %19
19: ; preds = %41, %18
%20 = load i32, i32* %8
%21 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%22 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = icmp slt i32 %20, %23
br i1 %24, label %25, label %44
25: ; preds = %19
%26 = load i32, i32* %5
%27 = load %struct.TYPE_6__*, %struct.TYPE_6__** %7
%28 = call i32 @read(i32 %26, %struct.TYPE_6__* %27, i32 16)
%29 = sext i32 %28 to i64
%30 = icmp ne i64 %29, 16
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %62
33: ; preds = %25
%34 = load %struct.TYPE_6__*, %struct.TYPE_6__** %7
%35 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %34, i32 0, i32 0
%36 = load i64, i64* %35
%37 = load i64, i64* @PT_DYNAMIC
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %33
br label %44
40: ; preds = %33
br label %41
41: ; preds = %40
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %19
44: ; preds = %39, %19
%45 = load i32, i32* %8
%46 = load %struct.TYPE_7__*, %struct.TYPE_7__** %6
%47 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = icmp eq i32 %45, %48
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %4
br label %62
53: ; preds = %44
%54 = load %struct.TYPE_6__*, %struct.TYPE_6__** %7
%55 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %54, i32 0, i32 1
%56 = load i64, i64* %55
%57 = icmp eq i64 0, %56
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = load i32, i32* @stderr
%60 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
store i32 3, i32* %4
br label %62
61: ; preds = %53
store i32 0, i32* %4
br label %62
62: ; preds = %61, %58, %50, %31, %16
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
%struct.TYPE_7__ = type { i32, i32 }
%struct.TYPE_6__ = type { i64, i64 }
@SEEK_SET = global i32 0
@.str = [25 x i8] c"positioning for sections\00"
@.str.1 = [23 x i8] c"reading section header\00"
@PT_DYNAMIC = global i64 0
@stderr = global i32 0
@.str.2 = [27 x i8] c"No dynamic section found.\0A\00"
@.str.3 = [36 x i8] c"Length of dynamic section is zero.\0A\00"
define i32 @elf_find_dynamic_section(i32 %0, %struct.TYPE_7__* nocapture %1, %struct.TYPE_6__* nocapture %2) {
%4 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %1, i64 0, i32 1
%5 = load i32, i32* %4
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @lseek(i32 %0, i32 %5, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %15, label %9
9: ; preds = %3
%10 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %1, i64 0, i32 0
%11 = load i32, i32* %10
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %33
13: ; preds = %9
%14 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %2, i64 0, i32 0
br label %17
15: ; preds = %3
%16 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %47
17: ; preds = %13, %29
%18 = phi i32 [ 0, %13 ], [ %30, %29 ]
%19 = tail call i32 @read(i32 %0, %struct.TYPE_6__* %2, i32 16)
%20 = icmp eq i32 %19, 16
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %47
23: ; preds = %17
%24 = load i64, i64* %14
%25 = load i64, i64* @PT_DYNAMIC
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = load i32, i32* %10
br label %33
29: ; preds = %23
%30 = add nuw nsw i32 %18, 1
%31 = load i32, i32* %10
%32 = icmp slt i32 %30, %31
br i1 %32, label %17, label %33
33: ; preds = %29, %27, %9
%34 = phi i32 [ %11, %9 ], [ %28, %27 ], [ %31, %29 ]
%35 = phi i32 [ 0, %9 ], [ %18, %27 ], [ %30, %29 ]
%36 = icmp eq i32 %35, %34
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = tail call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %47
40: ; preds = %33
%41 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %2, i64 0, i32 1
%42 = load i64, i64* %41
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = tail call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %40, %44, %37, %21, %15
%48 = phi i32 [ 1, %15 ], [ 1, %21 ], [ 2, %37 ], [ 3, %44 ], [ 0, %40 ]
ret i32 %48
}