#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_WRONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: ./a.out /path/to/file\n");
    return 1;
  }
  int fd;
  if ((fd = open(argv[1], O_CREAT | O_WRONLY)) == -1) {
    perror("/tmp/hole.file open failed");
    return 1;
  }
  char *a = "abcd";
  if (write(fd, a, 4) != 4) {
    perror("write fail 1");
    close(fd);
    return 1;
  }
  if (lseek(fd, 50, SEEK_CUR) == -1) {
    perror("lseek fail");
    close(fd);
    return 1;
  }
  if (write(fd, a, 4) != 4) {
    perror("write fail 1");
    close(fd);
    return 1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a1 <main+0x41>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b1 <main+0x151>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11e4 <main+0x84>
lea    0xe4b(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b1 <main+0x151>
lea    0xe4e(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     1229 <main+0xc9>
lea    0xe2e(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b1 <main+0x151>
mov    -0x14(%rbp),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
mov    $0x32,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1268 <main+0x108>
lea    0xdfc(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b1 <main+0x151>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     12a2 <main+0x142>
lea    0xdb5(%rip),%rdi
call   1080 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b1 <main+0x151>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    11dd <main+0x7d>
mov    0x8(%rsi),%rdi
mov    0x2e87(%rip),%rax
mov    0x2e48(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1202 <main+0xa2>
mov    %eax,%ebx
lea    0xeaa(%rip),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jne    11f9 <main+0x99>
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x32,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     1210 <main+0xb0>
lea    0xe78(%rip),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jne    11f9 <main+0x99>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
ret
lea    0xe3e(%rip),%rdi
jmp    1217 <main+0xb7>
lea    0xe15(%rip),%rdi
call   1080 <perror@plt>
jmp    11f2 <main+0x92>
lea    0xe34(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11f2 <main+0x92>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"Usage: ./a.out /path/to/file\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [27 x i8] c"/tmp/hole.file open failed\00"
@.str.2 = [5 x i8] c"abcd\00"
@.str.3 = [13 x i8] c"write fail 1\00"
@SEEK_CUR = global i32 0
@.str.4 = [11 x i8] c"lseek fail\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %54
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_CREAT
%18 = load i32, i32* @O_WRONLY
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %16, i32 %19)
store i32 %20, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %13
%23 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %54
24: ; preds = %13
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %7
%25 = load i32, i32* %6
%26 = load i8*, i8** %7
%27 = call i32 @write(i32 %25, i8* %26, i32 4)
%28 = icmp ne i32 %27, 4
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
store i32 1, i32* %3
br label %54
33: ; preds = %24
%34 = load i32, i32* %6
%35 = load i32, i32* @SEEK_CUR
%36 = call i32 @lseek(i32 %34, i32 50, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%40 = load i32, i32* %6
%41 = call i32 @close(i32 %40)
store i32 1, i32* %3
br label %54
42: ; preds = %33
%43 = load i32, i32* %6
%44 = load i8*, i8** %7
%45 = call i32 @write(i32 %43, i8* %44, i32 4)
%46 = icmp ne i32 %45, 4
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
store i32 1, i32* %3
br label %54
51: ; preds = %42
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
store i32 0, i32* %3
br label %54
54: ; preds = %51, %47, %38, %29, %22, %10
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"Usage: ./a.out /path/to/file\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [27 x i8] c"/tmp/hole.file open failed\00"
@.str.2 = [5 x i8] c"abcd\00"
@.str.3 = [13 x i8] c"write fail 1\00"
@SEEK_CUR = global i32 0
@.str.4 = [11 x i8] c"lseek fail\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %38
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_WRONLY
%12 = or i32 %11, %10
%13 = tail call i32 @open(i8* %9, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %38
17: ; preds = %7
%18 = tail call i32 @write(i32 %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%19 = icmp eq i32 %18, 4
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%22 = tail call i32 @close(i32 %13)
br label %38
23: ; preds = %17
%24 = load i32, i32* @SEEK_CUR
%25 = tail call i32 @lseek(i32 %13, i32 50, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%29 = tail call i32 @close(i32 %13)
br label %38
30: ; preds = %23
%31 = tail call i32 @write(i32 %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%32 = icmp eq i32 %31, 4
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%35 = tail call i32 @close(i32 %13)
br label %38
36: ; preds = %30
%37 = tail call i32 @close(i32 %13)
br label %38
38: ; preds = %15, %36, %33, %27, %20, %4
%39 = phi i32 [ 1, %4 ], [ 1, %15 ], [ 1, %20 ], [ 1, %27 ], [ 1, %33 ], [ 0, %36 ]
ret i32 %39
}