#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_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int open(char const *, int, ...);
int pgheader_detect(char const *);
int read(int, char *, int);
int /*<<< orphan*/ strcmp(char const *, char const *);
int write(int, char *, int);
int pgheader_delete(const char *infile, const char *outfile) {
  int fin, fout;
  char buf[16];
  int ret;
  if ((ret = pgheader_detect(infile))) {
    return ret;
  }
  if (!strcmp(infile, outfile)) {
    return 4;
  }
  fin = open(infile, O_RDONLY | 0x0);
  if (fin == -1) {
    return 1;
  }
  fout = open(outfile, O_WRONLY | O_CREAT | O_TRUNC | 0x0, S_IRUSR | S_IWUSR);
  if (fout == -1) {
    close(fin);
    return 1;
  }
  while (1) {
    int j, r;
    int zero;
    r = read(fin, buf, 16);
    if (r < 16) {
      close(fin);
      close(fout);
      return 2;
    }
    zero = 1;
    for (j = 0; j < 8; j++) {
      if (buf[j] != 0) {
        zero = 0;
      }
    }
    if (!zero) {
      break;
    }
  }
  if (write(fout, buf, 16) != 16) {
    close(fin);
    close(fout);
    return 1;
  }
  while ((ret = read(fin, buf, 16)) == 16) {
    if (write(fout, buf, ret) != 16) {
      close(fin);
      close(fout);
      return 1;
    }
  };
  close(fin);
  close(fout);
  if (0 < ret && ret < 16) {
    return 2;
  } else if (ret == -1) {
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<pgheader_delete>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <pgheader_detect@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
je     1190 <pgheader_delete+0x30>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13b9 <pgheader_delete+0x259>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11b2 <pgheader_delete+0x52>
movl   $0x4,-0x4(%rbp)
jmp    13b9 <pgheader_delete+0x259>
mov    -0x10(%rbp),%rdi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11e2 <pgheader_delete+0x82>
movl   $0x1,-0x4(%rbp)
jmp    13b9 <pgheader_delete+0x259>
mov    -0x18(%rbp),%rdi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
mov    0x2e02(%rip),%rax
or     (%rax),%esi
mov    0x2df1(%rip),%rax
or     (%rax),%esi
or     $0x0,%esi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
mov    0x2dbc(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    123e <pgheader_delete+0xde>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b9 <pgheader_delete+0x259>
jmp    1243 <pgheader_delete+0xe3>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1050 <read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x10,-0x3c(%rbp)
jge    127d <pgheader_delete+0x11d>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0x2,-0x4(%rbp)
jmp    13b9 <pgheader_delete+0x259>
movl   $0x1,-0x40(%rbp)
movl   $0x0,-0x38(%rbp)
cmpl   $0x8,-0x38(%rbp)
jge    12c1 <pgheader_delete+0x161>
movslq -0x38(%rbp),%rax
movsbl -0x30(%rbp,%rax,1),%eax
cmp    $0x0,%eax
je     12ae <pgheader_delete+0x14e>
movl   $0x0,-0x40(%rbp)
jmp    12b3 <pgheader_delete+0x153>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    128b <pgheader_delete+0x12b>
cmpl   $0x0,-0x40(%rbp)
jne    12d0 <pgheader_delete+0x170>
jmp    12d5 <pgheader_delete+0x175>
jmp    1243 <pgheader_delete+0xe3>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
cmp    $0x10,%eax
je     130b <pgheader_delete+0x1ab>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b9 <pgheader_delete+0x259>
jmp    1310 <pgheader_delete+0x1b0>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1050 <read@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x10,%eax
jne    1366 <pgheader_delete+0x206>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <write@plt>
cmp    $0x10,%eax
je     1361 <pgheader_delete+0x201>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b9 <pgheader_delete+0x259>
jmp    1310 <pgheader_delete+0x1b0>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
cmp    -0x34(%rbp),%eax
jge    1397 <pgheader_delete+0x237>
cmpl   $0x10,-0x34(%rbp)
jge    1397 <pgheader_delete+0x237>
movl   $0x2,-0x4(%rbp)
jmp    13b9 <pgheader_delete+0x259>
cmpl   $0xffffffff,-0x34(%rbp)
jne    13ad <pgheader_delete+0x24d>
movl   $0x1,-0x4(%rbp)
jmp    13b9 <pgheader_delete+0x259>
jmp    13b2 <pgheader_delete+0x252>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgheader_delete>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
call   1080 <pgheader_detect@plt>
mov    %eax,%ebx
test   %eax,%eax
je     118c <pgheader_delete+0x2c>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     128f <pgheader_delete+0x12f>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1070 <open@plt>
mov    $0x1,%ebx
cmp    $0xffffffff,%eax
je     117d <pgheader_delete+0x1d>
mov    %eax,%r12d
mov    0x2dea(%rip),%rax
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e18(%rip),%rax
or     (%rax),%esi
mov    0x2ddf(%rip),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1299 <pgheader_delete+0x139>
mov    %eax,%r14d
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x10,%edx
call   1050 <read@plt>
cmp    $0x10,%eax
jl     12a6 <pgheader_delete+0x146>
movq   (%rsp),%xmm0
movq   %xmm0,%rax
test   %rax,%rax
je     1210 <pgheader_delete+0xb0>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x10,%edx
call   1030 <write@plt>
cmp    $0x10,%eax
jne    127a <pgheader_delete+0x11a>
mov    %rsp,%r15
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1050 <read@plt>
cmp    $0x10,%eax
jne    12c0 <pgheader_delete+0x160>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1030 <write@plt>
cmp    $0x10,%eax
je     1250 <pgheader_delete+0xf0>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r14d,%edi
call   1040 <close@plt>
jmp    117d <pgheader_delete+0x1d>
mov    $0x4,%ebx
jmp    117d <pgheader_delete+0x1d>
mov    %r12d,%edi
call   1040 <close@plt>
jmp    117d <pgheader_delete+0x1d>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    $0x2,%ebx
jmp    117d <pgheader_delete+0x1d>
mov    %eax,%ebp
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r14d,%edi
call   1040 <close@plt>
lea    -0x1(%rbp),%eax
mov    $0x2,%ebx
cmp    $0xf,%eax
jb     117d <pgheader_delete+0x1d>
xor    %ebx,%ebx
cmp    $0xffffffff,%ebp
sete   %bl
jmp    117d <pgheader_delete+0x1d>

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @pgheader_delete(i8* %0, i8* nocapture %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @pgheader_detect(i8* %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %63
7: ; preds = %2
%8 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%9 = icmp eq i32 %8, 0
br i1 %9, label %63, label %10
10: ; preds = %7
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %63, label %14
14: ; preds = %10
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %16, %15
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRUSR
%21 = load i32, i32* @S_IWUSR
%22 = or i32 %21, %20
%23 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %19, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %27, label %25
25: ; preds = %14
%26 = bitcast [16 x i8]* %3 to <8 x i8>*
br label %29
27: ; preds = %14
%28 = tail call i32 @close(i32 %12)
br label %63
29: ; preds = %25, %32
%30 = call i32 @read(i32 %12, i8* %4, i32 16)
%31 = icmp slt i32 %30, 16
br i1 %31, label %37, label %32
32: ; preds = %29
%33 = load <8 x i8>, <8 x i8>* %26
%34 = freeze <8 x i8> %33
%35 = bitcast <8 x i8> %34 to i64
%36 = icmp eq i64 %35, 0
br i1 %36, label %29, label %40
37: ; preds = %29
%38 = tail call i32 @close(i32 %12)
%39 = tail call i32 @close(i32 %23)
br label %63
40: ; preds = %32
%41 = call i32 @write(i32 %23, i8* %4, i32 16)
%42 = icmp eq i32 %41, 16
br i1 %42, label %46, label %43
43: ; preds = %40
%44 = tail call i32 @close(i32 %12)
%45 = tail call i32 @close(i32 %23)
br label %63
46: ; preds = %40, %49
%47 = call i32 @read(i32 %12, i8* %4, i32 16)
%48 = icmp eq i32 %47, 16
br i1 %48, label %49, label %55
49: ; preds = %46
%50 = call i32 @write(i32 %23, i8* %4, i32 16)
%51 = icmp eq i32 %50, 16
br i1 %51, label %46, label %52
52: ; preds = %49
%53 = tail call i32 @close(i32 %12)
%54 = tail call i32 @close(i32 %23)
br label %63
55: ; preds = %46
%56 = tail call i32 @close(i32 %12)
%57 = tail call i32 @close(i32 %23)
%58 = add i32 %47, -1
%59 = icmp ult i32 %58, 15
br i1 %59, label %63, label %60
60: ; preds = %55
%61 = icmp eq i32 %47, -1
%62 = zext i1 %61 to i32
br label %63
63: ; preds = %37, %60, %55, %10, %7, %2, %52, %43, %27
%64 = phi i32 [ 1, %27 ], [ 1, %43 ], [ 1, %52 ], [ %5, %2 ], [ 4, %7 ], [ 1, %10 ], [ 2, %55 ], [ %62, %60 ], [ 2, %37 ]
ret i32 %64
}