#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);
}


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    1297 <main+0x137>
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    1297 <main+0x137>
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    1297 <main+0x137>
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    -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    1238 <main+0xc8>
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     122d <main+0xbd>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    %r14d,%edx
call   1050 <write@plt>
cmp    %r14d,%eax
jne    1256 <main+0xe6>
lea    0xe8c(%rip),%rdi
call   1040 <puts@plt>
mov    (%r15),%esi
mov    (%r12),%edx
mov    0x2de8(%rip),%rax
mov    (%rax),%ecx
mov    0x2daf(%rip),%rax
mov    (%rax),%r8d
lea    0xe32(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    1244 <main+0xd4>
mov    0x8(%rbx),%rsi
lea    0xe00(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe27(%rip),%rdi
call   1040 <puts@plt>
jmp    123f <main+0xcf>

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
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 %53
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 %53
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 %53
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)
br label %53
53: ; preds = %44, %42, %30, %11
%54 = load i32, i32* %3
ret i32 %54
}

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 = [3 x i8] c"ok\00"
@str.6 = [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 %37
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 %35
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.6, i64 0, i64 0))
br label %35
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)
br label %35
35: ; preds = %27, %25, %19
%36 = phi i32 [ 1, %19 ], [ 1, %25 ], [ 0, %27 ]
br label %37
37: ; preds = %35, %5
%38 = phi i32 [ 1, %5 ], [ %36, %35 ]
ret i32 %38
}