#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 S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
scalar_t__ close(int);
int creat(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(void) {
  int fd_a, fd_b;
  int temp;
  char *str = "Good Morning\n";
  fd_a = creat("a.txt", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd_a < 0) {
    perror("cannot create a.txt");
    exit(1);
  }
  printf("created file a.txt; fd_a is %d\n", fd_a);
  if ((temp = write(fd_a, str, strlen(str))) < 0) {
    perror("cannot write to a.txt");
    exit(1);
  }
  printf("written %d bytes to fd_a\n", temp);
  if (close(fd_a) < 0) {
    perror("cannot close a.txt");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    0x2e4e(%rip),%rax
or     (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    0x2e24(%rip),%rax
or     (%rax),%esi
lea    0xeca(%rip),%rdi
call   1080 <creat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11dd <main+0x6d>
lea    0xe40(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe3b(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1233 <main+0xc3>
lea    0xe1e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%esi
lea    0xe1b(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    126a <main+0xfa>
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e4d(%rip),%rax
mov    0x2e66(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4b(%rip),%rax
or     (%rax),%esi
mov    0x2e3a(%rip),%rax
or     (%rax),%esi
lea    0xee0(%rip),%rdi
call   1080 <creat@plt>
test   %eax,%eax
js     11fb <main+0x8b>
mov    %eax,%ebx
lea    0xe71(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
lea    0xe41(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1204 <main+0x94>
lea    0xe7a(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
test   %rax,%rax
js     120d <main+0x9d>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe0c(%rip),%rdi
jmp    1214 <main+0xa4>
lea    0xe37(%rip),%rdi
jmp    1214 <main+0xa4>
lea    0xe5e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"Good Morning\0A\00"
@.str.1 = [6 x i8] c"a.txt\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [20 x i8] c"cannot create a.txt\00"
@.str.3 = [32 x i8] c"created file a.txt; fd_a is %d\0A\00"
@.str.4 = [22 x i8] c"cannot write to a.txt\00"
@.str.5 = [26 x i8] c"written %d bytes to fd_a\0A\00"
@.str.6 = [19 x i8] c"cannot close a.txt\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRGRP
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IROTH
%12 = or i32 %10, %11
%13 = call i32 @creat(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %12)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %0
%20 = load i32, i32* %2
%21 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %2
%23 = load i8*, i8** %5
%24 = load i8*, i8** %5
%25 = call i32 @strlen(i8* %24)
%26 = call i32 @write(i32 %22, i8* %23, i32 %25)
store i32 %26, i32* %4
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %19
%32 = load i32, i32* %4
%33 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %2
%35 = call i64 @close(i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %31
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"Good Morning\0A\00"
@.str.1 = [6 x i8] c"a.txt\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [20 x i8] c"cannot create a.txt\00"
@.str.3 = [32 x i8] c"created file a.txt; fd_a is %d\0A\00"
@.str.4 = [22 x i8] c"cannot write to a.txt\00"
@.str.5 = [26 x i8] c"written %d bytes to fd_a\0A\00"
@.str.6 = [19 x i8] c"cannot close a.txt\00"
define i32 @main() {
%1 = load i32, i32* @S_IRUSR
%2 = load i32, i32* @S_IWUSR
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IRGRP
%5 = or i32 %3, %4
%6 = load i32, i32* @S_IROTH
%7 = or i32 %5, %6
%8 = tail call i32 @creat(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %8)
%15 = tail call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i32 %16)
%23 = tail call i64 @close(i32 %8)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %21
ret i32 0
}