#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, char *argv[]) {
  int fd;
  char buf[14];
  fd = open("myfile.txt", O_CREAT | O_WRONLY, 0600);
  if (fd == -1) {
    printf("Failed to create and open the file.\n");
    exit(1);
  }
  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("%s", 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    $0x180,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c0 <main+0x60>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
lea    0xe66(%rip),%rsi
mov    $0xd,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1218 <main+0xb8>
lea    0xe37(%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    0xe21(%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    $0x180,%edx
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1216 <main+0xa6>
mov    %eax,%ebx
lea    0xe61(%rip),%rsi
mov    %eax,%edi
mov    $0xd,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    0xe33(%rip),%rdi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     121f <main+0xaf>
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    0xe17(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe21(%rip),%rdi
jmp    1226 <main+0xb6>
lea    0xdf6(%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 = [3 x i8] c"%s\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 384)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* %6
%19 = call i32 @write(i32 %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 13)
%20 = load i32, i32* %6
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* @O_RDONLY
%23 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %17
%30 = load i32, i32* %6
%31 = getelementptr inbounds [14 x i8], [14 x i8]* %7, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 13)
%33 = getelementptr inbounds [14 x i8], [14 x i8]* %7, i64 0, i64 13
store i8 0, i8* %33
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [14 x i8], [14 x i8]* %7, i64 0, i64 0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %36)
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 = [3 x i8] c"%s\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 384)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
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))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 13)
%15 = tail call i32 @close(i32 %8)
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = call i32 @read(i32 %17, i8* %4, i32 13)
%24 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 13
store i8 0, i8* %24
%25 = tail call i32 @close(i32 %17)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %4)
ret i32 0
}