#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 /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ errno;
int lseek(int, int, int /*<<< orphan*/);
int read(int, char *, int);
int write(int, char *, int);
int patch_ar(int fd) {
  int len;
  char buf[2];
  char size[10];
  if (lseek(fd, 7, SEEK_SET) == -1)
    return -1;
  for (;;) {
    while ((len = read(fd, buf, 1)) == 1)
      if (*buf != '\n')
        break;
    if (len == 0)
      break;
    if (lseek(fd, 15, SEEK_CUR) == -1)
      return -1;
    if (write(fd, "0           ", 12) == -1)
      return -1;
    if (lseek(fd, 20, SEEK_CUR) == -1)
      return -1;
    if (read(fd, size, 10) != 10)
      return -1;
    if (read(fd, buf, 2) != 2)
      return -1;
    if (buf[0] != '`' || buf[1] != '\n') {
      errno = EINVAL;
      return -1;
    }
    if (lseek(fd, atoi(size), SEEK_CUR) == -1)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<patch_ar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
mov    $0x7,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1176 <patch_ar+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <patch_ar+0x1c9>
jmp    117b <patch_ar+0x3b>
jmp    1180 <patch_ar+0x40>
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x1,%eax
jne    11b4 <patch_ar+0x74>
movsbl -0xe(%rbp),%eax
cmp    $0xa,%eax
je     11af <patch_ar+0x6f>
jmp    11b4 <patch_ar+0x74>
jmp    1180 <patch_ar+0x40>
cmpl   $0x0,-0xc(%rbp)
jne    11c3 <patch_ar+0x83>
jmp    1302 <patch_ar+0x1c2>
mov    -0x8(%rbp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    $0xf,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11ee <patch_ar+0xae>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <patch_ar+0x1c9>
mov    -0x8(%rbp),%edi
lea    0xe08(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1217 <patch_ar+0xd7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <patch_ar+0x1c9>
mov    -0x8(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
mov    $0x14,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1242 <patch_ar+0x102>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <patch_ar+0x1c9>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1050 <read@plt>
cmp    $0xa,%eax
je     1268 <patch_ar+0x128>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <patch_ar+0x1c9>
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
cmp    $0x2,%eax
je     128e <patch_ar+0x14e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <patch_ar+0x1c9>
movsbl -0xe(%rbp),%eax
cmp    $0x60,%eax
jne    12a8 <patch_ar+0x168>
movsbl -0xd(%rbp),%eax
cmp    $0xa,%eax
je     12c6 <patch_ar+0x186>
mov    0x2d19(%rip),%rax
mov    (%rax),%ecx
mov    0x2d20(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <patch_ar+0x1c9>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <atoi@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%esi
mov    0x2d07(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    12fd <patch_ar+0x1bd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1309 <patch_ar+0x1c9>
jmp    117b <patch_ar+0x3b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<patch_ar>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
mov    $0x7,%esi
call   1040 <lseek@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     1266 <patch_ar+0x126>
lea    0xc(%rsp),%rbx
mov    0x2e6a(%rip),%r13
lea    0xe7b(%rip),%r15
lea    0xe(%rsp),%r12
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    11ab <patch_ar+0x6b>
cmpb   $0xa,0xc(%rsp)
je     1190 <patch_ar+0x50>
test   %eax,%eax
je     124f <patch_ar+0x10f>
mov    0x0(%r13),%edx
mov    %ebp,%edi
mov    $0xf,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     1266 <patch_ar+0x126>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1266 <patch_ar+0x126>
mov    0x0(%r13),%edx
mov    %ebp,%edi
mov    $0x14,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     1266 <patch_ar+0x126>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0xa,%edx
call   1050 <read@plt>
cmp    $0xa,%eax
jne    1266 <patch_ar+0x126>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1050 <read@plt>
cmp    $0x2,%eax
jne    1266 <patch_ar+0x126>
cmpb   $0x60,0xc(%rsp)
jne    1254 <patch_ar+0x114>
cmpb   $0xa,0xd(%rsp)
jne    1254 <patch_ar+0x114>
mov    %r12,%rdi
call   1060 <atoi@plt>
mov    0x0(%r13),%edx
mov    %ebp,%edi
mov    %eax,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1190 <patch_ar+0x50>
jmp    1266 <patch_ar+0x126>
xor    %r14d,%r14d
jmp    1266 <patch_ar+0x126>
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
mov    0x2d74(%rip),%rcx
mov    %eax,(%rcx)
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@SEEK_CUR = global i32 0
@.str = [13 x i8] c"0 \00"
@EINVAL = global i32 0
@errno = global i32 0
define i32 @patch_ar(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i8]
%6 = alloca [10 x i8]
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = load i32, i32* @SEEK_SET
%9 = call i32 @lseek(i32 %7, i32 7, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %81
12: ; preds = %1
br label %13
13: ; preds = %79, %12
br label %14
14: ; preds = %25, %13
%15 = load i32, i32* %3
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 1)
store i32 %17, i32* %4
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %26
19: ; preds = %14
%20 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 10
br i1 %23, label %24, label %25
24: ; preds = %19
br label %26
25: ; preds = %19
br label %14
26: ; preds = %24, %14
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
br label %80
30: ; preds = %26
%31 = load i32, i32* %3
%32 = load i32, i32* @SEEK_CUR
%33 = call i32 @lseek(i32 %31, i32 15, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 -1, i32* %2
br label %81
36: ; preds = %30
%37 = load i32, i32* %3
%38 = call i32 @write(i32 %37, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 -1, i32* %2
br label %81
41: ; preds = %36
%42 = load i32, i32* %3
%43 = load i32, i32* @SEEK_CUR
%44 = call i32 @lseek(i32 %42, i32 20, i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %47
46: ; preds = %41
store i32 -1, i32* %2
br label %81
47: ; preds = %41
%48 = load i32, i32* %3
%49 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%50 = call i32 @read(i32 %48, i8* %49, i32 10)
%51 = icmp ne i32 %50, 10
br i1 %51, label %52, label %53
52: ; preds = %47
store i32 -1, i32* %2
br label %81
53: ; preds = %47
%54 = load i32, i32* %3
%55 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%56 = call i32 @read(i32 %54, i8* %55, i32 2)
%57 = icmp ne i32 %56, 2
br i1 %57, label %58, label %59
58: ; preds = %53
store i32 -1, i32* %2
br label %81
59: ; preds = %53
%60 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp ne i32 %62, 96
br i1 %63, label %69, label %64
64: ; preds = %59
%65 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
%66 = load i8, i8* %65
%67 = sext i8 %66 to i32
%68 = icmp ne i32 %67, 10
br i1 %68, label %69, label %71
69: ; preds = %64, %59
%70 = load i32, i32* @EINVAL
store i32 %70, i32* @errno
store i32 -1, i32* %2
br label %81
71: ; preds = %64
%72 = load i32, i32* %3
%73 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%74 = call i32 @atoi(i8* %73)
%75 = load i32, i32* @SEEK_CUR
%76 = call i32 @lseek(i32 %72, i32 %74, i32 %75)
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %79
78: ; preds = %71
store i32 -1, i32* %2
br label %81
79: ; preds = %71
br label %13
80: ; preds = %29
store i32 0, i32* %2
br label %81
81: ; preds = %80, %78, %69, %58, %52, %46, %40, %35, %11
%82 = load i32, i32* %2
ret i32 %82
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@SEEK_CUR = global i32 0
@.str = [13 x i8] c"0 \00"
@EINVAL = global i32 0
@errno = global i32 0
define i32 @patch_ar(i32 %0) {
%2 = alloca [2 x i8]
%3 = alloca [10 x i8]
%4 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @lseek(i32 %0, i32 7, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %50, label %9
9: ; preds = %1
%10 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
br label %11
11: ; preds = %17, %9
%12 = call i32 @read(i32 %0, i8* %4, i32 1)
%13 = icmp ne i32 %12, 1
%14 = load i8, i8* %4
%15 = icmp ne i8 %14, 10
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %18, label %17
17: ; preds = %11, %45
br label %11
18: ; preds = %11
%19 = icmp eq i32 %12, 0
br i1 %19, label %50, label %20
20: ; preds = %18
%21 = load i32, i32* @SEEK_CUR
%22 = tail call i32 @lseek(i32 %0, i32 15, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %50, label %24
24: ; preds = %20
%25 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %50, label %27
27: ; preds = %24
%28 = load i32, i32* @SEEK_CUR
%29 = tail call i32 @lseek(i32 %0, i32 20, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %50, label %31
31: ; preds = %27
%32 = call i32 @read(i32 %0, i8* %5, i32 10)
%33 = icmp eq i32 %32, 10
br i1 %33, label %34, label %50
34: ; preds = %31
%35 = call i32 @read(i32 %0, i8* %4, i32 2)
%36 = icmp eq i32 %35, 2
br i1 %36, label %37, label %50
37: ; preds = %34
%38 = load i8, i8* %4
%39 = icmp ne i8 %38, 96
%40 = load i8, i8* %10
%41 = icmp ne i8 %40, 10
%42 = select i1 %39, i1 true, i1 %41
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = load i32, i32* @EINVAL
store i32 %44, i32* @errno
br label %50
45: ; preds = %37
%46 = call i32 @atoi(i8* %5)
%47 = load i32, i32* @SEEK_CUR
%48 = tail call i32 @lseek(i32 %0, i32 %46, i32 %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %17
50: ; preds = %18, %45, %34, %31, %27, %24, %20, %1, %43
%51 = phi i32 [ -1, %43 ], [ -1, %1 ], [ 0, %18 ], [ -1, %45 ], [ -1, %34 ], [ -1, %31 ], [ -1, %27 ], [ -1, %24 ], [ -1, %20 ]
ret i32 %51
}