#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_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int strlen(char *);
unsigned int write(int, char *, unsigned int);
int main(int argc, char **argv) {
  int fd = open("/mnt/snapshot/test_file", O_RDWR | O_CREAT);
  if (fd == -1) {
    printf("Error opening test file\n");
    return -1;
  }
  unsigned int str_len =
      strlen("hello great big world out there") * sizeof(char);
  unsigned int written = 0;
  while (written != str_len) {
    written = write(fd, "hello great big world out there" + written,
                    str_len - written);
    if (written == -1) {
      printf("Error while writing to test file\n");
      goto out;
    }
  }
  close(fd);
  return 0;
out:
  close(fd);
  return -1;
}


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)
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    0x2e82(%rip),%rax
or     (%rax),%esi
lea    0xe81(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a9 <main+0x59>
lea    0xe80(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1232 <main+0xe2>
lea    0xe81(%rip),%rdi
call   1040 <strlen@plt>
cltq
shl    $0x0,%rax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     120f <main+0xbf>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%eax
lea    0xe53(%rip),%rsi
add    %rax,%rsi
mov    -0x18(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    120a <main+0xba>
lea    0xe51(%rip),%rdi
call   1050 <printf@plt>
jmp    1223 <main+0xd3>
jmp    11c5 <main+0x75>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1232 <main+0xe2>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e85(%rip),%rax
mov    0x2e96(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe93(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11c1 <main+0x71>
mov    %eax,%ebx
lea    0xe98(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
xor    %eax,%eax
nopl   0x0(%rax)
mov    %ebp,%edx
sub    %eax,%edx
je     11d4 <main+0x84>
mov    %eax,%esi
add    %r14,%rsi
mov    %ebx,%edi
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1190 <main+0x40>
lea    0xe8a(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%eax
jmp    11dd <main+0x8d>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    11dd <main+0x8d>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"/mnt/snapshot/test_file\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [25 x i8] c"Error opening test file\0A\00"
@.str.2 = [32 x i8] c"hello great big world out there\00"
@.str.3 = [34 x i8] c"Error while writing to test file\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %46
17: ; preds = %2
%18 = call i32 @strlen(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%19 = sext i32 %18 to i64
%20 = mul i64 %19, 1
%21 = trunc i64 %20 to i32
store i32 %21, i32* %7
store i32 0, i32* %8
br label %22
22: ; preds = %39, %17
%23 = load i32, i32* %8
%24 = load i32, i32* %7
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %40
26: ; preds = %22
%27 = load i32, i32* %6
%28 = load i32, i32* %8
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64 %29
%31 = load i32, i32* %7
%32 = load i32, i32* %8
%33 = sub i32 %31, %32
%34 = call i32 @write(i32 %27, i8* %30, i32 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %43
39: ; preds = %26
br label %22
40: ; preds = %22
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
store i32 0, i32* %3
br label %46
43: ; preds = %37
%44 = load i32, i32* %6
%45 = call i32 @close(i32 %44)
store i32 -1, i32* %3
br label %46
46: ; preds = %43, %40, %15
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@.str = [24 x i8] c"/mnt/snapshot/test_file\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [32 x i8] c"hello great big world out there\00"
@str = [33 x i8] c"Error while writing to test file\00"
@str.4 = [24 x i8] c"Error opening test file\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.4, i64 0, i64 0))
br label %26
10: ; preds = %2
%11 = tail call i32 @strlen(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %15, %10
%13 = phi i32 [ 0, %10 ], [ %19, %15 ]
%14 = icmp eq i32 %11, %13
br i1 %14, label %24, label %15
15: ; preds = %12
%16 = zext i32 %13 to i64
%17 = getelementptr inbounds [32 x i8], [32 x i8]* @.str.2, i64 0, i64 %16
%18 = sub i32 %11, %13
%19 = tail call i32 @write(i32 %6, i8* %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %12
21: ; preds = %15
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%23 = tail call i32 @close(i32 %6)
br label %26
24: ; preds = %12
%25 = tail call i32 @close(i32 %6)
br label %26
26: ; preds = %24, %21, %8
%27 = phi i32 [ -1, %8 ], [ -1, %21 ], [ 0, %24 ]
ret i32 %27
}