#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*/ SEEK_SET;
int /*<<< orphan*/ die(char const *);
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 (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    0x2e5c(%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    1380 <get_hdrs_offset+0x220>
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)
mov    -0x8(%rbp),%edi
mov    -0x414(%rbp),%esi
mov    0x2d7c(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    1279 <get_hdrs_offset+0x119>
lea    0xd8c(%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     12a1 <get_hdrs_offset+0x141>
mov    -0x10(%rbp),%rdi
call   1070 <die@plt>
movl   $0x0,-0x418(%rbp)
cmpl   $0x200,-0x418(%rbp)
jge    1358 <get_hdrs_offset+0x1f8>
mov    -0x418(%rbp),%eax
add    $0x0,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0x48,%eax
jne    133f <get_hdrs_offset+0x1df>
mov    -0x418(%rbp),%eax
add    $0x1,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0x64,%eax
jne    133f <get_hdrs_offset+0x1df>
mov    -0x418(%rbp),%eax
add    $0x2,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0x72,%eax
jne    133f <get_hdrs_offset+0x1df>
mov    -0x418(%rbp),%eax
add    $0x3,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0x53,%eax
jne    133f <get_hdrs_offset+0x1df>
mov    -0x414(%rbp),%eax
add    -0x418(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1380 <get_hdrs_offset+0x220>
jmp    1344 <get_hdrs_offset+0x1e4>
mov    -0x418(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x418(%rbp)
jmp    12ab <get_hdrs_offset+0x14b>
jmp    135d <get_hdrs_offset+0x1fd>
mov    0x2c7c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xc95(%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   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e5f(%rip),%r15
mov    (%r15),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
jns    1194 <get_hdrs_offset+0x34>
lea    0xe71(%rip),%rdi
call   1070 <die@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1050 <read@plt>
cmp    $0x400,%eax
je     11b2 <get_hdrs_offset+0x52>
mov    %r14,%rdi
call   1070 <die@plt>
movq   0x28(%rsp),%xmm0
movd   %xmm0,%eax
mov    $0x28,%ebx
cmp    $0x53726448,%eax
je     125b <get_hdrs_offset+0xfb>
lea    0x22(%rsp),%rdi
call   1030 <ld2@plt>
mov    %eax,%ebx
lea    -0x1e0(,%rbx,4),%esi
mov    (%r15),%edx
mov    %ebp,%edi
call   1040 <lseek@plt>
test   %rax,%rax
jns    11fa <get_hdrs_offset+0x9a>
lea    0xe0b(%rip),%rdi
call   1070 <die@plt>
shl    $0x2,%ebx
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1050 <read@plt>
cmp    $0x400,%eax
je     121b <get_hdrs_offset+0xbb>
mov    %r14,%rdi
call   1070 <die@plt>
add    $0xfffffe20,%ebx
mov    $0xfffffffffffffffc,%rax
jmp    123f <get_hdrs_offset+0xdf>
nopw   0x0(%rax,%rax,1)
add    $0x4,%ebx
add    $0x4,%rax
cmp    $0x1fc,%rax
jae    126b <get_hdrs_offset+0x10b>
cmpb   $0x48,0x4(%rsp,%rax,1)
jne    1230 <get_hdrs_offset+0xd0>
cmpb   $0x64,0x5(%rsp,%rax,1)
jne    1230 <get_hdrs_offset+0xd0>
cmpb   $0x72,0x6(%rsp,%rax,1)
jne    1230 <get_hdrs_offset+0xd0>
cmpb   $0x53,0x7(%rsp,%rax,1)
jne    1230 <get_hdrs_offset+0xd0>
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    0xd8b(%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"
@stderr = global i32 0
@.str.1 = [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 %119
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* %4
%55 = load i32, i32* %7
%56 = load i32, i32* @SEEK_SET
%57 = call i64 @lseek(i32 %54, i32 %55, i32 %56)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %44
%60 = call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %61
61: ; preds = %59, %44
%62 = load i32, i32* %4
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%64 = call i32 @read(i32 %62, i8* %63, i32 1024)
%65 = icmp ne i32 %64, 1024
br i1 %65, label %66, label %69
66: ; preds = %61
%67 = load i8*, i8** %5
%68 = call i32 @die(i8* %67)
br label %69
69: ; preds = %66, %61
store i32 0, i32* %8
br label %70
70: ; preds = %110, %69
%71 = load i32, i32* %8
%72 = icmp slt i32 %71, 512
br i1 %72, label %73, label %113
73: ; preds = %70
%74 = load i32, i32* %8
%75 = add nsw i32 %74, 0
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %76
%78 = load i8, i8* %77
%79 = sext i8 %78 to i32
%80 = icmp eq i32 %79, 72
br i1 %80, label %81, label %109
81: ; preds = %73
%82 = load i32, i32* %8
%83 = add nsw i32 %82, 1
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %84
%86 = load i8, i8* %85
%87 = sext i8 %86 to i32
%88 = icmp eq i32 %87, 100
br i1 %88, label %89, label %109
89: ; preds = %81
%90 = load i32, i32* %8
%91 = add nsw i32 %90, 2
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %92
%94 = load i8, i8* %93
%95 = sext i8 %94 to i32
%96 = icmp eq i32 %95, 114
br i1 %96, label %97, label %109
97: ; preds = %89
%98 = load i32, i32* %8
%99 = add nsw i32 %98, 3
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %100
%102 = load i8, i8* %101
%103 = sext i8 %102 to i32
%104 = icmp eq i32 %103, 83
br i1 %104, label %105, label %109
105: ; preds = %97
%106 = load i32, i32* %7
%107 = load i32, i32* %8
%108 = add nsw i32 %106, %107
store i32 %108, i32* %3
br label %119
109: ; preds = %97, %89, %81, %73
br label %110
110: ; preds = %109
%111 = load i32, i32* %8
%112 = add nsw i32 %111, 4
store i32 %112, i32* %8
br label %70
113: ; preds = %70
br label %114
114: ; preds = %113
%115 = load i32, i32* @stderr
%116 = load i8*, i8** %5
%117 = call i32 @fprintf(i32 %115, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %116)
%118 = call i32 @exit(i32 1)
unreachable
119: ; preds = %105, %43
%120 = load i32, i32* %3
ret i32 %120
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [6 x i8] c"lseek\00"
@stderr = global i32 0
@.str.1 = [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 %68, 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 = load i32, i32* @SEEK_SET
%28 = call i64 @lseek(i32 %0, i32 %26, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @die(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %30, %22
%33 = call i32 @read(i32 %0, i8* %4, i32 1024)
%34 = icmp eq i32 %33, 1024
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = call i32 @die(i8* %1)
br label %37
37: ; preds = %35, %32
br label %38
38: ; preds = %37, %61
%39 = phi i64 [ %62, %61 ], [ 0, %37 ]
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = icmp eq i8 %41, 72
br i1 %42, label %43, label %61
43: ; preds = %38
%44 = or i64 %39, 1
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %44
%46 = load i8, i8* %45
%47 = icmp eq i8 %46, 100
br i1 %47, label %48, label %61
48: ; preds = %43
%49 = or i64 %39, 2
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = icmp eq i8 %51, 114
br i1 %52, label %53, label %61
53: ; preds = %48
%54 = or i64 %39, 3
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %54
%56 = load i8, i8* %55
%57 = icmp eq i8 %56, 83
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = trunc i64 %39 to i32
%60 = add nsw i32 %26, %59
br label %68
61: ; preds = %38, %43, %48, %53
%62 = add nuw nsw i64 %39, 4
%63 = icmp ult i64 %39, 508
br i1 %63, label %38, label %64
64: ; preds = %61
%65 = load i32, i32* @stderr
%66 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %15, %58
%69 = phi i32 [ %60, %58 ], [ 40, %15 ]
ret i32 %69
}