#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_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int creat(char *, int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int fd1;
  int fd2;
  int fd3;
  char buf[32];
  fd1 = open("test.txt", O_CREAT | O_WRONLY | O_TRUNC,
             S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  if (fd1 == -1) {
    perror("open(1)");
    return -1;
  }
  write(fd1, "ABCDE", 5);
  close(fd1);
  fd2 = open("test.txt", O_RDONLY);
  if (fd2 == -1) {
    perror("open(2)");
    return -2;
  }
  read(fd2, buf, 32);
  printf("buf = %s\n", buf);
  close(fd2);
  fd3 = creat("test.txt",
              S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  if (fd3 == -1) {
    perror("creat");
    return -3;
  }
  write(fd3, "XYZ", 3);
  close(fd3);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    0x2e1e(%rip),%rax
or     (%rax),%edx
mov    0x2e05(%rip),%rax
or     (%rax),%edx
mov    0x2e14(%rip),%rax
or     (%rax),%edx
mov    0x2deb(%rip),%rax
or     (%rax),%edx
mov    0x2dc2(%rip),%rax
or     (%rax),%edx
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1203 <main+0x93>
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130e <main+0x19e>
mov    -0x8(%rbp),%edi
lea    0xe04(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    125b <main+0xeb>
lea    0xdcd(%rip),%rdi
call   1080 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    130e <main+0x19e>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x20,%edx
call   1060 <read@plt>
lea    -0x30(%rbp),%rsi
lea    0xda8(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%esi
mov    0x2d34(%rip),%rax
or     (%rax),%esi
mov    0x2d1b(%rip),%rax
or     (%rax),%esi
mov    0x2d2a(%rip),%rax
or     (%rax),%esi
mov    0x2d01(%rip),%rax
or     (%rax),%esi
mov    0x2cd8(%rip),%rax
or     (%rax),%esi
lea    0xd3f(%rip),%rdi
call   1090 <creat@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    12eb <main+0x17b>
lea    0xd4f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    130e <main+0x19e>
mov    -0x10(%rbp),%edi
lea    0xd3a(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e73(%rip),%rax
mov    0x2e0c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e59(%rip),%rax
or     (%rax),%esi
mov    0x2e08(%rip),%rbp
mov    0x2e21(%rip),%r14
mov    (%r14),%edx
or     0x0(%rbp),%edx
mov    0x2e04(%rip),%r13
or     0x0(%r13),%edx
mov    0x2e11(%rip),%r12
or     (%r12),%edx
mov    0x2de6(%rip),%rax
or     (%rax),%edx
mov    0x2dbd(%rip),%rax
or     (%rax),%edx
lea    0xe24(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12b6 <main+0x146>
mov    %eax,%ebx
lea    0xe1c(%rip),%rsi
mov    %eax,%edi
mov    $0x5,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
lea    0xde8(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12c9 <main+0x159>
mov    %eax,%ebx
mov    %r13,%r15
mov    %r14,%r13
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x20,%edx
call   1060 <read@plt>
lea    0xdd6(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x0(%r13),%esi
or     0x0(%rbp),%esi
or     (%r15),%esi
or     (%r12),%esi
mov    0x2d43(%rip),%rax
or     (%rax),%esi
mov    0x2d1a(%rip),%rax
or     (%rax),%esi
lea    0xd81(%rip),%rdi
call   1090 <creat@plt>
cmp    $0xffffffff,%eax
je     12dc <main+0x16c>
mov    %eax,%ebx
lea    0xd9d(%rip),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4c(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    12a7 <main+0x137>
lea    0xd47(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffe,%eax
jmp    12a7 <main+0x137>
lea    0xd46(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffd,%eax
jmp    12a7 <main+0x137>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str.1 = [8 x i8] c"open(1)\00"
@.str.2 = [6 x i8] c"ABCDE\00"
@O_RDONLY = global i32 0
@.str.3 = [8 x i8] c"open(2)\00"
@.str.4 = [10 x i8] c"buf = %s\0A\00"
@.str.5 = [6 x i8] c"creat\00"
@.str.6 = [4 x i8] c"XYZ\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [32 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_WRONLY
%8 = or i32 %6, %7
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRGRP
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IWGRP
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IROTH
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IWOTH
%21 = or i32 %19, %20
%22 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %21)
store i32 %22, i32* %2
%23 = load i32, i32* %2
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %0
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %67
27: ; preds = %0
%28 = load i32, i32* %2
%29 = call i32 @write(i32 %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%30 = load i32, i32* %2
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* @O_RDONLY
%33 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %32)
store i32 %33, i32* %3
%34 = load i32, i32* %3
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %27
%37 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 -2, i32* %1
br label %67
38: ; preds = %27
%39 = load i32, i32* %3
%40 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%41 = call i32 @read(i32 %39, i8* %40, i32 32)
%42 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%43 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %42)
%44 = load i32, i32* %3
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* @S_IRUSR
%47 = load i32, i32* @S_IWUSR
%48 = or i32 %46, %47
%49 = load i32, i32* @S_IRGRP
%50 = or i32 %48, %49
%51 = load i32, i32* @S_IWGRP
%52 = or i32 %50, %51
%53 = load i32, i32* @S_IROTH
%54 = or i32 %52, %53
%55 = load i32, i32* @S_IWOTH
%56 = or i32 %54, %55
%57 = call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %56)
store i32 %57, i32* %4
%58 = load i32, i32* %4
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %38
%61 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
store i32 -3, i32* %1
br label %67
62: ; preds = %38
%63 = load i32, i32* %4
%64 = call i32 @write(i32 %63, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 3)
%65 = load i32, i32* %4
%66 = call i32 @close(i32 %65)
store i32 0, i32* %1
br label %67
67: ; preds = %62, %60, %36, %25
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@.str.1 = [8 x i8] c"open(1)\00"
@.str.2 = [6 x i8] c"ABCDE\00"
@O_RDONLY = global i32 0
@.str.3 = [8 x i8] c"open(2)\00"
@.str.4 = [10 x i8] c"buf = %s\0A\00"
@.str.5 = [6 x i8] c"creat\00"
@.str.6 = [4 x i8] c"XYZ\00"
define i32 @main() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_WRONLY
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IWGRP
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IROTH
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IWOTH
%18 = or i32 %16, %17
%19 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %0
%22 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %53
23: ; preds = %0
%24 = tail call i32 @write(i32 %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%25 = tail call i32 @close(i32 %19)
%26 = load i32, i32* @O_RDONLY
%27 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %53
31: ; preds = %23
%32 = call i32 @read(i32 %27, i8* %2, i32 32)
%33 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%34 = call i32 @close(i32 %27)
%35 = load i32, i32* @S_IRUSR
%36 = load i32, i32* @S_IWUSR
%37 = or i32 %36, %35
%38 = load i32, i32* @S_IRGRP
%39 = or i32 %37, %38
%40 = load i32, i32* @S_IWGRP
%41 = or i32 %39, %40
%42 = load i32, i32* @S_IROTH
%43 = or i32 %41, %42
%44 = load i32, i32* @S_IWOTH
%45 = or i32 %43, %44
%46 = call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %31
%49 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %53
50: ; preds = %31
%51 = call i32 @write(i32 %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 3)
%52 = call i32 @close(i32 %46)
br label %53
53: ; preds = %50, %48, %29, %21
%54 = phi i32 [ -1, %21 ], [ -2, %29 ], [ -3, %48 ], [ 0, %50 ]
ret i32 %54
}