#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 O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int main() {
  int sourcefile = open("string.c", O_RDONLY);
  int destfile = open("copyofstring.c", O_WRONLY | O_CREAT);
  if (sourcefile == -1) {
    perror("open source file failed");
    return 1;
  }
  if (destfile == -1) {
    perror("open dest file failed");
    return 2;
  }
  close(sourcefile);
  close(destfile);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
lea    0xeb7(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e91(%rip),%rax
or     (%rax),%esi
lea    0xe90(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    119a <main+0x6a>
lea    0xe86(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11d3 <main+0xa3>
cmpl   $0xffffffff,-0xc(%rbp)
jne    11bc <main+0x8c>
lea    0xe7c(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    11d3 <main+0xa3>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
lea    0xec3(%rip),%rdi
call   1040 <open@plt>
mov    %eax,%ebx
mov    0x2e77(%rip),%rax
mov    0x2ea0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe9d(%rip),%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%ebx
je     118b <main+0x5b>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     119e <main+0x6e>
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe7d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%eax
jmp    1184 <main+0x54>
lea    0xe82(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%eax
jmp    1184 <main+0x54>

LLVM IR (-O0):
@.str = [9 x i8] c"string.c\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"copyofstring.c\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [24 x i8] c"open source file failed\00"
@.str.3 = [22 x i8] c"open dest file failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %24
14: ; preds = %0
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %24
19: ; preds = %14
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %3
%23 = call i32 @close(i32 %22)
store i32 0, i32* %1
br label %24
24: ; preds = %19, %17, %12
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [9 x i8] c"string.c\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"copyofstring.c\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [24 x i8] c"open source file failed\00"
@.str.3 = [22 x i8] c"open dest file failed\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %2, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %17
10: ; preds = %0
%11 = icmp eq i32 %6, -1
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %17
14: ; preds = %10
%15 = tail call i32 @close(i32 %2)
%16 = tail call i32 @close(i32 %6)
br label %17
17: ; preds = %14, %12, %8
%18 = phi i32 [ 1, %8 ], [ 2, %12 ], [ 0, %14 ]
ret i32 %18
}