#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_3__ TYPE_1__;

/* Type definitions */
typedef scalar_t__ off_t;
struct TYPE_3__ {
  unsigned int e_phnum;
  unsigned int e_phentsize;
  scalar_t__ e_phoff;
};
typedef int /*<<< orphan*/ Elf64_Phdr;
typedef TYPE_1__ Elf64_Ehdr;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
int read(int, int /*<<< orphan*/ *, int);
int getSegment(int fd, Elf64_Ehdr *hdr, unsigned int idx, Elf64_Phdr *segment) {
  int status = 0;
  if (idx < hdr->e_phnum) {
    off_t seekOffset = hdr->e_phoff + idx * hdr->e_phentsize;
    if (lseek(fd, seekOffset, SEEK_SET) >= 0) {
      if (read(fd, segment, sizeof(*segment)) == sizeof(*segment))
        status = 1;
    }
  }
  return status;
}


x86 assembly (clang, -O0):
<getSegment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jae    11b5 <getSegment+0x95>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rdx
imul   0x4(%rdx),%ecx
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0x0,%rax
jl     11b0 <getSegment+0x90>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cltq
cmp    $0x4,%rax
jne    11ab <getSegment+0x8b>
movl   $0x1,-0x24(%rbp)
jmp    11b0 <getSegment+0x90>
jmp    11b5 <getSegment+0x95>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getSegment>:
push   %rbp
push   %r14
push   %rbx
xor    %ebx,%ebx
cmp    %edx,(%rsi)
jbe    1166 <getSegment+0x46>
mov    %rcx,%r14
mov    %edi,%ebp
imul   0x4(%rsi),%edx
add    0x8(%rsi),%rdx
mov    0x2ea2(%rip),%rax
mov    (%rax),%eax
mov    %rdx,%rsi
mov    %eax,%edx
call   1030 <lseek@plt>
test   %rax,%rax
js     1166 <getSegment+0x46>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <read@plt>
xor    %ebx,%ebx
cmp    $0x4,%eax
sete   %bl
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32, i64 }
@SEEK_SET = global i32 0
define i32 @getSegment(i32 %0, %struct.TYPE_3__* %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca %struct.TYPE_3__*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i64
store i32 %0, i32* %5
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %6
store i32 %2, i32* %7
store i32* %3, i32** %8
store i32 0, i32* %9
%11 = load i32, i32* %7
%12 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%13 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = icmp ult i32 %11, %14
br i1 %15, label %16, label %41
16: ; preds = %4
%17 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%18 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %17, i32 0, i32 2
%19 = load i64, i64* %18
%20 = load i32, i32* %7
%21 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%22 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = mul i32 %20, %23
%25 = zext i32 %24 to i64
%26 = add nsw i64 %19, %25
store i64 %26, i64* %10
%27 = load i32, i32* %5
%28 = load i64, i64* %10
%29 = load i32, i32* @SEEK_SET
%30 = call i64 @lseek(i32 %27, i64 %28, i32 %29)
%31 = icmp sge i64 %30, 0
br i1 %31, label %32, label %40
32: ; preds = %16
%33 = load i32, i32* %5
%34 = load i32*, i32** %8
%35 = call i32 @read(i32 %33, i32* %34, i32 4)
%36 = sext i32 %35 to i64
%37 = icmp eq i64 %36, 4
br i1 %37, label %38, label %39
38: ; preds = %32
store i32 1, i32* %9
br label %39
39: ; preds = %38, %32
br label %40
40: ; preds = %39, %16
br label %41
41: ; preds = %40, %4
%42 = load i32, i32* %9
ret i32 %42
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32, i64 }
@SEEK_SET = global i32 0
define i32 @getSegment(i32 %0, %struct.TYPE_3__* nocapture %1, i32 %2, i32* nocapture %3) {
%5 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%6 = load i32, i32* %5
%7 = icmp ugt i32 %6, %2
br i1 %7, label %8, label %23
8: ; preds = %4
%9 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 2
%10 = load i64, i64* %9
%11 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%12 = load i32, i32* %11
%13 = mul i32 %12, %2
%14 = zext i32 %13 to i64
%15 = add nsw i64 %10, %14
%16 = load i32, i32* @SEEK_SET
%17 = tail call i64 @lseek(i32 %0, i64 %15, i32 %16)
%18 = icmp sgt i64 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %8
%20 = tail call i32 @read(i32 %0, i32* %3, i32 4)
%21 = icmp eq i32 %20, 4
%22 = zext i1 %21 to i32
br label %23
23: ; preds = %19, %8, %4
%24 = phi i32 [ 0, %4 ], [ 0, %8 ], [ %22, %19 ]
ret i32 %24
}