#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 /*<<< orphan*/ O_RDONLY;
int creat(char *, int);
int /*<<< orphan*/ merror(char *, ...);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int f1, f2, n;
  char buf[1024];
  if (argc != 3)
    merror("Usage: prog: from to");
  if ((f1 = open(argv[1], O_RDONLY, 0)) == -1)
    merror("prog: can't open %s", argv[1]);
  if ((f2 = creat(argv[2], 0666)) == -1)
    merror("prog: can't create %s, mode %03o", argv[2], 0666);
  while ((n = read(f1, buf, 1024)) > 0)
    if (write(f2, buf, n) != n)
      merror("prog: write error on file %s", argv[2]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1181 <main+0x31>
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1070 <merror@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11bb <main+0x6b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1070 <merror@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    $0x1b6,%esi
call   1060 <creat@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11f4 <main+0xa4>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xe41(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1070 <merror@plt>
jmp    11f9 <main+0xa9>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    124f <main+0xff>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x1c(%rbp),%eax
je     124a <main+0xfa>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1070 <merror@plt>
jmp    11f9 <main+0xa9>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
je     1177 <main+0x27>
lea    0xe90(%rip),%rdi
xor    %eax,%eax
call   1070 <merror@plt>
mov    0x8(%r14),%rdi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    11a5 <main+0x55>
mov    0x8(%r14),%rsi
lea    0xe77(%rip),%rdi
xor    %eax,%eax
call   1070 <merror@plt>
mov    0x10(%r14),%rdi
mov    $0x1b6,%esi
call   1060 <creat@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    11d2 <main+0x82>
mov    0x10(%r14),%rsi
lea    0xe63(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1070 <merror@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1237 <main+0xe7>
mov    %eax,%ebx
mov    %rsp,%rbp
lea    0xe58(%rip),%r12
jmp    1216 <main+0xc6>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1237 <main+0xe7>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     1200 <main+0xb0>
mov    0x10(%r14),%rsi
mov    %r12,%rdi
xor    %eax,%eax
call   1070 <merror@plt>
jmp    1200 <main+0xb0>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Usage: prog: from to\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"prog: can't open %s\00"
@.str.2 = [33 x i8] c"prog: can't create %s, mode %03o\00"
@.str.3 = [29 x i8] c"prog: write error on file %s\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 [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @merror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18, i32 0)
store i32 %19, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @merror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %21, %14
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @creat(i8* %29, i32 438)
store i32 %30, i32* %7
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %37
32: ; preds = %26
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i32 (i8*, ...) @merror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %35, i32 438)
br label %37
37: ; preds = %32, %26
br label %38
38: ; preds = %55, %37
%39 = load i32, i32* %6
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%41 = call i32 @read(i32 %39, i8* %40, i32 1024)
store i32 %41, i32* %8
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %56
43: ; preds = %38
%44 = load i32, i32* %7
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%46 = load i32, i32* %8
%47 = call i32 @write(i32 %44, i8* %45, i32 %46)
%48 = load i32, i32* %8
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %55
50: ; preds = %43
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 2
%53 = load i8*, i8** %52
%54 = call i32 (i8*, ...) @merror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %50, %43
br label %38
56: ; preds = %38
ret i32 0
}

LLVM IR (-O2):
@.str = [21 x i8] c"Usage: prog: from to\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"prog: can't open %s\00"
@.str.2 = [33 x i8] c"prog: can't create %s, mode %03o\00"
@.str.3 = [29 x i8] c"prog: write error on file %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 3
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @merror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = load i8*, i8** %9
%16 = tail call i32 (i8*, ...) @merror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %14, %8
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i32 @creat(i8* %19, i32 438)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i8*, i8** %18
%24 = tail call i32 (i8*, ...) @merror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %23, i32 438)
br label %25
25: ; preds = %22, %17
%26 = call i32 @read(i32 %12, i8* %4, i32 1024)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %38
28: ; preds = %25, %35
%29 = phi i32 [ %36, %35 ], [ %26, %25 ]
%30 = call i32 @write(i32 %20, i8* %4, i32 %29)
%31 = icmp eq i32 %30, %29
br i1 %31, label %35, label %32
32: ; preds = %28
%33 = load i8*, i8** %18
%34 = tail call i32 (i8*, ...) @merror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %32, %28
%36 = call i32 @read(i32 %12, i8* %4, i32 1024)
%37 = icmp sgt i32 %36, 0
br i1 %37, label %28, label %38
38: ; preds = %35, %25
ret i32 0
}