#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 */
typedef int ssize_t;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int write(int, char *, int);
int main(void) {
  char buf[4096];
  int fd;
  ssize_t res;
  memset(buf, 1, 4096);
  fd = open("./mnt/testbigfile.data", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
  if (fd < 0) {
    perror("Unable to open testfile");
    return 1;
  }
  res = write(fd, buf, 4096);
  if (res != 4096) {
    perror("Error writing");
    (void)close(fd);
    return 1;
  }
  res = lseek(fd, 409600, SEEK_SET);
  if (res < 0) {
    perror("Unable to seek in testfile");
    return 1;
  }
  res = write(fd, buf, 4096);
  if (res != 4096) {
    perror("Error writing");
    (void)close(fd);
    return 1;
  }
  (void)close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x1010(%rbp),%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
mov    0x2e36(%rip),%rax
or     (%rax),%edx
lea    0xe4d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jge    11e3 <main+0x83>
lea    0xe45(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d9 <main+0x179>
mov    -0x1014(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x1000,-0x1018(%rbp)
je     1233 <main+0xd3>
lea    0xe18(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1014(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d9 <main+0x179>
mov    -0x1014(%rbp),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
mov    $0x64000,%esi
call   1040 <lseek@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x0,-0x1018(%rbp)
jge    1277 <main+0x117>
lea    0xdd7(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d9 <main+0x179>
mov    -0x1014(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x1000,-0x1018(%rbp)
je     12c7 <main+0x167>
lea    0xd84(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1014(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d9 <main+0x179>
mov    -0x1014(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x1008,%rsp
mov    %rsp,%rdi
mov    $0x1,%ebp
mov    $0x1,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    0x2e31(%rip),%rax
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2f(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe55(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     122a <main+0xca>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1000,%edx
call   1030 <write@plt>
cmp    $0x1000,%eax
jne    1210 <main+0xb0>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x64000,%esi
call   1040 <lseek@plt>
test   %eax,%eax
js     1238 <main+0xd8>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1000,%edx
call   1030 <write@plt>
cmp    $0x1000,%eax
jne    1210 <main+0xb0>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe18(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x1,%ebp
jmp    1204 <main+0xa4>
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
jmp    1204 <main+0xa4>
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebp
jmp    1204 <main+0xa4>

LLVM IR (-O0):
@.str = [23 x i8] c"./mnt/testbigfile.data\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [24 x i8] c"Unable to open testfile\00"
@.str.2 = [14 x i8] c"Error writing\00"
@SEEK_SET = global i32 0
@.str.3 = [27 x i8] c"Unable to seek in testfile\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [4096 x i8]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 1, i32 4096)
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IWUSR
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %49
18: ; preds = %0
%19 = load i32, i32* %3
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%21 = call i32 @write(i32 %19, i8* %20, i32 4096)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 4096
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* %3
%27 = call i32 @close(i32 %26)
store i32 1, i32* %1
br label %49
28: ; preds = %18
%29 = load i32, i32* %3
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @lseek(i32 %29, i32 409600, i32 %30)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %49
36: ; preds = %28
%37 = load i32, i32* %3
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%39 = call i32 @write(i32 %37, i8* %38, i32 4096)
store i32 %39, i32* %4
%40 = load i32, i32* %4
%41 = icmp ne i32 %40, 4096
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* %3
%45 = call i32 @close(i32 %44)
store i32 1, i32* %1
br label %49
46: ; preds = %36
%47 = load i32, i32* %3
%48 = call i32 @close(i32 %47)
store i32 0, i32* %1
br label %49
49: ; preds = %46, %42, %34, %24, %16
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
@.str = [23 x i8] c"./mnt/testbigfile.data\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [24 x i8] c"Unable to open testfile\00"
@.str.2 = [14 x i8] c"Error writing\00"
@SEEK_SET = global i32 0
@.str.3 = [27 x i8] c"Unable to seek in testfile\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i32 1, i32 4096)
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %8, %7
%10 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %34
14: ; preds = %0
%15 = call i32 @write(i32 %10, i8* %2, i32 4096)
%16 = icmp eq i32 %15, 4096
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @close(i32 %10)
br label %34
20: ; preds = %14
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @lseek(i32 %10, i32 409600, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %34
26: ; preds = %20
%27 = call i32 @write(i32 %10, i8* %2, i32 4096)
%28 = icmp eq i32 %27, 4096
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @close(i32 %10)
br label %34
32: ; preds = %26
%33 = call i32 @close(i32 %10)
br label %34
34: ; preds = %32, %29, %24, %17, %12
%35 = phi i32 [ 1, %12 ], [ 1, %17 ], [ 1, %24 ], [ 1, %29 ], [ 0, %32 ]
ret i32 %35
}