#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 /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int S_IROTH;
int S_IRWXG;
int S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int write(int, char *, int);
int main() {
  char *src_file = "src_file";
  char *dest_file = "copied_file.txt";
  int dest_fd, src_fd, read_byte, write_byte;
  char read_buf[4096];
  dest_fd = open(dest_file, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IROTH);
  if (dest_fd < 0) {
    perror("\nError opening the destination file");
    exit(1);
  } else {
    fprintf(stderr, "\nSuccessfully opened the destination file..");
  }
  src_fd = open(src_file, O_RDONLY);
  if (src_fd < 0) {
    perror("\nError opening the source file");
    exit(1);
  } else {
    fprintf(stderr, "Successfully opened the source file.");
  }
  while (1) {
    read_byte = read(src_fd, read_buf, 4096);
    if (read_byte == 0) {
      fprintf(stdout, "Reached the EOF for src file");
      break;
    }
    write_byte = write(dest_fd, read_buf, 4096);
    if (write_byte < 0) {
      perror("Error writing file");
      exit(1);
    }
  }
  close(src_fd);
  close(dest_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe75(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    0x2e4c(%rip),%rax
or     (%rax),%esi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    0x2e1a(%rip),%rax
or     (%rax),%edx
mov    0x2df1(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f3 <main+0x83>
lea    0xe35(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    123f <main+0xcf>
lea    0xe39(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1259 <main+0xe9>
mov    -0x20(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1294 <main+0x124>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12d0 <main+0x160>
mov    -0x1c(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    12cb <main+0x15b>
lea    0xe0e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1259 <main+0xe9>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    0x2e1e(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4c(%rip),%rax
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e12(%rip),%rax
or     (%rax),%edx
lea    0xe5a(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     127f <main+0x10f>
mov    %eax,%r14d
mov    0x2df8(%rip),%rbx
mov    (%rbx),%edi
lea    0xe6c(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0xe1a(%rip),%rdi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     1288 <main+0x118>
mov    %eax,%ebp
mov    (%rbx),%edi
lea    0xe88(%rip),%rsi
call   1060 <fprintf@plt>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
je     124d <main+0xdd>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    1210 <main+0xa0>
lea    0xe8c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd93(%rip),%rdi
jmp    123e <main+0xce>
lea    0xdda(%rip),%rdi
jmp    123e <main+0xce>

LLVM IR (-O0):
@.str = [9 x i8] c"src_file\00"
@.str.1 = [16 x i8] c"copied_file.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IROTH = global i32 0
@.str.2 = [36 x i8] c"\0AError opening the destination file\00"
@stderr = global i32 0
@.str.3 = [44 x i8] c"\0ASuccessfully opened the destination file..\00"
@O_RDONLY = global i32 0
@.str.4 = [31 x i8] c"\0AError opening the source file\00"
@.str.5 = [37 x i8] c"Successfully opened the source file.\00"
@stdout = global i32 0
@.str.6 = [29 x i8] c"Reached the EOF for src file\00"
@.str.7 = [19 x i8] c"Error writing file\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [4096 x i8]
store i32 0, i32* %1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %2
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8** %3
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRWXU
%14 = load i32, i32* @S_IRWXG
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IROTH
%17 = or i32 %15, %16
%18 = call i32 (i8*, i32, ...) @open(i8* %9, i32 %12, i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %0
%22 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %0
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %24
%28 = load i8*, i8** %2
%29 = load i32, i32* @O_RDONLY
%30 = call i32 (i8*, i32, ...) @open(i8* %28, i32 %29)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0))
br label %39
39: ; preds = %36
br label %40
40: ; preds = %39, %58
%41 = load i32, i32* %5
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 4096)
store i32 %43, i32* %6
%44 = load i32, i32* %6
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i32, i32* @stdout
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %59
49: ; preds = %40
%50 = load i32, i32* %4
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%52 = call i32 @write(i32 %50, i8* %51, i32 4096)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %49
%56 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %49
br label %40
59: ; preds = %46
%60 = load i32, i32* %5
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* %4
%63 = call i32 @close(i32 %62)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"src_file\00"
@.str.1 = [16 x i8] c"copied_file.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IROTH = global i32 0
@.str.2 = [36 x i8] c"\0AError opening the destination file\00"
@stderr = global i32 0
@.str.3 = [44 x i8] c"\0ASuccessfully opened the destination file..\00"
@O_RDONLY = global i32 0
@.str.4 = [31 x i8] c"\0AError opening the source file\00"
@.str.5 = [37 x i8] c"Successfully opened the source file.\00"
@stdout = global i32 0
@.str.6 = [29 x i8] c"Reached the EOF for src file\00"
@.str.7 = [19 x i8] c"Error writing file\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRWXU
%7 = load i32, i32* @S_IRWXG
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IROTH
%10 = or i32 %8, %9
%11 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %5, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%19 = load i32, i32* @O_RDONLY
%20 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0))
br label %28
28: ; preds = %36, %25
%29 = call i32 @read(i32 %20, i8* %2, i32 4096)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i32, i32* @stdout
%33 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
%34 = tail call i32 @close(i32 %20)
%35 = tail call i32 @close(i32 %11)
ret i32 0
36: ; preds = %28
%37 = call i32 @write(i32 %11, i8* %2, i32 4096)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %28
39: ; preds = %36
%40 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%41 = tail call i32 @exit(i32 1)
unreachable
}