#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*/ File_Close(int);
int File_Open(char const *);
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int read(int, int *, int);
int write(int, void *, int);
int File_UpgradeWrite(const char *FileName, int index, int data_len,
                      void *buf) {
  int fd;
  int i, len;
  int ret;
  if ((fd = File_Open(FileName)) < 0)
    return -1;
  i = len = 0;
  lseek(fd, 0, SEEK_SET);
  while (1) {
    if ((ret = read(fd, &i, 4)) <= 0) {
      break;
    }
    if (read(fd, &len, 4) <= 0)
      break;
    lseek(fd, len, SEEK_CUR);
    if (index < i) {
      lseek(fd, (-8 - len), SEEK_CUR);
      if (write(fd, &index, 4) != 4) {
        File_Close(fd);
        return -1;
      }
      if (write(fd, &data_len, 4) != 4) {
        File_Close(fd);
        return -1;
      }
      if (write(fd, buf, data_len) != data_len) {
        File_Close(fd);
        return -1;
      }
      break;
    }
  }
  if (read(fd, &i, 4) == 0) {
    if (write(fd, &index, 4) != 4) {
      File_Close(fd);
      return -1;
    }
    if (write(fd, &data_len, 4) != 4) {
      File_Close(fd);
      return -1;
    }
    if (write(fd, buf, data_len) != data_len) {
      File_Close(fd);
      return -1;
    }
  }
  File_Close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<File_UpgradeWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <File_Open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1187 <File_UpgradeWrite+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <File_UpgradeWrite+0x21a>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
jg     11ca <File_UpgradeWrite+0x7a>
jmp    12b4 <File_UpgradeWrite+0x164>
mov    -0x24(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x0,%eax
jg     11e9 <File_UpgradeWrite+0x99>
jmp    12b4 <File_UpgradeWrite+0x164>
mov    -0x24(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x14(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    12af <File_UpgradeWrite+0x15f>
mov    -0x24(%rbp),%edi
mov    $0xfffffff8,%esi
sub    -0x2c(%rbp),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x24(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     1250 <File_UpgradeWrite+0x100>
mov    -0x24(%rbp),%edi
call   1070 <File_Close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <File_UpgradeWrite+0x21a>
mov    -0x24(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     127e <File_UpgradeWrite+0x12e>
mov    -0x24(%rbp),%edi
call   1070 <File_Close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <File_UpgradeWrite+0x21a>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
cmp    -0x18(%rbp),%eax
je     12aa <File_UpgradeWrite+0x15a>
mov    -0x24(%rbp),%edi
call   1070 <File_Close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <File_UpgradeWrite+0x21a>
jmp    12b4 <File_UpgradeWrite+0x164>
jmp    11a8 <File_UpgradeWrite+0x58>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x0,%eax
jne    135b <File_UpgradeWrite+0x20b>
mov    -0x24(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     12fc <File_UpgradeWrite+0x1ac>
mov    -0x24(%rbp),%edi
call   1070 <File_Close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <File_UpgradeWrite+0x21a>
mov    -0x24(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     132a <File_UpgradeWrite+0x1da>
mov    -0x24(%rbp),%edi
call   1070 <File_Close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <File_UpgradeWrite+0x21a>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
cmp    -0x18(%rbp),%eax
je     1356 <File_UpgradeWrite+0x206>
mov    -0x24(%rbp),%edi
call   1070 <File_Close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <File_UpgradeWrite+0x21a>
jmp    135b <File_UpgradeWrite+0x20b>
mov    -0x24(%rbp),%edi
call   1070 <File_Close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<File_UpgradeWrite>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r15
mov    %edx,%r14d
mov    %esi,%ebp
mov    %esi,0x1c(%rsp)
mov    %edx,0x18(%rsp)
call   1060 <File_Open@plt>
test   %eax,%eax
js     127f <File_UpgradeWrite+0x12f>
mov    %eax,%ebx
mov    %r15,0x20(%rsp)
mov    %r14d,0x10(%rsp)
movl   $0x0,0x14(%rsp)
movl   $0x0,0xc(%rsp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
lea    0xc(%rsp),%r12
lea    0x14(%rsp),%r13
mov    0x2e2e(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1266 <File_UpgradeWrite+0x116>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1266 <File_UpgradeWrite+0x116>
mov    0x14(%rsp),%r14d
mov    (%r15),%edx
mov    %ebx,%edi
mov    %r14d,%esi
call   1040 <lseek@plt>
cmp    %ebp,0xc(%rsp)
jle    11c0 <File_UpgradeWrite+0x70>
mov    $0xfffffff8,%esi
sub    %r14d,%esi
mov    (%r15),%edx
mov    %ebx,%edi
call   1040 <lseek@plt>
lea    0x1c(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
mov    $0xffffffff,%ebp
cmp    $0x4,%eax
jne    12d0 <File_UpgradeWrite+0x180>
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jne    12d0 <File_UpgradeWrite+0x180>
mov    %ebx,%edi
mov    0x20(%rsp),%rsi
mov    0x10(%rsp),%r14d
mov    %r14d,%edx
call   1030 <write@plt>
cmp    %r14d,%eax
jne    12d0 <File_UpgradeWrite+0x180>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1050 <read@plt>
test   %eax,%eax
je     1286 <File_UpgradeWrite+0x136>
xor    %ebp,%ebp
jmp    12d0 <File_UpgradeWrite+0x180>
mov    $0xffffffff,%ebp
jmp    12d7 <File_UpgradeWrite+0x187>
lea    0x1c(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
mov    $0xffffffff,%ebp
cmp    $0x4,%eax
jne    12d0 <File_UpgradeWrite+0x180>
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jne    12d0 <File_UpgradeWrite+0x180>
mov    %ebx,%edi
mov    0x20(%rsp),%rsi
mov    0x10(%rsp),%r14d
mov    %r14d,%edx
call   1030 <write@plt>
cmp    %r14d,%eax
je     127b <File_UpgradeWrite+0x12b>
mov    %ebx,%edi
call   1070 <File_Close@plt>
mov    %ebp,%eax
add    $0x28,%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
define i32 @File_UpgradeWrite(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%14 = load i8*, i8** %6
%15 = call i32 @File_Open(i8* %14)
store i32 %15, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %107
18: ; preds = %4
store i32 0, i32* %12
store i32 0, i32* %11
%19 = load i32, i32* %10
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @lseek(i32 %19, i32 0, i32 %20)
br label %22
22: ; preds = %18, %72
%23 = load i32, i32* %10
%24 = call i32 @read(i32 %23, i32* %11, i32 4)
store i32 %24, i32* %13
%25 = icmp sle i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
br label %73
27: ; preds = %22
%28 = load i32, i32* %10
%29 = call i32 @read(i32 %28, i32* %12, i32 4)
%30 = icmp sle i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
br label %73
32: ; preds = %27
%33 = load i32, i32* %10
%34 = load i32, i32* %12
%35 = load i32, i32* @SEEK_CUR
%36 = call i32 @lseek(i32 %33, i32 %34, i32 %35)
%37 = load i32, i32* %7
%38 = load i32, i32* %11
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %72
40: ; preds = %32
%41 = load i32, i32* %10
%42 = load i32, i32* %12
%43 = sub nsw i32 -8, %42
%44 = load i32, i32* @SEEK_CUR
%45 = call i32 @lseek(i32 %41, i32 %43, i32 %44)
%46 = load i32, i32* %10
%47 = bitcast i32* %7 to i8*
%48 = call i32 @write(i32 %46, i8* %47, i32 4)
%49 = icmp ne i32 %48, 4
br i1 %49, label %50, label %53
50: ; preds = %40
%51 = load i32, i32* %10
%52 = call i32 @File_Close(i32 %51)
store i32 -1, i32* %5
br label %107
53: ; preds = %40
%54 = load i32, i32* %10
%55 = bitcast i32* %8 to i8*
%56 = call i32 @write(i32 %54, i8* %55, i32 4)
%57 = icmp ne i32 %56, 4
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = load i32, i32* %10
%60 = call i32 @File_Close(i32 %59)
store i32 -1, i32* %5
br label %107
61: ; preds = %53
%62 = load i32, i32* %10
%63 = load i8*, i8** %9
%64 = load i32, i32* %8
%65 = call i32 @write(i32 %62, i8* %63, i32 %64)
%66 = load i32, i32* %8
%67 = icmp ne i32 %65, %66
br i1 %67, label %68, label %71
68: ; preds = %61
%69 = load i32, i32* %10
%70 = call i32 @File_Close(i32 %69)
store i32 -1, i32* %5
br label %107
71: ; preds = %61
br label %73
72: ; preds = %32
br label %22
73: ; preds = %71, %31, %26
%74 = load i32, i32* %10
%75 = call i32 @read(i32 %74, i32* %11, i32 4)
%76 = icmp eq i32 %75, 0
br i1 %76, label %77, label %104
77: ; preds = %73
%78 = load i32, i32* %10
%79 = bitcast i32* %7 to i8*
%80 = call i32 @write(i32 %78, i8* %79, i32 4)
%81 = icmp ne i32 %80, 4
br i1 %81, label %82, label %85
82: ; preds = %77
%83 = load i32, i32* %10
%84 = call i32 @File_Close(i32 %83)
store i32 -1, i32* %5
br label %107
85: ; preds = %77
%86 = load i32, i32* %10
%87 = bitcast i32* %8 to i8*
%88 = call i32 @write(i32 %86, i8* %87, i32 4)
%89 = icmp ne i32 %88, 4
br i1 %89, label %90, label %93
90: ; preds = %85
%91 = load i32, i32* %10
%92 = call i32 @File_Close(i32 %91)
store i32 -1, i32* %5
br label %107
93: ; preds = %85
%94 = load i32, i32* %10
%95 = load i8*, i8** %9
%96 = load i32, i32* %8
%97 = call i32 @write(i32 %94, i8* %95, i32 %96)
%98 = load i32, i32* %8
%99 = icmp ne i32 %97, %98
br i1 %99, label %100, label %103
100: ; preds = %93
%101 = load i32, i32* %10
%102 = call i32 @File_Close(i32 %101)
store i32 -1, i32* %5
br label %107
103: ; preds = %93
br label %104
104: ; preds = %103, %73
%105 = load i32, i32* %10
%106 = call i32 @File_Close(i32 %105)
store i32 0, i32* %5
br label %107
107: ; preds = %104, %100, %90, %82, %68, %58, %50, %17
%108 = load i32, i32* %5
ret i32 %108
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@SEEK_CUR = global i32 0
define i32 @File_UpgradeWrite(i8* %0, i32 %1, i32 %2, i8* nocapture %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = bitcast i32* %7 to i8*
%10 = bitcast i32* %8 to i8*
%11 = tail call i32 @File_Open(i8* %0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %60, label %13
13: ; preds = %4
store i32 0, i32* %8
store i32 0, i32* %7
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @lseek(i32 %11, i32 0, i32 %14)
br label %16
16: ; preds = %22, %13
%17 = call i32 @read(i32 %11, i32* %7, i32 4)
%18 = icmp slt i32 %17, 1
br i1 %18, label %42, label %19
19: ; preds = %16
%20 = call i32 @read(i32 %11, i32* %8, i32 4)
%21 = icmp slt i32 %20, 1
br i1 %21, label %42, label %22
22: ; preds = %19
%23 = load i32, i32* %8
%24 = load i32, i32* @SEEK_CUR
%25 = tail call i32 @lseek(i32 %11, i32 %23, i32 %24)
%26 = load i32, i32* %7
%27 = icmp sgt i32 %26, %1
br i1 %27, label %28, label %16
28: ; preds = %22
%29 = sub nsw i32 -8, %23
%30 = load i32, i32* @SEEK_CUR
%31 = tail call i32 @lseek(i32 %11, i32 %29, i32 %30)
%32 = bitcast i32* %5 to i8*
%33 = call i32 @write(i32 %11, i8* %32, i32 4)
%34 = icmp eq i32 %33, 4
br i1 %34, label %35, label %57
35: ; preds = %28
%36 = bitcast i32* %6 to i8*
%37 = call i32 @write(i32 %11, i8* %36, i32 4)
%38 = icmp eq i32 %37, 4
br i1 %38, label %39, label %57
39: ; preds = %35
%40 = tail call i32 @write(i32 %11, i8* %3, i32 %2)
%41 = icmp eq i32 %40, %2
br i1 %41, label %42, label %57
42: ; preds = %19, %16, %39
%43 = call i32 @read(i32 %11, i32* %7, i32 4)
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %56
45: ; preds = %42
%46 = bitcast i32* %5 to i8*
%47 = call i32 @write(i32 %11, i8* %46, i32 4)
%48 = icmp eq i32 %47, 4
br i1 %48, label %49, label %57
49: ; preds = %45
%50 = bitcast i32* %6 to i8*
%51 = call i32 @write(i32 %11, i8* %50, i32 4)
%52 = icmp eq i32 %51, 4
br i1 %52, label %53, label %57
53: ; preds = %49
%54 = tail call i32 @write(i32 %11, i8* %3, i32 %2)
%55 = icmp eq i32 %54, %2
br i1 %55, label %56, label %57
56: ; preds = %53, %42
br label %57
57: ; preds = %53, %49, %45, %39, %35, %28, %56
%58 = phi i32 [ 0, %56 ], [ -1, %28 ], [ -1, %35 ], [ -1, %39 ], [ -1, %45 ], [ -1, %49 ], [ -1, %53 ]
%59 = tail call i32 @File_Close(i32 %11)
br label %60
60: ; preds = %57, %4
%61 = phi i32 [ -1, %4 ], [ %58, %57 ]
ret i32 %61
}