#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int fstat(int, struct stat *);
int open(char const *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *);
int read(int, char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char const *argv[]) {
  int returnCode = 0, sourcefd = -1, destfd = -1;
  if (argc < 2 || argc > 3) {
    printf("Usage: %s <source> <dest>[terminal]\n", argv[0]);
    returnCode = 1;
    goto ret;
  }
  sourcefd = open(argv[1], O_RDONLY);
  if (sourcefd == -1) {
    perror("Error opening source file");
    returnCode = 1;
    goto ret;
  }
  destfd = -1;
  if (argc == 3) {
    struct stat buf;
    int res = fstat(sourcefd, &buf);
    if (res != 0) {
      perror("Error reading source file permissions");
      returnCode = 1;
      goto ret;
    }
    destfd = open(argv[2], O_CREAT | O_EXCL | O_WRONLY, buf.st_mode);
    if (destfd == -1) {
      perror("Error opening destination file");
      returnCode = 1;
      goto ret;
    }
    dup2(destfd, STDOUT_FILENO);
  }
  char buffer[50];
  for (int size = read(sourcefd, buffer, 50); size != 0;
       size = read(sourcefd, buffer, 50)) {
    write(STDOUT_FILENO, buffer, size);
  }
ret:
  close(sourcefd);
  close(destfd);
  return returnCode;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11bf <main+0x3f>
cmpl   $0x3,-0x8(%rbp)
jle    11de <main+0x5e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe33(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x14(%rbp)
jmp    1311 <main+0x191>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    121b <main+0x9b>
lea    0xe1b(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x14(%rbp)
jmp    1311 <main+0x191>
movl   $0xffffffff,-0x1c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    12c0 <main+0x140>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10a0 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     125d <main+0xdd>
lea    0xdf3(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x14(%rbp)
jmp    1311 <main+0x191>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
mov    0x2d53(%rip),%rax
or     (%rax),%esi
mov    0x2d3a(%rip),%rax
or     (%rax),%esi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12af <main+0x12f>
lea    0xdc7(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x14(%rbp)
jmp    1311 <main+0x191>
mov    -0x1c(%rbp),%edi
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x32,%edx
call   1070 <read@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
je     130c <main+0x18c>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rsi
mov    -0x64(%rbp),%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x32,%edx
call   1070 <read@plt>
mov    %eax,-0x64(%rbp)
jmp    12d4 <main+0x154>
jmp    1311 <main+0x191>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    -0x4(%rbp),%eax
cmp    $0xfffffffd,%eax
ja     11bf <main+0x3f>
mov    (%r14),%rsi
lea    0xe5d(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%r12d
mov    $0x1,%r14d
mov    $0xffffffff,%r15d
jmp    1290 <main+0x110>
mov    0x8(%r14),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12b0 <main+0x130>
mov    %eax,%r12d
mov    $0xffffffff,%r15d
cmp    $0x3,%ebp
jne    1246 <main+0xc6>
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
call   10a0 <fstat@plt>
test   %eax,%eax
jne    12c1 <main+0x141>
mov    0x10(%r14),%rdi
mov    0x2dee(%rip),%rax
mov    0x2db7(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d9c(%rip),%rax
or     (%rax),%esi
mov    0x8(%rsp),%edx
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12ca <main+0x14a>
mov    %eax,%r15d
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1040 <dup2@plt>
lea    0x10(%rsp),%rsi
mov    %r12d,%edi
mov    $0x32,%edx
call   1070 <read@plt>
xor    %r14d,%r14d
test   %eax,%eax
je     1290 <main+0x110>
mov    0x2d82(%rip),%rbx
lea    0x10(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    (%rbx),%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x32,%edx
call   1070 <read@plt>
test   %eax,%eax
jne    1270 <main+0xf0>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6e(%rip),%rdi
call   1090 <perror@plt>
jmp    11a8 <main+0x28>
lea    0xd77(%rip),%rdi
jmp    12d1 <main+0x151>
lea    0xd94(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%r15d
mov    $0x1,%r14d
jmp    1290 <main+0x110>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [37 x i8] c"Usage: %s <source> <dest>[terminal]\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"Error opening source file\00"
@.str.2 = [38 x i8] c"Error reading source file permissions\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [31 x i8] c"Error opening destination file\00"
@STDOUT_FILENO = global i32 0
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 %struct.stat
%10 = alloca i32
%11 = alloca [50 x i8]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 -1, i32* %7
store i32 -1, i32* %8
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 3
br i1 %17, label %18, label %23
18: ; preds = %15, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 1, i32* %6
br label %80
23: ; preds = %15
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_RDONLY
%28 = call i32 (i8*, i32, ...) @open(i8* %26, i32 %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %6
br label %80
33: ; preds = %23
store i32 -1, i32* %8
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, 3
br i1 %35, label %36, label %63
36: ; preds = %33
%37 = load i32, i32* %7
%38 = call i32 @fstat(i32 %37, %struct.stat* %9)
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %6
br label %80
43: ; preds = %36
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 2
%46 = load i8*, i8** %45
%47 = load i32, i32* @O_CREAT
%48 = load i32, i32* @O_EXCL
%49 = or i32 %47, %48
%50 = load i32, i32* @O_WRONLY
%51 = or i32 %49, %50
%52 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 (i8*, i32, ...) @open(i8* %46, i32 %51, i32 %53)
store i32 %54, i32* %8
%55 = load i32, i32* %8
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %59
57: ; preds = %43
%58 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %6
br label %80
59: ; preds = %43
%60 = load i32, i32* %8
%61 = load i32, i32* @STDOUT_FILENO
%62 = call i32 @dup2(i32 %60, i32 %61)
br label %63
63: ; preds = %59, %33
%64 = load i32, i32* %7
%65 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%66 = call i32 @read(i32 %64, i8* %65, i32 50)
store i32 %66, i32* %12
br label %67
67: ; preds = %75, %63
%68 = load i32, i32* %12
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %79
70: ; preds = %67
%71 = load i32, i32* @STDOUT_FILENO
%72 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%73 = load i32, i32* %12
%74 = call i32 @write(i32 %71, i8* %72, i32 %73)
br label %75
75: ; preds = %70
%76 = load i32, i32* %7
%77 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%78 = call i32 @read(i32 %76, i8* %77, i32 50)
store i32 %78, i32* %12
br label %67
79: ; preds = %67
br label %80
80: ; preds = %79, %57, %41, %31, %18
%81 = load i32, i32* %7
%82 = call i32 @close(i32 %81)
%83 = load i32, i32* %8
%84 = call i32 @close(i32 %83)
%85 = load i32, i32* %6
ret i32 %85
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [37 x i8] c"Usage: %s <source> <dest>[terminal]\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"Error opening source file\00"
@.str.2 = [38 x i8] c"Error reading source file permissions\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [31 x i8] c"Error opening destination file\00"
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [50 x i8]
%5 = add i32 %0, -4
%6 = icmp ult i32 %5, -2
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %53
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %53
18: ; preds = %10
%19 = icmp eq i32 %0, 3
br i1 %19, label %20, label %42
20: ; preds = %18
%21 = bitcast %struct.stat* %3 to i8*
%22 = call i32 @fstat(i32 %14, %struct.stat* %3)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %36
24: ; preds = %20
%25 = getelementptr inbounds i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_CREAT
%28 = load i32, i32* @O_EXCL
%29 = or i32 %28, %27
%30 = load i32, i32* @O_WRONLY
%31 = or i32 %29, %30
%32 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%33 = load i32, i32* %32
%34 = tail call i32 (i8*, i32, ...) @open(i8* %26, i32 %31, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %24, %20
%37 = phi i8* [ getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), %20 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), %24 ]
%38 = tail call i32 @perror(i8* %37)
br label %53
39: ; preds = %24
%40 = load i32, i32* @STDOUT_FILENO
%41 = tail call i32 @dup2(i32 %34, i32 %40)
br label %42
42: ; preds = %39, %18
%43 = phi i32 [ %34, %39 ], [ -1, %18 ]
%44 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%45 = call i32 @read(i32 %14, i8* %44, i32 50)
%46 = icmp eq i32 %45, 0
br i1 %46, label %53, label %47
47: ; preds = %42, %47
%48 = phi i32 [ %51, %47 ], [ %45, %42 ]
%49 = load i32, i32* @STDOUT_FILENO
%50 = call i32 @write(i32 %49, i8* %44, i32 %48)
%51 = call i32 @read(i32 %14, i8* %44, i32 50)
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %47
53: ; preds = %47, %42, %36, %16, %7
%54 = phi i32 [ 1, %7 ], [ 1, %16 ], [ 1, %36 ], [ 0, %42 ], [ 0, %47 ]
%55 = phi i32 [ -1, %7 ], [ -1, %16 ], [ %14, %36 ], [ %14, %42 ], [ %14, %47 ]
%56 = phi i32 [ -1, %7 ], [ -1, %16 ], [ -1, %36 ], [ %43, %42 ], [ %43, %47 ]
%57 = tail call i32 @close(i32 %55)
%58 = tail call i32 @close(i32 %56)
ret i32 %54
}