#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 */

/* Type definitions */
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ die(char const *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int ld2(char *);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
off_t get_hdrs_offset(int kernelfd, const char *filename) {
  char buffer[1024];
  off_t offset;
  int i;
  if (lseek(kernelfd, 0, SEEK_SET) < 0)
    die("lseek");
  if (read(kernelfd, buffer, 1024) != 1024)
    die(filename);
  if (buffer[40] == 'H' && buffer[41] == 'd' && buffer[42] == 'r' &&
      buffer[43] == 'S') {
    return 40;
  } else {
    offset = ld2(buffer + 32 + 2) << 2;
    offset -= (128 * 4);
    offset += 32;
    if (offset < 0) {
      errno = -EINVAL;
      die("Calculated a negative offset, probably elftoaout generated an "
          "invalid image. Did you use a recent elftoaout ?");
    }
    if (lseek(kernelfd, offset, SEEK_SET) < 0)
      die("lseek");
    if (read(kernelfd, buffer, 1024) != 1024)
      die(filename);
    for (i = 0; i < (128 * 4); i += 4) {
      if (buffer[i + 0] == 'H' && buffer[i + 1] == 'd' &&
          buffer[i + 2] == 'r' && buffer[i + 3] == 'S') {
        return offset + i;
      }
    }
  }
  fprintf(stderr, "Couldn't find headers signature in %s\n", filename);
  exit(1);
}


x86 assembly (clang, -O0):
<get_hdrs_offset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    119b <get_hdrs_offset+0x3b>
lea    0xe6a(%rip),%rdi
call   1070 <die@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
cmp    $0x400,%eax
je     11c3 <get_hdrs_offset+0x63>
mov    -0x10(%rbp),%rdi
call   1070 <die@plt>
movsbl -0x3e8(%rbp),%eax
cmp    $0x48,%eax
jne    120f <get_hdrs_offset+0xaf>
movsbl -0x3e7(%rbp),%eax
cmp    $0x64,%eax
jne    120f <get_hdrs_offset+0xaf>
movsbl -0x3e6(%rbp),%eax
cmp    $0x72,%eax
jne    120f <get_hdrs_offset+0xaf>
movsbl -0x3e5(%rbp),%eax
cmp    $0x53,%eax
jne    120f <get_hdrs_offset+0xaf>
movl   $0x28,-0x4(%rbp)
jmp    13ad <get_hdrs_offset+0x24d>
lea    -0x410(%rbp),%rdi
add    $0x20,%rdi
add    $0x2,%rdi
call   1030 <ld2@plt>
shl    $0x2,%eax
mov    %eax,-0x414(%rbp)
mov    -0x414(%rbp),%eax
sub    $0x200,%eax
mov    %eax,-0x414(%rbp)
mov    -0x414(%rbp),%eax
add    $0x20,%eax
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    1279 <get_hdrs_offset+0x119>
xor    %ecx,%ecx
mov    0x2d66(%rip),%rax
sub    (%rax),%ecx
mov    0x2d6d(%rip),%rax
mov    %ecx,(%rax)
lea    0xd92(%rip),%rdi
call   1070 <die@plt>
mov    -0x8(%rbp),%edi
mov    -0x414(%rbp),%esi
mov    0x2d47(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    12a6 <get_hdrs_offset+0x146>
lea    0xd5f(%rip),%rdi
call   1070 <die@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
cmp    $0x400,%eax
je     12ce <get_hdrs_offset+0x16e>
mov    -0x10(%rbp),%rdi
call   1070 <die@plt>
movl   $0x0,-0x418(%rbp)
cmpl   $0x200,-0x418(%rbp)
jge    1385 <get_hdrs_offset+0x225>
mov    -0x418(%rbp),%eax
add    $0x0,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0x48,%eax
jne    136c <get_hdrs_offset+0x20c>
mov    -0x418(%rbp),%eax
add    $0x1,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0x64,%eax
jne    136c <get_hdrs_offset+0x20c>
mov    -0x418(%rbp),%eax
add    $0x2,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0x72,%eax
jne    136c <get_hdrs_offset+0x20c>
mov    -0x418(%rbp),%eax
add    $0x3,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0x53,%eax
jne    136c <get_hdrs_offset+0x20c>
mov    -0x414(%rbp),%eax
add    -0x418(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13ad <get_hdrs_offset+0x24d>
jmp    1371 <get_hdrs_offset+0x211>
mov    -0x418(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x418(%rbp)
jmp    12d8 <get_hdrs_offset+0x178>
jmp    138a <get_hdrs_offset+0x22a>
mov    0x2c4f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xcd6(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_hdrs_offset>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2e55(%rip),%r12
mov    (%r12),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
jns    1197 <get_hdrs_offset+0x37>
lea    0xe6e(%rip),%rdi
call   1070 <die@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x400,%edx
call   1050 <read@plt>
cmp    $0x400,%eax
je     11b5 <get_hdrs_offset+0x55>
mov    %r14,%rdi
call   1070 <die@plt>
movq   0x28(%rsp),%xmm0
movd   %xmm0,%eax
mov    $0x28,%ebp
cmp    $0x53726448,%eax
je     128b <get_hdrs_offset+0x12b>
lea    0x22(%rsp),%rdi
call   1030 <ld2@plt>
lea    -0x1e0(,%rax,4),%r15d
ff
mov    %eax,%ebp
shl    $0x2,%ebp
cmp    $0x1df,%ebp
jg     120e <get_hdrs_offset+0xae>
mov    0x2dd3(%rip),%rax
xor    %ecx,%ecx
sub    (%rax),%ecx
mov    0x2dd8(%rip),%rax
mov    %ecx,(%rax)
lea    0xdfd(%rip),%rdi
call   1070 <die@plt>
mov    (%r12),%edx
mov    %ebx,%edi
mov    %r15d,%esi
call   1040 <lseek@plt>
test   %rax,%rax
jns    122d <get_hdrs_offset+0xcd>
lea    0xdd8(%rip),%rdi
call   1070 <die@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x400,%edx
call   1050 <read@plt>
cmp    $0x400,%eax
je     124b <get_hdrs_offset+0xeb>
mov    %r14,%rdi
call   1070 <die@plt>
add    $0xfffffe20,%ebp
mov    $0xfffffffffffffffc,%rax
jmp    126f <get_hdrs_offset+0x10f>
nopw   0x0(%rax,%rax,1)
add    $0x4,%ebp
add    $0x4,%rax
cmp    $0x1fc,%rax
jae    129d <get_hdrs_offset+0x13d>
cmpb   $0x48,0x4(%rsp,%rax,1)
jne    1260 <get_hdrs_offset+0x100>
cmpb   $0x64,0x5(%rsp,%rax,1)
jne    1260 <get_hdrs_offset+0x100>
cmpb   $0x72,0x6(%rsp,%rax,1)
jne    1260 <get_hdrs_offset+0x100>
cmpb   $0x53,0x7(%rsp,%rax,1)
jne    1260 <get_hdrs_offset+0x100>
mov    %ebp,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [6 x i8] c"lseek\00"
@EINVAL = global i32 0
@errno = global i32 0
@.str.1 = [110 x i8] c"Calculated a negative offset, probably elftoaout generated an invalid image. Did you use a recent elftoaout ?\00"
@stderr = global i32 0
@.str.2 = [39 x i8] c"Couldn't find headers signature in %s\0A\00"
define i32 @get_hdrs_offset(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i32, i32* %4
%10 = load i32, i32* @SEEK_SET
%11 = call i64 @lseek(i32 %9, i32 0, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i32, i32* %4
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 1024)
%19 = icmp ne i32 %18, 1024
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i8*, i8** %5
%22 = call i32 @die(i8* %21)
br label %23
23: ; preds = %20, %15
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 40
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 72
br i1 %27, label %28, label %44
28: ; preds = %23
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 41
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 100
br i1 %32, label %33, label %44
33: ; preds = %28
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 42
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 114
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 43
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 83
br i1 %42, label %43, label %44
43: ; preds = %38
store i32 40, i32* %3
br label %126
44: ; preds = %38, %33, %28, %23
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%46 = getelementptr inbounds i8, i8* %45, i64 32
%47 = getelementptr inbounds i8, i8* %46, i64 2
%48 = call i32 @ld2(i8* %47)
%49 = shl i32 %48, 2
store i32 %49, i32* %7
%50 = load i32, i32* %7
%51 = sub nsw i32 %50, 512
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = add nsw i32 %52, 32
store i32 %53, i32* %7
%54 = load i32, i32* %7
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %44
%57 = load i32, i32* @EINVAL
%58 = sub nsw i32 0, %57
store i32 %58, i32* @errno
%59 = call i32 @die(i8* getelementptr inbounds ([110 x i8], [110 x i8]* @.str.1, i64 0, i64 0))
br label %60
60: ; preds = %56, %44
%61 = load i32, i32* %4
%62 = load i32, i32* %7
%63 = load i32, i32* @SEEK_SET
%64 = call i64 @lseek(i32 %61, i32 %62, i32 %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %60
%67 = call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %68
68: ; preds = %66, %60
%69 = load i32, i32* %4
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%71 = call i32 @read(i32 %69, i8* %70, i32 1024)
%72 = icmp ne i32 %71, 1024
br i1 %72, label %73, label %76
73: ; preds = %68
%74 = load i8*, i8** %5
%75 = call i32 @die(i8* %74)
br label %76
76: ; preds = %73, %68
store i32 0, i32* %8
br label %77
77: ; preds = %117, %76
%78 = load i32, i32* %8
%79 = icmp slt i32 %78, 512
br i1 %79, label %80, label %120
80: ; preds = %77
%81 = load i32, i32* %8
%82 = add nsw i32 %81, 0
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %83
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
%87 = icmp eq i32 %86, 72
br i1 %87, label %88, label %116
88: ; preds = %80
%89 = load i32, i32* %8
%90 = add nsw i32 %89, 1
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %91
%93 = load i8, i8* %92
%94 = sext i8 %93 to i32
%95 = icmp eq i32 %94, 100
br i1 %95, label %96, label %116
96: ; preds = %88
%97 = load i32, i32* %8
%98 = add nsw i32 %97, 2
%99 = sext i32 %98 to i64
%100 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %99
%101 = load i8, i8* %100
%102 = sext i8 %101 to i32
%103 = icmp eq i32 %102, 114
br i1 %103, label %104, label %116
104: ; preds = %96
%105 = load i32, i32* %8
%106 = add nsw i32 %105, 3
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %107
%109 = load i8, i8* %108
%110 = sext i8 %109 to i32
%111 = icmp eq i32 %110, 83
br i1 %111, label %112, label %116
112: ; preds = %104
%113 = load i32, i32* %7
%114 = load i32, i32* %8
%115 = add nsw i32 %113, %114
store i32 %115, i32* %3
br label %126
116: ; preds = %104, %96, %88, %80
br label %117
117: ; preds = %116
%118 = load i32, i32* %8
%119 = add nsw i32 %118, 4
store i32 %119, i32* %8
br label %77
120: ; preds = %77
br label %121
121: ; preds = %120
%122 = load i32, i32* @stderr
%123 = load i8*, i8** %5
%124 = call i32 @fprintf(i32 %122, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %123)
%125 = call i32 @exit(i32 1)
unreachable
126: ; preds = %112, %43
%127 = load i32, i32* %3
ret i32 %127
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [6 x i8] c"lseek\00"
@EINVAL = global i32 0
@errno = global i32 0
@.str.1 = [110 x i8] c"Calculated a negative offset, probably elftoaout generated an invalid image. Did you use a recent elftoaout ?\00"
@stderr = global i32 0
@.str.2 = [39 x i8] c"Couldn't find headers signature in %s\0A\00"
define i32 @get_hdrs_offset(i32 %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @SEEK_SET
%6 = tail call i64 @lseek(i32 %0, i32 0, i32 %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = call i32 @read(i32 %0, i8* %4, i32 1024)
%12 = icmp eq i32 %11, 1024
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @die(i8* %1)
br label %15
15: ; preds = %13, %10
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 40
%17 = bitcast i8* %16 to <4 x i8>*
%18 = load <4 x i8>, <4 x i8>* %17
%19 = freeze <4 x i8> %18
%20 = bitcast <4 x i8> %19 to i32
%21 = icmp eq i32 %20, 1400005704
br i1 %21, label %74, label %22
22: ; preds = %15
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 34
%24 = call i32 @ld2(i8* %23)
%25 = shl i32 %24, 2
%26 = add nsw i32 %25, -480
%27 = icmp slt i32 %25, 480
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i32, i32* @EINVAL
%30 = sub nsw i32 0, %29
store i32 %30, i32* @errno
%31 = call i32 @die(i8* getelementptr inbounds ([110 x i8], [110 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %28, %22
%33 = load i32, i32* @SEEK_SET
%34 = call i64 @lseek(i32 %0, i32 %26, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
%39 = call i32 @read(i32 %0, i8* %4, i32 1024)
%40 = icmp eq i32 %39, 1024
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = call i32 @die(i8* %1)
br label %43
43: ; preds = %41, %38
br label %44
44: ; preds = %43, %67
%45 = phi i64 [ %68, %67 ], [ 0, %43 ]
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = icmp eq i8 %47, 72
br i1 %48, label %49, label %67
49: ; preds = %44
%50 = or i64 %45, 1
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %50
%52 = load i8, i8* %51
%53 = icmp eq i8 %52, 100
br i1 %53, label %54, label %67
54: ; preds = %49
%55 = or i64 %45, 2
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = icmp eq i8 %57, 114
br i1 %58, label %59, label %67
59: ; preds = %54
%60 = or i64 %45, 3
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %60
%62 = load i8, i8* %61
%63 = icmp eq i8 %62, 83
br i1 %63, label %64, label %67
64: ; preds = %59
%65 = trunc i64 %45 to i32
%66 = add nsw i32 %26, %65
br label %74
67: ; preds = %44, %49, %54, %59
%68 = add nuw nsw i64 %45, 4
%69 = icmp ult i64 %45, 508
br i1 %69, label %44, label %70
70: ; preds = %67
%71 = load i32, i32* @stderr
%72 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %15, %64
%75 = phi i32 [ %66, %64 ], [ 40, %15 ]
ret i32 %75
}