#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 /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ malloc(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int strlen(char *);
int write(int, char *, int);
int main() {
  char *c;
  int fd, sz, fd1, sz1;
  c = (char *)malloc(120 * sizeof(char));
  fd = open("foo.txt", O_RDONLY);
  fd1 = open("out3.txt", O_RDWR | O_CREAT | O_APPEND, 0644);
  if (fd < 0 && fd1 < 0) {
    perror("r1");
    exit(1);
  }
  sz = read(fd, c, 12);
  printf("called read(%d, c, 10).returned that %d bytes were read.\n", fd, sz);
  c[sz] = '\0';
  printf("Those bytes are as follows: %s\n", c);
  sz1 = write(fd1, c, strlen(c));
  printf("called write(%d,c, %d).it returned %d\n", fd, strlen(c), sz1);
  close(fd);
  close(fd1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x78,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
mov    0x2de0(%rip),%rax
or     (%rax),%esi
lea    0xe1f(%rip),%rdi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1222 <main+0x92>
cmpl   $0x0,-0x1c(%rbp)
jge    1222 <main+0x92>
lea    0xdfe(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rsi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
mov    -0x20(%rbp),%ecx
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x78,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdi
xor    %eax,%eax
call   1090 <open@plt>
mov    0x2e1e(%rip),%rcx
mov    0x2e2f(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %eax,%r15d
mov    0x2de9(%rip),%rax
or     (%rax),%esi
lea    0xe28(%rip),%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1090 <open@plt>
test   %r15d,%eax
js     1286 <main+0xf6>
mov    %eax,%r14d
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1070 <read@plt>
mov    %eax,%ebp
lea    0xe03(%rip),%rdi
mov    %r15d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
lea    0xe23(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0xe13(%rip),%rdi
mov    %r15d,%esi
mov    %eax,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd84(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"foo.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [9 x i8] c"out3.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.2 = [3 x i8] c"r1\00"
@.str.3 = [58 x i8] c"called read(%d, c, 10).returned that %d bytes were read.\0A\00"
@.str.4 = [32 x i8] c"Those bytes are as follows: %s\0A\00"
@.str.5 = [39 x i8] c"called write(%d,c, %d).it returned %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i64 @malloc(i32 120)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %2
%9 = load i32, i32* @O_RDONLY
%10 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @O_APPEND
%15 = or i32 %13, %14
%16 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 420)
store i32 %16, i32* %5
%17 = load i32, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %0
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %19, %0
%26 = load i32, i32* %3
%27 = load i8*, i8** %2
%28 = call i32 @read(i32 %26, i8* %27, i32 12)
store i32 %28, i32* %4
%29 = load i32, i32* %3
%30 = load i32, i32* %4
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), i32 %29, i32 %30)
%32 = load i8*, i8** %2
%33 = load i32, i32* %4
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %2
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %36)
%38 = load i32, i32* %5
%39 = load i8*, i8** %2
%40 = load i8*, i8** %2
%41 = call i32 @strlen(i8* %40)
%42 = call i32 @write(i32 %38, i8* %39, i32 %41)
store i32 %42, i32* %6
%43 = load i32, i32* %3
%44 = load i8*, i8** %2
%45 = call i32 @strlen(i8* %44)
%46 = load i32, i32* %6
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %43, i32 %45, i32 %46)
%48 = load i32, i32* %3
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %5
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
@.str = [8 x i8] c"foo.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [9 x i8] c"out3.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.2 = [3 x i8] c"r1\00"
@.str.3 = [58 x i8] c"called read(%d, c, 10).returned that %d bytes were read.\0A\00"
@.str.4 = [32 x i8] c"Those bytes are as follows: %s\0A\00"
@.str.5 = [39 x i8] c"called write(%d,c, %d).it returned %d\0A\00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 120)
%2 = inttoptr i64 %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_APPEND
%9 = or i32 %7, %8
%10 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 420)
%11 = and i32 %10, %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = tail call i32 @read(i32 %4, i8* %2, i32 12)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), i32 %4, i32 %17)
%19 = sext i32 %17 to i64
%20 = getelementptr inbounds i8, i8* %2, i64 %19
store i8 0, i8* %20
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%22 = tail call i32 @strlen(i8* %2)
%23 = tail call i32 @write(i32 %10, i8* %2, i32 %22)
%24 = tail call i32 @strlen(i8* %2)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %4, i32 %24, i32 %23)
%26 = tail call i32 @close(i32 %4)
%27 = tail call i32 @close(i32 %10)
ret i32 0
}