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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int src, dest, bwritten;
  char *srcname, *destname, buffer[512];
  if (argc < 3) {
    printf("Program needs two arguments\n");
    exit(0);
  }
  srcname = argv[1];
  destname = argv[2];
  src = open(srcname, O_RDONLY, 0);
  if (src == -1) {
    printf("Cannot open file %s for reading\n", srcname);
    exit(0);
  }
  dest = open(destname, O_WRONLY, 0);
  if (dest == -1) {
    printf("Cannot open file %s for writing\n", destname);
    exit(0);
  }
  while ((bwritten = read(src, buffer, sizeof(buffer))) > 0) {
    write(dest, buffer, bwritten);
  }
  close(src);
  close(dest);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1198 <main+0x38>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ea <main+0x8a>
mov    -0x28(%rbp),%rsi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1224 <main+0xc4>
mov    -0x30(%rbp),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    1229 <main+0xc9>
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1260 <main+0x100>
mov    -0x18(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
jmp    1229 <main+0xc9>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
cmp    $0x2,%edi
jle    122f <main+0xbf>
mov    0x8(%rsi),%rbx
mov    0x10(%rsi),%r14
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1242 <main+0xd2>
mov    %eax,%r15d
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %edx,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     124e <main+0xde>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x200,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    1210 <main+0xa0>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1070 <read@plt>
test   %eax,%eax
jg     11f0 <main+0x80>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0c(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xdb7(%rip),%rdi
mov    %rbx,%rsi
jmp    1258 <main+0xe8>
lea    0xdcc(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [33 x i8] c"Cannot open file %s for reading\0A\00"
@O_WRONLY = global i32 0
@.str.2 = [33 x i8] c"Cannot open file %s for writing\0A\00"
@str = [28 x i8] c"Program needs two arguments\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %11, i32 %14, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %9
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%19 = tail call i32 @exit(i32 0)
unreachable
20: ; preds = %9
%21 = load i32, i32* @O_WRONLY
%22 = tail call i32 @open(i8* %13, i32 %21, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = call i32 @read(i32 %15, i8* %4, i32 512)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %30, label %35
27: ; preds = %20
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%29 = tail call i32 @exit(i32 0)
unreachable
30: ; preds = %24, %30
%31 = phi i32 [ %33, %30 ], [ %25, %24 ]
%32 = call i32 @write(i32 %22, i8* %4, i32 %31)
%33 = call i32 @read(i32 %15, i8* %4, i32 512)
%34 = icmp sgt i32 %33, 0
br i1 %34, label %30, label %35
35: ; preds = %30, %24
%36 = tail call i32 @close(i32 %15)
%37 = tail call i32 @close(i32 %22)
ret i32 0
}