#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_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *, char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ usage();
int /*<<< orphan*/ write(int, char *, int);
void main(int argc, char *argv[]) {
  int fd_src;
  int fd_dst;
  if (argc != 3) {
    usage();
  }
  fd_src = open(argv[1], O_RDONLY);
  fd_dst =
      open(argv[2], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  while (true) {
    char buffer[512] = {0};
    int count = 0;
    count = read(fd_src, buffer, 512);
    write(fd_dst, buffer, count);
    if (count < 512) {
      close(fd_src);
      close(fd_dst);
      break;
    }
  }
  printf("file copied: %s -> %s.\n", argv[1], argv[2]);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x4(%rbp)
je     1193 <main+0x23>
mov    $0x0,%al
call   1080 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
mov    0x2e00(%rip),%rax
or     (%rax),%edx
mov    0x2de7(%rip),%rax
or     (%rax),%edx
mov    0x2dd6(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
movl   $0x0,-0x224(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %eax,-0x224(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x224(%rbp),%edx
call   1030 <write@plt>
cmpl   $0x200,-0x224(%rbp)
jge    1267 <main+0xf7>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    126c <main+0xfc>
jmp    11f6 <main+0x86>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xd7d(%rip),%rdi
call   1040 <printf@plt>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
je     1190 <main+0x20>
xor    %eax,%eax
call   1080 <usage@plt>
mov    0x8(%r14),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1090 <open@plt>
mov    0x2dfd(%rip),%rcx
mov    0x2e46(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2dfb(%rip),%rcx
mov    0x2e14(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %eax,%r15d
mov    0x2df6(%rip),%rax
or     (%rax),%edx
mov    0x10(%r14),%rdi
mov    0x2de1(%rip),%rax
or     (%rax),%edx
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%r13d
mov    %rsp,%r12
mov    $0x200,%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %eax,%ebp
mov    %r13d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x200,%ebp
jl     1266 <main+0xf6>
mov    %rsp,%rbp
nopw   0x0(%rax,%rax,1)
mov    $0x200,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %eax,%ebx
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x1ff,%ebx
jg     1230 <main+0xc0>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r13d,%edi
call   1060 <close@plt>
mov    0x8(%r14),%rsi
mov    0x10(%r14),%rdx
lea    0xd7b(%rip),%rdi
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [24 x i8] c"file copied: %s -> %s.\0A\00"
define void @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca [512 x i8]
%8 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%9 = load i32, i32* %3
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (...) @usage()
br label %13
13: ; preds = %11, %2
%14 = load i8**, i8*** %4
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = call i32 (i8*, i32, ...) @open(i8* %16, i32 %17)
store i32 %18, i32* %5
%19 = load i8**, i8*** %4
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_WRONLY
%23 = load i32, i32* @O_CREAT
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IRUSR
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRGRP
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IROTH
%31 = or i32 %29, %30
%32 = call i32 (i8*, i32, ...) @open(i8* %21, i32 %24, i32 %31)
store i32 %32, i32* %6
br label %33
33: ; preds = %13, %49
%34 = bitcast [512 x i8]* %7 to i8*
store i32 0, i32* %8
%35 = load i32, i32* %5
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%37 = call i32 @read(i32 %35, i8* %36, i32 512)
store i32 %37, i32* %8
%38 = load i32, i32* %6
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%40 = load i32, i32* %8
%41 = call i32 @write(i32 %38, i8* %39, i32 %40)
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 512
br i1 %43, label %44, label %49
44: ; preds = %33
%45 = load i32, i32* %5
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
br label %50
49: ; preds = %33
br label %33
50: ; preds = %44
%51 = load i8**, i8*** %4
%52 = getelementptr inbounds i8*, i8** %51, i64 1
%53 = load i8*, i8** %52
%54 = load i8**, i8*** %4
%55 = getelementptr inbounds i8*, i8** %54, i64 2
%56 = load i8*, i8** %55
%57 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %53, i8* %56)
ret void
}

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