#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 /*<<< orphan*/ c;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char **argv) {
  char c;
  int source_fd, dest_fd, read_size, write_size;
  if (argc != 3) {
    err(1, "Not enough ");
  }
  printf("%s\n", argv[1]);
  if ((source_fd = open(argv[1], O_RDONLY)) == -1) {
    err(1, "Can't open the source file.");
  }
  if ((dest_fd = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU)) == -1) {
    close(source_fd);
    err(1, "Can't create open dest file for writing.");
  }
  while ((read_size = read(source_fd, &c, sizeof(c))) > 0) {
    write_size = write(dest_fd, &c, read_size);
    if (write_size != read_size) {
      err(1, "Write to file failed");
      close(source_fd);
      close(dest_fd);
    }
  }
  close(source_fd);
  close(dest_fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1191 <main+0x31>
mov    $0x1,%edi
lea    0xe74(%rip),%rsi
call   1070 <err@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe6c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11da <main+0x7a>
mov    $0x1,%edi
lea    0xe3b(%rip),%rsi
call   1070 <err@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
or     (%rax),%esi
mov    0x2df5(%rip),%rax
or     (%rax),%esi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1232 <main+0xd2>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
lea    0xdff(%rip),%rsi
call   1070 <err@plt>
jmp    1237 <main+0xd7>
mov    -0x18(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    1298 <main+0x138>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%edx
lea    -0x11(%rbp),%rsi
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
je     1293 <main+0x133>
mov    $0x1,%edi
lea    0xdd7(%rip),%rsi
call   1070 <err@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
jmp    1237 <main+0xd7>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
cmp    $0x3,%edi
je     1185 <main+0x25>
lea    0xe85(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    0x8(%rbp),%rsi
lea    0xe7c(%rip),%rdi
call   1040 <printf@plt>
mov    0x8(%rbp),%rdi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    11c2 <main+0x62>
lea    0xe58(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    0x10(%rbp),%rdi
mov    0x2e2b(%rip),%rax
mov    0x2de4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e11(%rip),%rax
or     (%rax),%esi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
call   1080 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1211 <main+0xb1>
mov    %r14d,%edi
call   1050 <close@plt>
lea    0xe25(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
lea    0xf(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1284 <main+0x124>
mov    %eax,%ebx
lea    0xf(%rsp),%r12
lea    0xe20(%rip),%r15
jmp    1256 <main+0xf6>
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1284 <main+0x124>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     1240 <main+0xe0>
mov    $0x1,%edi
mov    %r15,%rsi
call   1070 <err@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1240 <main+0xe0>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"Not enough \00"
@.str.1 = [4 x i8] c"%s\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [28 x i8] c"Can't open the source file.\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@.str.3 = [41 x i8] c"Can't create open dest file for writing.\00"
@.str.4 = [21 x i8] c"Write to file failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = call i32 (i8*, i32, ...) @open(i8* %22, i32 %23)
store i32 %24, i32* %7
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = call i32 @err(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %15
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = load i32, i32* @O_CREAT
%33 = load i32, i32* @O_WRONLY
%34 = or i32 %32, %33
%35 = load i32, i32* @O_TRUNC
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IRWXU
%38 = call i32 (i8*, i32, ...) @open(i8* %31, i32 %36, i32 %37)
store i32 %38, i32* %8
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %28
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
%43 = call i32 @err(i32 1, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %40, %28
br label %45
45: ; preds = %62, %44
%46 = load i32, i32* %7
%47 = call i32 @read(i32 %46, i8* %6, i32 1)
store i32 %47, i32* %9
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %63
49: ; preds = %45
%50 = load i32, i32* %8
%51 = load i32, i32* %9
%52 = call i32 @write(i32 %50, i8* %6, i32 %51)
store i32 %52, i32* %10
%53 = load i32, i32* %10
%54 = load i32, i32* %9
%55 = icmp ne i32 %53, %54
br i1 %55, label %56, label %62
56: ; preds = %49
%57 = call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%58 = load i32, i32* %7
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %8
%61 = call i32 @close(i32 %60)
br label %62
62: ; preds = %56, %49
br label %45
63: ; preds = %45
%64 = load i32, i32* %7
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [12 x i8] c"Not enough \00"
@.str.1 = [4 x i8] c"%s\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [28 x i8] c"Can't open the source file.\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@.str.3 = [41 x i8] c"Can't create open dest file for writing.\00"
@.str.4 = [21 x i8] c"Write to file failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = icmp eq i32 %0, 3
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = load i8*, i8** %8
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 (i8*, i32, ...) @open(i8* %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %7
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_CREAT
%21 = load i32, i32* @O_WRONLY
%22 = or i32 %21, %20
%23 = load i32, i32* @O_TRUNC
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IRWXU
%26 = tail call i32 (i8*, i32, ...) @open(i8* %19, i32 %24, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = tail call i32 @close(i32 %13)
%30 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %28, %17
%32 = call i32 @read(i32 %13, i8* %3, i32 1)
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %45
34: ; preds = %31, %42
%35 = phi i32 [ %43, %42 ], [ %32, %31 ]
%36 = call i32 @write(i32 %26, i8* %3, i32 %35)
%37 = icmp eq i32 %36, %35
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%40 = tail call i32 @close(i32 %13)
%41 = tail call i32 @close(i32 %26)
br label %42
42: ; preds = %38, %34
%43 = call i32 @read(i32 %13, i8* %3, i32 1)
%44 = icmp sgt i32 %43, 0
br i1 %44, label %34, label %45
45: ; preds = %42, %31
%46 = tail call i32 @close(i32 %13)
%47 = tail call i32 @close(i32 %26)
ret i32 0
}