#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_APPEND;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, int);
int strlen(char *);
int write(int, char *, int);
int main() {
  int fd, sz;
  fd = open("out3", O_RDWR | O_CREAT | O_APPEND, 0644);
  if (fd < 0) {
    perror("r1");
    exit(1);
  }
  sz = write(fd, "cs241\n", strlen("cs241\n"));
  printf("called write(%d,\"cs360\\n\", %d).it returned %d\n", fd,
         strlen("cs360\n"), sz);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
mov    0x2e37(%rip),%rax
or     (%rax),%esi
lea    0xe66(%rip),%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11c7 <main+0x57>
lea    0xe4d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    0xe34(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x10(%rbp),%edi
mov    %eax,%edx
lea    0xe23(%rip),%rsi
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    0xe44(%rip),%rdi
call   1040 <strlen@plt>
mov    -0xc(%rbp),%esi
mov    %eax,%edx
mov    -0x8(%rbp),%ecx
lea    0xe01(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e65(%rip),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e3b(%rip),%rax
or     (%rax),%esi
lea    0xe6a(%rip),%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11ef <main+0x7f>
mov    %eax,%ebx
lea    0xe5b(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebp
lea    0xe74(%rip),%rdi
call   1040 <strlen@plt>
lea    0xe39(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
mov    %ebp,%ecx
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe0f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"out3\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.1 = [3 x i8] c"r1\00"
@.str.2 = [7 x i8] c"cs241\0A\00"
@.str.3 = [47 x i8] c"called write(%d,\22cs360\\n\22, %d).it returned %d\0A\00"
@.str.4 = [7 x i8] c"cs360\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %3, %4
%6 = load i32, i32* @O_APPEND
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %7, i32 420)
store i32 %8, i32* %1
%9 = load i32, i32* %1
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* %1
%16 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @write(i32 %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %16)
store i32 %17, i32* %2
%18 = load i32, i32* %1
%19 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%20 = load i32, i32* %2
%21 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %18, i32 %19, i32 %20)
%22 = load i32, i32* %1
%23 = call i32 @close(i32 %22)
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"out3\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.1 = [3 x i8] c"r1\00"
@.str.2 = [7 x i8] c"cs241\0A\00"
@.str.3 = [47 x i8] c"called write(%d,\22cs360\\n\22, %d).it returned %d\0A\00"
@.str.4 = [7 x i8] c"cs360\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @O_APPEND
%5 = or i32 %3, %4
%6 = tail call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %5, i32 420)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = tail call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = tail call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%15 = tail call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %6, i32 %14, i32 %13)
%16 = tail call i32 @close(i32 %6)
ret i32 0
}