#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_APPEND;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage %s: file\n", argv[0]);
    return 1;
  }
  int fd, len;
  char buf[512];
  strcpy(buf, "testing write file system and creation\n");
  len = strlen(buf);
  fd = open(argv[1], O_RDWR | O_CREAT, 0755);
  if (fd < 0) {
    printf("cannot open file %s\n", argv[1]);
    return 1;
  }
  if (write(fd, buf, len) != len) {
    printf("cannot write to file\n");
    return 1;
  }
  printf("ok\n");
  printf("O_RDWR: %x, O_CREAT: %x, O_TRUNC: %x, O_APPEND: %x\n", O_RDWR,
         O_CREAT, O_TRUNC, O_APPEND);
  close(fd);
  fd = open(argv[1], O_RDWR | O_TRUNC, 0);
  if (fd < 0) {
    printf("Cannot open file in truncate mode\n");
    return 1;
  }
  if (write(fd, "Testing", 8) != 8) {
    printf("cannot write");
    return 1;
  }
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a4 <main+0x44>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131e <main+0x1be>
lea    -0x220(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x220(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
mov    $0x1ed,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1219 <main+0xb9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131e <main+0x1be>
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <write@plt>
cmp    -0x18(%rbp),%eax
je     124e <main+0xee>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131e <main+0x1be>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
mov    0x2d7b(%rip),%rax
mov    (%rax),%ecx
mov    0x2d42(%rip),%rax
mov    (%rax),%r8d
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
mov    0x2d41(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12df <main+0x17f>
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131e <main+0x1be>
mov    -0x14(%rbp),%edi
lea    0xdd5(%rip),%rsi
mov    $0x8,%edx
call   1040 <write@plt>
cmp    $0x8,%eax
je     1316 <main+0x1b6>
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131e <main+0x1be>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     1196 <main+0x26>
mov    (%rbx),%rsi
lea    0xe6f(%rip),%rdi
jmp    1277 <main+0x107>
lea    0xe73(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %eax,%r14d
mov    0x8(%rbx),%rdi
mov    0x2e1a(%rip),%r15
mov    0x2e33(%rip),%r12
mov    (%r12),%esi
or     (%r15),%esi
mov    $0x1ed,%edx
call   1090 <open@plt>
test   %eax,%eax
js     126c <main+0xfc>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    %r14d,%edx
call   1050 <write@plt>
cmp    %r14d,%eax
jne    1280 <main+0x110>
lea    0xe99(%rip),%rdi
call   1040 <puts@plt>
mov    (%r15),%esi
mov    (%r12),%edx
mov    0x2de0(%rip),%r14
mov    (%r14),%ecx
mov    0x2da6(%rip),%rax
mov    (%rax),%r8d
lea    0xe29(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    0x8(%rbx),%rdi
mov    (%r14),%esi
or     (%r15),%esi
xor    %edx,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1289 <main+0x119>
mov    %eax,%ebx
lea    0xe31(%rip),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1050 <write@plt>
cmp    $0x8,%eax
jne    12aa <main+0x13a>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
jmp    129a <main+0x12a>
mov    0x8(%rbx),%rsi
lea    0xdc1(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1295 <main+0x125>
lea    0xe34(%rip),%rdi
jmp    1290 <main+0x120>
lea    0xe09(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd8(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1295 <main+0x125>

LLVM IR (-O0):
@.str = [16 x i8] c"Usage %s: file\0A\00"
@.str.1 = [40 x i8] c"testing write file system and creation\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [21 x i8] c"cannot open file %s\0A\00"
@.str.3 = [22 x i8] c"cannot write to file\0A\00"
@.str.4 = [4 x i8] c"ok\0A\00"
@.str.5 = [52 x i8] c"O_RDWR: %x, O_CREAT: %x, O_TRUNC: %x, O_APPEND: %x\0A\00"
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
@.str.6 = [35 x i8] c"Cannot open file in truncate mode\0A\00"
@.str.7 = [8 x i8] c"Testing\00"
@.str.8 = [13 x i8] c"cannot write\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 [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 1, i32* %3
br label %73
16: ; preds = %2
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%18 = call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %7
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_CREAT
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %23, i32 %26, i32 493)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %16
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %33)
store i32 1, i32* %3
br label %73
35: ; preds = %16
%36 = load i32, i32* %6
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%38 = load i32, i32* %7
%39 = call i32 @write(i32 %36, i8* %37, i32 %38)
%40 = load i32, i32* %7
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %35
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %73
44: ; preds = %35
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%46 = load i32, i32* @O_RDWR
%47 = load i32, i32* @O_CREAT
%48 = load i32, i32* @O_TRUNC
%49 = load i32, i32* @O_APPEND
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.5, i64 0, i64 0), i32 %46, i32 %47, i32 %48, i32 %49)
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 1
%55 = load i8*, i8** %54
%56 = load i32, i32* @O_RDWR
%57 = load i32, i32* @O_TRUNC
%58 = or i32 %56, %57
%59 = call i32 @open(i8* %55, i32 %58, i32 0)
store i32 %59, i32* %6
%60 = load i32, i32* %6
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %44
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %73
64: ; preds = %44
%65 = load i32, i32* %6
%66 = call i32 @write(i32 %65, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 8)
%67 = icmp ne i32 %66, 8
br i1 %67, label %68, label %70
68: ; preds = %64
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %3
br label %73
70: ; preds = %64
%71 = load i32, i32* %6
%72 = call i32 @close(i32 %71)
br label %73
73: ; preds = %70, %68, %62, %42, %30, %11
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@.str = [16 x i8] c"Usage %s: file\0A\00"
@.str.1 = [40 x i8] c"testing write file system and creation\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [21 x i8] c"cannot open file %s\0A\00"
@.str.5 = [52 x i8] c"O_RDWR: %x, O_CREAT: %x, O_TRUNC: %x, O_APPEND: %x\0A\00"
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
@.str.7 = [8 x i8] c"Testing\00"
@.str.8 = [13 x i8] c"cannot write\00"
@str = [3 x i8] c"ok\00"
@str.9 = [34 x i8] c"Cannot open file in truncate mode\00"
@str.10 = [21 x i8] c"cannot write to file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %52
8: ; preds = %2
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%10 = call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @strlen(i8* %9)
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_CREAT
%16 = or i32 %15, %14
%17 = call i32 @open(i8* %13, i32 %16, i32 493)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %8
%20 = load i8*, i8** %12
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %20)
br label %50
22: ; preds = %8
%23 = call i32 @write(i32 %17, i8* %9, i32 %11)
%24 = icmp eq i32 %23, %11
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.10, i64 0, i64 0))
br label %50
27: ; preds = %22
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
%29 = load i32, i32* @O_RDWR
%30 = load i32, i32* @O_CREAT
%31 = load i32, i32* @O_TRUNC
%32 = load i32, i32* @O_APPEND
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.5, i64 0, i64 0), i32 %29, i32 %30, i32 %31, i32 %32)
%34 = call i32 @close(i32 %17)
%35 = load i8*, i8** %12
%36 = load i32, i32* @O_RDWR
%37 = load i32, i32* @O_TRUNC
%38 = or i32 %37, %36
%39 = call i32 @open(i8* %35, i32 %38, i32 0)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %27
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.9, i64 0, i64 0))
br label %50
43: ; preds = %27
%44 = call i32 @write(i32 %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i32 8)
%45 = icmp eq i32 %44, 8
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
br label %50
48: ; preds = %43
%49 = call i32 @close(i32 %39)
br label %50
50: ; preds = %48, %46, %41, %25, %19
%51 = phi i32 [ 1, %19 ], [ 1, %25 ], [ 1, %41 ], [ 1, %46 ], [ 0, %48 ]
br label %52
52: ; preds = %50, %5
%53 = phi i32 [ 1, %5 ], [ %51, %50 ]
ret i32 %53
}