#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 EOF;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int getchar();
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(void) {
  int fd;
  int cont;
  char *temp = "ABCDEFG\n";
  char buff[1024];
  if (0 < (fd = open("/opt/test.txt", O_RDWR | O_CREAT, 0644))) {
    while (1) {
      printf("write test\n");
      if (write(fd, temp, strlen(temp)) == -1) {
        printf("write failed\n");
      }
      printf("\nread test\n");
      if (read(fd, buff, 1024) == -1) {
        printf("read failed\n");
      }
      printf("continue?  \n");
      cont = getchar();
      if (cont == EOF)
        break;
    }
    close(fd);
  } else
    printf("file open failed\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    0x2e5b(%rip),%rax
or     (%rax),%esi
lea    0xe63(%rip),%rdi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jge    128a <main+0x11a>
jmp    11c4 <main+0x54>
lea    0xe4c(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x42c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x42c(%rbp),%edi
mov    -0x428(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1216 <main+0xa6>
lea    0xe12(%rip),%rdi
call   1050 <printf@plt>
lea    0xe14(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
jne    124b <main+0xdb>
lea    0xdf7(%rip),%rdi
call   1050 <printf@plt>
lea    0xdf8(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2d6d(%rip),%rcx
cmp    (%rcx),%eax
jne    1278 <main+0x108>
jmp    127d <main+0x10d>
jmp    11c4 <main+0x54>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
jmp    1296 <main+0x126>
lea    0xdc6(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    0x2e58(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6f(%rip),%rdi
mov    $0x1a4,%edx
call   1090 <open@plt>
test   %eax,%eax
jle    1248 <main+0xd8>
mov    %eax,%ebx
lea    0xe4b(%rip),%r15
lea    0xe77(%rip),%r13
mov    %rsp,%rbp
lea    0xe78(%rip),%r12
mov    0x2e0b(%rip),%r14
jmp    11e4 <main+0x74>
nop
mov    %r12,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
cmp    (%r14),%eax
je     123f <main+0xcf>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1215 <main+0xa5>
lea    0xe46(%rip),%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
jne    11d0 <main+0x60>
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
jmp    11d0 <main+0x60>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1254 <main+0xe4>
lea    0xdc8(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"ABCDEFG\0A\00"
@.str.1 = [14 x i8] c"/opt/test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [12 x i8] c"write test\0A\00"
@.str.3 = [14 x i8] c"write failed\0A\00"
@.str.4 = [12 x i8] c"\0Aread test\0A\00"
@.str.5 = [13 x i8] c"read failed\0A\00"
@.str.6 = [13 x i8] c"continue? \0A\00"
@EOF = global i32 0
@.str.7 = [18 x i8] c"file open failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [1024 x i8]
store i32 0, i32* %1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %4
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 420)
store i32 %9, i32* %2
%10 = icmp slt i32 0, %9
br i1 %10, label %11, label %41
11: ; preds = %0
br label %12
12: ; preds = %11, %37
%13 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* %2
%15 = load i8*, i8** %4
%16 = load i8*, i8** %4
%17 = call i32 @strlen(i8* %16)
%18 = call i32 @write(i32 %14, i8* %15, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %12
%23 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%24 = load i32, i32* %2
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 1024)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %30
30: ; preds = %28, %22
%31 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%32 = call i32 (...) @getchar()
store i32 %32, i32* %3
%33 = load i32, i32* %3
%34 = load i32, i32* @EOF
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %30
br label %38
37: ; preds = %30
br label %12
38: ; preds = %36
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
br label %43
41: ; preds = %0
%42 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"ABCDEFG\0A\00"
@.str.1 = [14 x i8] c"/opt/test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@EOF = global i32 0
@str = [17 x i8] c"file open failed\00"
@str.8 = [11 x i8] c"write test\00"
@str.9 = [11 x i8] c"\0Aread test\00"
@str.10 = [12 x i8] c"continue? \00"
@str.11 = [12 x i8] c"read failed\00"
@str.12 = [13 x i8] c"write failed\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%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 ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %5, i32 420)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %28
8: ; preds = %0, %21
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.8, i64 0, i64 0))
%10 = tail call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.12, i64 0, i64 0))
br label %15
15: ; preds = %13, %8
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.9, i64 0, i64 0))
%17 = call i32 @read(i32 %6, i8* %2, i32 1024)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.11, i64 0, i64 0))
br label %21
21: ; preds = %19, %15
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.10, i64 0, i64 0))
%23 = tail call i32 (...) @getchar()
%24 = load i32, i32* @EOF
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %8
26: ; preds = %21
%27 = tail call i32 @close(i32 %6)
br label %30
28: ; preds = %0
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %30
30: ; preds = %28, %26
ret i32 0
}