#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 */
typedef int ssize_t;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int write(int, char *, int);
int main() {
  int sourcefile = open("string.c", O_RDONLY);
  if (sourcefile == -1) {
    perror("open source file failed");
    return 1;
  }
  int destfile =
      open("copyofstring.c", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  if (destfile == -1) {
    perror("open dest file failed");
    return 2;
  }
  ssize_t rret = 0;
  ssize_t wret = 0;
  char buf[100];
  rret = read(sourcefile, buf, 100);
  if (rret == -1) {
    perror("read error");
    return 3;
  }
  wret = write(destfile, buf, rret);
  if (wret == -1) {
    perror("write error");
    return 4;
  }
  close(sourcefile);
  close(destfile);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
lea    0xeac(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    119e <main+0x4e>
lea    0xe73(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    128d <main+0x13d>
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
mov    0x2e07(%rip),%rax
or     (%rax),%edx
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11fe <main+0xae>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    128d <main+0x13d>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1242 <main+0xf2>
lea    0xe0c(%rip),%rdi
call   1070 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    128d <main+0x13d>
mov    -0xc(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1276 <main+0x126>
lea    0xde3(%rip),%rdi
call   1070 <perror@plt>
movl   $0x4,-0x4(%rbp)
jmp    128d <main+0x13d>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xeb8(%rip),%rdi
xor    %eax,%eax
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11f6 <main+0xa6>
mov    %eax,%ebx
mov    0x2e31(%rip),%rax
mov    0x2e72(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
mov    0x2e26(%rip),%rax
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe6c(%rip),%rdi
xor    %eax,%eax
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1209 <main+0xb9>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x64,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     121c <main+0xcc>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     122f <main+0xdf>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe03(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    11ef <main+0x9f>
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%eax
jmp    11ef <main+0x9f>
lea    0xe1a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%eax
jmp    11ef <main+0x9f>
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
mov    $0x4,%eax
jmp    11ef <main+0x9f>

LLVM IR (-O0):
@.str = [9 x i8] c"string.c\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"open source file failed\00"
@.str.2 = [15 x i8] c"copyofstring.c\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [22 x i8] c"open dest file failed\00"
@.str.4 = [11 x i8] c"read error\00"
@.str.5 = [12 x i8] c"write error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [100 x i8]
store i32 0, i32* %1
%7 = load i32, i32* @O_RDONLY
%8 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %49
13: ; preds = %0
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IWUSR
%21 = or i32 %19, %20
%22 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 %21)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %13
%26 = 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 %49
27: ; preds = %13
store i32 0, i32* %4
store i32 0, i32* %5
%28 = load i32, i32* %2
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 100)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
store i32 3, i32* %1
br label %49
35: ; preds = %27
%36 = load i32, i32* %3
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%38 = load i32, i32* %4
%39 = call i32 @write(i32 %36, i8* %37, i32 %38)
store i32 %39, i32* %5
%40 = load i32, i32* %5
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %35
%43 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
store i32 4, i32* %1
br label %49
44: ; preds = %35
%45 = load i32, i32* %2
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %3
%48 = call i32 @close(i32 %47)
store i32 0, i32* %1
br label %49
49: ; preds = %44, %42, %33, %25, %11
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
@.str = [9 x i8] c"string.c\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"open source file failed\00"
@.str.2 = [15 x i8] c"copyofstring.c\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [22 x i8] c"open dest file failed\00"
@.str.4 = [11 x i8] c"read error\00"
@.str.5 = [12 x i8] c"write error\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %36
7: ; preds = %0
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %14, %13
%16 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %7
%19 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %36
20: ; preds = %7
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%22 = call i32 @read(i32 %3, i8* %21, i32 100)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %34
26: ; preds = %20
%27 = call i32 @write(i32 %16, i8* %21, i32 %22)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %34
31: ; preds = %26
%32 = tail call i32 @close(i32 %3)
%33 = tail call i32 @close(i32 %16)
br label %34
34: ; preds = %31, %29, %24
%35 = phi i32 [ 3, %24 ], [ 4, %29 ], [ 0, %31 ]
br label %36
36: ; preds = %18, %34, %5
%37 = phi i32 [ 1, %5 ], [ 2, %18 ], [ %35, %34 ]
ret i32 %37
}