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

/* Variables and functions */
int O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(unsigned int);
int open(char const *, int);
int read(int, char *, int);
char *realloc(char *, unsigned int);
int /*<<< orphan*/ stderr;
int pgheader_read_raw(char **raw_header, const char *infile,
                      unsigned int *size) {
  int fin;
  char buf[16];
  unsigned int nbheader;
  nbheader = 2048;
  *raw_header = malloc(nbheader);
  if (!(*raw_header)) {
    return 1;
  }
  (*size) = 0;
  fin = open(infile, O_RDONLY | 0x0);
  if (fin == -1) {
    free(*raw_header);
    *raw_header = NULL;
    return 1;
  }
  *size = 0;
  while (1) {
    int j, r;
    int zero;
    r = read(fin, buf, 16);
    if (r == 0) {
      close(fin);
      return 0;
    }
    if (r < 16) {
      free(*raw_header);
      *raw_header = NULL;
      close(fin);
      return 2;
    }
    zero = 1;
    for (j = 0; j < 8; j++) {
      if (buf[j] != 0) {
        zero = 0;
      }
    }
    if (!zero) {
      close(fin);
      return 0;
    }
    if ((*size) >= nbheader) {
      nbheader *= 2;
      *raw_header = realloc(*raw_header, nbheader);
    }
    for (j = 8; j < 16; j++) {
      (*raw_header)[(*size) + j - 8] = buf[j];
    }
    (*size) += 8;
  }
  fprintf(stderr, "pgheader_read_raw(): internal error\n");
  close(fin);
  return 2;
}


x86 assembly (clang, -O0):
<pgheader_read_raw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x800,-0x44(%rbp)
mov    -0x44(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a7 <pgheader_read_raw+0x47>
movl   $0x1,-0x4(%rbp)
jmp    1349 <pgheader_read_raw+0x1e9>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11f6 <pgheader_read_raw+0x96>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1349 <pgheader_read_raw+0x1e9>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x24(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x10,%edx
call   1050 <read@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jne    1232 <pgheader_read_raw+0xd2>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1349 <pgheader_read_raw+0x1e9>
cmpl   $0x10,-0x4c(%rbp)
jge    1267 <pgheader_read_raw+0x107>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x2,-0x4(%rbp)
jmp    1349 <pgheader_read_raw+0x1e9>
movl   $0x1,-0x50(%rbp)
movl   $0x0,-0x48(%rbp)
cmpl   $0x8,-0x48(%rbp)
jge    12ab <pgheader_read_raw+0x14b>
movslq -0x48(%rbp),%rax
movsbl -0x40(%rbp,%rax,1),%eax
cmp    $0x0,%eax
je     1298 <pgheader_read_raw+0x138>
movl   $0x0,-0x50(%rbp)
jmp    129d <pgheader_read_raw+0x13d>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1275 <pgheader_read_raw+0x115>
cmpl   $0x0,-0x50(%rbp)
jne    12c9 <pgheader_read_raw+0x169>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1349 <pgheader_read_raw+0x1e9>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
cmp    -0x44(%rbp),%eax
jb     12fa <pgheader_read_raw+0x19a>
mov    -0x44(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x44(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x8,-0x48(%rbp)
cmpl   $0x10,-0x48(%rbp)
jge    1339 <pgheader_read_raw+0x1d9>
movslq -0x48(%rbp),%rax
mov    -0x40(%rbp,%rax,1),%dl
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
add    -0x48(%rbp),%ecx
sub    $0x8,%ecx
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1301 <pgheader_read_raw+0x1a1>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x8,%ecx
mov    %ecx,(%rax)
jmp    1200 <pgheader_read_raw+0xa0>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgheader_read_raw>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,%r14
mov    $0x800,%edi
call   1060 <malloc@plt>
mov    %rax,(%r14)
mov    $0x1,%r12d
test   %rax,%rax
je     12e3 <pgheader_read_raw+0x183>
movl   $0x0,(%rbx)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12b5 <pgheader_read_raw+0x155>
mov    %eax,%r15d
movl   $0x0,(%rbx)
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1050 <read@plt>
test   %eax,%eax
je     12a8 <pgheader_read_raw+0x148>
mov    $0x800,%ebp
mov    %rsp,%r12
jmp    1282 <pgheader_read_raw+0x122>
xchg   %ax,%ax
add    %ebp,%ebp
mov    (%r14),%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
mov    %rax,(%r14)
mov    (%rbx),%ecx
movzbl 0x8(%rsp),%edx
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
movzbl 0x9(%rsp),%eax
mov    (%r14),%rcx
mov    (%rbx),%edx
add    $0x1,%edx
mov    %al,(%rcx,%rdx,1)
movzbl 0xa(%rsp),%eax
mov    (%r14),%rcx
mov    (%rbx),%edx
add    $0x2,%edx
mov    %al,(%rcx,%rdx,1)
movzbl 0xb(%rsp),%eax
mov    (%r14),%rcx
mov    (%rbx),%edx
add    $0x3,%edx
mov    %al,(%rcx,%rdx,1)
movzbl 0xc(%rsp),%eax
mov    (%r14),%rcx
mov    (%rbx),%edx
add    $0x4,%edx
mov    %al,(%rcx,%rdx,1)
movzbl 0xd(%rsp),%eax
mov    (%r14),%rcx
mov    (%rbx),%edx
add    $0x5,%edx
mov    %al,(%rcx,%rdx,1)
movzbl 0xe(%rsp),%eax
mov    (%r14),%rcx
mov    (%rbx),%edx
add    $0x6,%edx
mov    %al,(%rcx,%rdx,1)
movzbl 0xf(%rsp),%eax
mov    (%r14),%rcx
mov    (%rbx),%edx
add    $0x7,%edx
mov    %al,(%rcx,%rdx,1)
addl   $0x8,(%rbx)
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x10,%edx
call   1050 <read@plt>
test   %eax,%eax
je     12a8 <pgheader_read_raw+0x148>
cmp    $0x10,%eax
jl     12c6 <pgheader_read_raw+0x166>
movq   (%rsp),%xmm0
movq   %xmm0,%rax
test   %rax,%rax
jne    12a8 <pgheader_read_raw+0x148>
mov    (%rbx),%ecx
cmp    %ebp,%ecx
jae    11e0 <pgheader_read_raw+0x80>
mov    (%r14),%rax
jmp    11f1 <pgheader_read_raw+0x91>
mov    %r15d,%edi
call   1040 <close@plt>
xor    %r12d,%r12d
jmp    12e3 <pgheader_read_raw+0x183>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
jmp    12e3 <pgheader_read_raw+0x183>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
mov    %r15d,%edi
call   1040 <close@plt>
mov    $0x2,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
define i32 @pgheader_read_raw(i8** %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca [16 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32 2048, i32* %10
%14 = load i32, i32* %10
%15 = call i8* @malloc(i32 %14)
%16 = load i8**, i8*** %5
store i8* %15, i8** %16
%17 = load i8**, i8*** %5
%18 = load i8*, i8** %17
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %3
store i32 1, i32* %4
br label %115
21: ; preds = %3
%22 = load i32*, i32** %7
store i32 0, i32* %22
%23 = load i8*, i8** %6
%24 = load i32, i32* @O_RDONLY
%25 = or i32 %24, 0
%26 = call i32 @open(i8* %23, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %21
%30 = load i8**, i8*** %5
%31 = load i8*, i8** %30
%32 = call i32 @free(i8* %31)
%33 = load i8**, i8*** %5
store i8* null, i8** %33
store i32 1, i32* %4
br label %115
34: ; preds = %21
%35 = load i32*, i32** %7
store i32 0, i32* %35
br label %36
36: ; preds = %34, %111
%37 = load i32, i32* %8
%38 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 16)
store i32 %39, i32* %12
%40 = load i32, i32* %12
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = load i32, i32* %8
%44 = call i32 @close(i32 %43)
store i32 0, i32* %4
br label %115
45: ; preds = %36
%46 = load i32, i32* %12
%47 = icmp slt i32 %46, 16
br i1 %47, label %48, label %55
48: ; preds = %45
%49 = load i8**, i8*** %5
%50 = load i8*, i8** %49
%51 = call i32 @free(i8* %50)
%52 = load i8**, i8*** %5
store i8* null, i8** %52
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
store i32 2, i32* %4
br label %115
55: ; preds = %45
store i32 1, i32* %13
store i32 0, i32* %11
br label %56
56: ; preds = %68, %55
%57 = load i32, i32* %11
%58 = icmp slt i32 %57, 8
br i1 %58, label %59, label %71
59: ; preds = %56
%60 = load i32, i32* %11
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 %61
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %59
store i32 0, i32* %13
br label %67
67: ; preds = %66, %59
br label %68
68: ; preds = %67
%69 = load i32, i32* %11
%70 = add nsw i32 %69, 1
store i32 %70, i32* %11
br label %56
71: ; preds = %56
%72 = load i32, i32* %13
%73 = icmp ne i32 %72, 0
br i1 %73, label %77, label %74
74: ; preds = %71
%75 = load i32, i32* %8
%76 = call i32 @close(i32 %75)
store i32 0, i32* %4
br label %115
77: ; preds = %71
%78 = load i32*, i32** %7
%79 = load i32, i32* %78
%80 = load i32, i32* %10
%81 = icmp uge i32 %79, %80
br i1 %81, label %82, label %90
82: ; preds = %77
%83 = load i32, i32* %10
%84 = mul i32 %83, 2
store i32 %84, i32* %10
%85 = load i8**, i8*** %5
%86 = load i8*, i8** %85
%87 = load i32, i32* %10
%88 = call i8* @realloc(i8* %86, i32 %87)
%89 = load i8**, i8*** %5
store i8* %88, i8** %89
br label %90
90: ; preds = %82, %77
store i32 8, i32* %11
br label %91
91: ; preds = %108, %90
%92 = load i32, i32* %11
%93 = icmp slt i32 %92, 16
br i1 %93, label %94, label %111
94: ; preds = %91
%95 = load i32, i32* %11
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 %96
%98 = load i8, i8* %97
%99 = load i8**, i8*** %5
%100 = load i8*, i8** %99
%101 = load i32*, i32** %7
%102 = load i32, i32* %101
%103 = load i32, i32* %11
%104 = add i32 %102, %103
%105 = sub i32 %104, 8
%106 = zext i32 %105 to i64
%107 = getelementptr inbounds i8, i8* %100, i64 %106
store i8 %98, i8* %107
br label %108
108: ; preds = %94
%109 = load i32, i32* %11
%110 = add nsw i32 %109, 1
store i32 %110, i32* %11
br label %91
111: ; preds = %91
%112 = load i32*, i32** %7
%113 = load i32, i32* %112
%114 = add i32 %113, 8
store i32 %114, i32* %112
br label %36
115: ; preds = %74, %48, %42, %29, %20
%116 = load i32, i32* %4
ret i32 %116
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
define i32 @pgheader_read_raw(i8** nocapture %0, i8* nocapture %1, i32* nocapture %2) {
%4 = alloca [16 x i8]
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%6 = tail call dereferenceable_or_null(2048) i8* @malloc(i32 2048)
store i8* %6, i8** %0
%7 = icmp eq i8* %6, null
br i1 %7, label %108, label %8
8: ; preds = %3
store i32 0, i32* %2
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %1, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %25, label %12
12: ; preds = %8
store i32 0, i32* %2
%13 = call i32 @read(i32 %10, i8* %5, i32 16)
%14 = icmp eq i32 %13, 0
br i1 %14, label %28, label %15
15: ; preds = %12
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 8
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 9
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 10
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 11
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 12
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 13
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 14
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 15
%24 = bitcast [16 x i8]* %4 to <8 x i8>*
br label %30
25: ; preds = %8
%26 = load i8*, i8** %0
%27 = tail call i32 @free(i8* %26)
store i8* null, i8** %0
br label %108
28: ; preds = %55, %12
%29 = tail call i32 @close(i32 %10)
br label %108
30: ; preds = %15, %55
%31 = phi i32 [ %106, %55 ], [ %13, %15 ]
%32 = phi i32 [ %58, %55 ], [ 2048, %15 ]
%33 = icmp slt i32 %31, 16
br i1 %33, label %39, label %34
34: ; preds = %30
%35 = load <8 x i8>, <8 x i8>* %24
%36 = freeze <8 x i8> %35
%37 = bitcast <8 x i8> %36 to i64
%38 = icmp eq i64 %37, 0
br i1 %38, label %45, label %43
39: ; preds = %30
%40 = load i8*, i8** %0
%41 = tail call i32 @free(i8* %40)
store i8* null, i8** %0
%42 = tail call i32 @close(i32 %10)
br label %108
43: ; preds = %34
%44 = tail call i32 @close(i32 %10)
br label %108
45: ; preds = %34
%46 = load i32, i32* %2
%47 = icmp ult i32 %46, %32
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = load i8*, i8** %0
br label %55
50: ; preds = %45
%51 = shl i32 %32, 1
%52 = load i8*, i8** %0
%53 = tail call i8* @realloc(i8* %52, i32 %51)
store i8* %53, i8** %0
%54 = load i32, i32* %2
br label %55
55: ; preds = %48, %50
%56 = phi i32 [ %54, %50 ], [ %46, %48 ]
%57 = phi i8* [ %53, %50 ], [ %49, %48 ]
%58 = phi i32 [ %51, %50 ], [ %32, %48 ]
%59 = load i8, i8* %16
%60 = zext i32 %56 to i64
%61 = getelementptr inbounds i8, i8* %57, i64 %60
store i8 %59, i8* %61
%62 = load i8, i8* %17
%63 = load i8*, i8** %0
%64 = load i32, i32* %2
%65 = add i32 %64, 1
%66 = zext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %63, i64 %66
store i8 %62, i8* %67
%68 = load i8, i8* %18
%69 = load i8*, i8** %0
%70 = load i32, i32* %2
%71 = add i32 %70, 2
%72 = zext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %69, i64 %72
store i8 %68, i8* %73
%74 = load i8, i8* %19
%75 = load i8*, i8** %0
%76 = load i32, i32* %2
%77 = add i32 %76, 3
%78 = zext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %75, i64 %78
store i8 %74, i8* %79
%80 = load i8, i8* %20
%81 = load i8*, i8** %0
%82 = load i32, i32* %2
%83 = add i32 %82, 4
%84 = zext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %81, i64 %84
store i8 %80, i8* %85
%86 = load i8, i8* %21
%87 = load i8*, i8** %0
%88 = load i32, i32* %2
%89 = add i32 %88, 5
%90 = zext i32 %89 to i64
%91 = getelementptr inbounds i8, i8* %87, i64 %90
store i8 %86, i8* %91
%92 = load i8, i8* %22
%93 = load i8*, i8** %0
%94 = load i32, i32* %2
%95 = add i32 %94, 6
%96 = zext i32 %95 to i64
%97 = getelementptr inbounds i8, i8* %93, i64 %96
store i8 %92, i8* %97
%98 = load i8, i8* %23
%99 = load i8*, i8** %0
%100 = load i32, i32* %2
%101 = add i32 %100, 7
%102 = zext i32 %101 to i64
%103 = getelementptr inbounds i8, i8* %99, i64 %102
store i8 %98, i8* %103
%104 = load i32, i32* %2
%105 = add i32 %104, 8
store i32 %105, i32* %2
%106 = call i32 @read(i32 %10, i8* %5, i32 16)
%107 = icmp eq i32 %106, 0
br i1 %107, label %28, label %30
108: ; preds = %43, %39, %28, %3, %25
%109 = phi i32 [ 1, %25 ], [ 1, %3 ], [ 0, %43 ], [ 2, %39 ], [ 0, %28 ]
ret i32 %109
}