#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 BUFSIZ;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ chmod(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ fstat(int, struct stat *);
int open(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ send_errno(int /*<<< orphan*/);
int /*<<< orphan*/ send_success();
int write(int, char *, int);
int /*<<< orphan*/ x_perror(char *);
void do_cp(int argc, char **argv) {
  int srcf;
  assert(argc == 2);
  srcf = open(argv[0], O_RDONLY);
  if (srcf != -1) {
    int dstf = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (dstf != -1) {
      struct stat statb;
      int rd;
      if (fstat(srcf, &statb) == 0)
        chmod(argv[1], statb.st_mode);
      do {
        char buf[BUFSIZ];
        int wr;
        rd = read(srcf, buf, BUFSIZ);
        if (rd < 0)
          x_perror("copy-read");
        wr = write(dstf, buf, rd);
        if (wr != rd)
          x_perror("copy-write");
      } while (rd != 0);
      close(dstf);
      send_success();
    } else
      send_errno(errno);
    close(srcf);
  } else
    send_errno(errno);
}


x86 assembly (clang, -O0):
<do_cp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     130b <do_cp+0x16b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
or     (%rax),%esi
mov    0x2deb(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     12f0 <do_cp+0x150>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10c0 <fstat@plt>
cmp    $0x0,%rax
jne    1246 <do_cp+0xa6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x20(%rbp),%esi
call   1090 <chmod@plt>
jmp    124b <do_cp+0xab>
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x48(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%edx
call   1060 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12a1 <do_cp+0x101>
lea    0xd64(%rip),%rdi
call   1030 <x_perror@plt>
mov    -0x48(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     12cb <do_cp+0x12b>
lea    0xd44(%rip),%rdi
call   1030 <x_perror@plt>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
cmpl   $0x0,-0x24(%rbp)
jne    124b <do_cp+0xab>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   1080 <send_success@plt>
jmp    12fe <do_cp+0x15e>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
call   10b0 <send_errno@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
jmp    1319 <do_cp+0x179>
mov    0x2cae(%rip),%rax
mov    (%rax),%edi
call   10b0 <send_errno@plt>
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_cp>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
xor    %eax,%eax
cmp    $0x2,%edi
sete   %al
mov    %eax,%edi
call   1070 <assert@plt>
mov    (%r15),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
je     12b0 <do_cp+0x110>
mov    0x2dca(%rip),%rax
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%r15),%rdi
mov    0x2df4(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12cc <do_cp+0x12c>
mov    %eax,%r13d
lea    -0x30(%rbp),%rsi
mov    -0x2c(%rbp),%edi
call   10c0 <fstat@plt>
test   %rax,%rax
jne    1233 <do_cp+0x93>
mov    0x8(%r15),%rdi
mov    -0x30(%rbp),%esi
call   1090 <chmod@plt>
mov    0x2d8e(%rip),%r12
jmp    1247 <do_cp+0xa7>
nopl   0x0(%rax)
mov    %r14,%rsp
test   %ebx,%ebx
je     129f <do_cp+0xff>
mov    %rsp,%r14
mov    (%r12),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    (%r12),%edx
mov    -0x2c(%rbp),%edi
mov    %r15,%rsi
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1280 <do_cp+0xe0>
lea    0xd85(%rip),%rdi
call   1030 <x_perror@plt>
mov    %r13d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
cmp    %ebx,%eax
je     1240 <do_cp+0xa0>
lea    0xd72(%rip),%rdi
call   1030 <x_perror@plt>
jmp    1240 <do_cp+0xa0>
mov    %r13d,%edi
call   1050 <close@plt>
xor    %eax,%eax
call   1080 <send_success@plt>
jmp    12da <do_cp+0x13a>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <send_errno@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   10b0 <send_errno@plt>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@BUFSIZ = global i32 0
@.str = [10 x i8] c"copy-read\00"
@.str.1 = [11 x i8] c"copy-write\00"
@errno = global i32 0
define void @do_cp(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, 2
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i8**, i8*** %4
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDONLY
%20 = call i32 (i8*, i32, ...) @open(i8* %18, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %83
23: ; preds = %2
%24 = load i8**, i8*** %4
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_WRONLY
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = load i32, i32* @O_TRUNC
%31 = or i32 %29, %30
%32 = call i32 (i8*, i32, ...) @open(i8* %26, i32 %31, i32 438)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp ne i32 %33, -1
br i1 %34, label %35, label %77
35: ; preds = %23
%36 = load i32, i32* %5
%37 = call i64 @fstat(i32 %36, %struct.stat* %7)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %35
%40 = load i8**, i8*** %4
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @chmod(i8* %42, i32 %44)
br label %46
46: ; preds = %39, %35
br label %47
47: ; preds = %70, %46
%48 = load i32, i32* @BUFSIZ
%49 = zext i32 %48 to i64
%50 = call i8* @llvm.stacksave()
store i8* %50, i8** %9
%51 = alloca i8, i64 %49
store i64 %49, i64* %10
%52 = load i32, i32* %5
%53 = load i32, i32* @BUFSIZ
%54 = call i32 @read(i32 %52, i8* %51, i32 %53)
store i32 %54, i32* %8
%55 = load i32, i32* %8
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %47
%58 = call i32 @x_perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %59
59: ; preds = %57, %47
%60 = load i32, i32* %6
%61 = load i32, i32* %8
%62 = call i32 @write(i32 %60, i8* %51, i32 %61)
store i32 %62, i32* %11
%63 = load i32, i32* %11
%64 = load i32, i32* %8
%65 = icmp ne i32 %63, %64
br i1 %65, label %66, label %68
66: ; preds = %59
%67 = call i32 @x_perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %68
68: ; preds = %66, %59
%69 = load i8*, i8** %9
br label %70
70: ; preds = %68
%71 = load i32, i32* %8
%72 = icmp ne i32 %71, 0
br i1 %72, label %47, label %73
73: ; preds = %70
%74 = load i32, i32* %6
%75 = call i32 @close(i32 %74)
%76 = call i32 (...) @send_success()
br label %80
77: ; preds = %23
%78 = load i32, i32* @errno
%79 = call i32 @send_errno(i32 %78)
br label %80
80: ; preds = %77, %73
%81 = load i32, i32* %5
%82 = call i32 @close(i32 %81)
br label %86
83: ; preds = %2
%84 = load i32, i32* @errno
%85 = call i32 @send_errno(i32 %84)
br label %86
86: ; preds = %83, %80
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@BUFSIZ = global i32 0
@.str = [10 x i8] c"copy-read\00"
@.str.1 = [11 x i8] c"copy-write\00"
@errno = global i32 0
define void @do_cp(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp eq i32 %0, 2
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = load i8*, i8** %1
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 (i8*, i32, ...) @open(i8* %7, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %56, label %11
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%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* @O_TRUNC
%18 = or i32 %16, %17
%19 = tail call i32 (i8*, i32, ...) @open(i8* %13, i32 %18, i32 438)
%20 = icmp eq i32 %19, -1
br i1 %20, label %51, label %21
21: ; preds = %11
%22 = bitcast %struct.stat* %3 to i8*
%23 = call i64 @fstat(i32 %9, %struct.stat* %3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i8*, i8** %12
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = tail call i32 @chmod(i8* %26, i32 %28)
br label %30
30: ; preds = %25, %21
br label %31
31: ; preds = %30, %46
%32 = load i32, i32* @BUFSIZ
%33 = zext i32 %32 to i64
%34 = tail call i8* @llvm.stacksave()
%35 = alloca i8, i64 %33
%36 = load i32, i32* @BUFSIZ
%37 = call i32 @read(i32 %9, i8* %35, i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %31
%40 = tail call i32 @x_perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %41
41: ; preds = %39, %31
%42 = call i32 @write(i32 %19, i8* %35, i32 %37)
%43 = icmp eq i32 %42, %37
br i1 %43, label %46, label %44
44: ; preds = %41
%45 = tail call i32 @x_perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %46
46: ; preds = %44, %41
tail call void @llvm.stackrestore(i8* %34)
%47 = icmp eq i32 %37, 0
br i1 %47, label %48, label %31
48: ; preds = %46
%49 = tail call i32 @close(i32 %19)
%50 = tail call i32 (...) @send_success()
br label %54
51: ; preds = %11
%52 = load i32, i32* @errno
%53 = tail call i32 @send_errno(i32 %52)
br label %54
54: ; preds = %51, %48
%55 = tail call i32 @close(i32 %9)
br label %59
56: ; preds = %2
%57 = load i32, i32* @errno
%58 = tail call i32 @send_errno(i32 %57)
br label %59
59: ; preds = %56, %54
ret void
}