#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 /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, const char *argv[]) {
  int fd;
  char buf[14];
  fd = open("myfile.txt", O_CREAT | O_WRONLY, 0700);
  if (fd == -1) {
    printf("Failed to create and open the file.\n");
  }
  write(fd, "Hello World!\n", 13);
  close(fd);
  fd = open("myfile.txt", O_RDONLY);
  if (fd == -1) {
    printf("Failed to open and read the file.\n");
    exit(1);
  }
  read(fd, buf, 13);
  buf[13] = '\0';
  close(fd);
  printf("buf: %s\n", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    0x2e42(%rip),%rax
or     (%rax),%esi
lea    0xe71(%rip),%rdi
mov    $0x1c0,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b6 <main+0x56>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    0xe70(%rip),%rsi
mov    $0xd,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    120e <main+0xae>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0xd,%edx
call   1060 <read@plt>
movb   $0x0,-0x15(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
lea    -0x22(%rbp),%rsi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e7a(%rip),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe70(%rip),%rdi
mov    $0x1c0,%edx
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11af <main+0x3f>
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
lea    0xe55(%rip),%rsi
mov    %ebx,%edi
mov    $0xd,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    0xe27(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1222 <main+0xb2>
mov    %eax,%ebx
lea    0xa(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0xd,%edx
call   1070 <read@plt>
movb   $0x0,0x17(%rsp)
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xe0b(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdf9(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"myfile.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [37 x i8] c"Failed to create and open the file.\0A\00"
@.str.2 = [14 x i8] c"Hello World!\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [35 x i8] c"Failed to open and read the file.\0A\00"
@.str.4 = [9 x i8] c"buf: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [14 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_WRONLY
%10 = or i32 %8, %9
%11 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %10, i32 448)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i32, i32* %6
%18 = call i32 @write(i32 %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 13)
%19 = load i32, i32* %6
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* @O_RDONLY
%22 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %16
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %16
%29 = load i32, i32* %6
%30 = getelementptr inbounds [14 x i8], [14 x i8]* %7, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 13)
%32 = getelementptr inbounds [14 x i8], [14 x i8]* %7, i64 0, i64 13
store i8 0, i8* %32
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
%35 = getelementptr inbounds [14 x i8], [14 x i8]* %7, i64 0, i64 0
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %35)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"myfile.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.2 = [14 x i8] c"Hello World!\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [9 x i8] c"buf: %s\0A\00"
@str = [34 x i8] c"Failed to open and read the file.\00"
@str.5 = [36 x i8] c"Failed to create and open the file.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [14 x i8]
%4 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_WRONLY
%7 = or i32 %6, %5
%8 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7, i32 448)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.5, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 13)
%14 = tail call i32 @close(i32 %8)
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = call i32 @read(i32 %16, i8* %4, i32 13)
%23 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 13
store i8 0, i8* %23
%24 = tail call i32 @close(i32 %16)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %4)
ret i32 0
}