#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*/ close(int);
int open(char const *const, int, ...);
int read(int, char *, int);
scalar_t__ stat(char const *const, struct stat *);
int /*<<< orphan*/ unlink(char const *const);
int /*<<< orphan*/ warn(char *, char const *const);
int write(int, char *, int);
int cp(const char *const spath, const char *const dpath) {
  char buf[1024];
  int s, d, l;
  struct stat st;
  if (stat(spath, &st)) {
    warn("%s", spath);
    return 1;
  }
  if ((s = open(spath, O_RDONLY)) < 0) {
    warn("%s", spath);
    return 1;
  }
  if ((d = open(dpath, O_CREAT | O_EXCL | O_WRONLY, st.st_mode)) < 0) {
    warn("%s", dpath);
    close(s);
    return 1;
  }
  while ((l = read(s, buf, 512)) > 0) {
    if (l < 0) {
      warn("%s", spath);
      goto cp_err;
    }
    l = write(d, buf, l);
    if (l < 0) {
      warn("%s", dpath);
      goto cp_err;
    }
  }
cp_err:
  close(s);
  close(d);
  if (l < 0) {
    unlink(dpath);
    return 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<cp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     11b9 <cp+0x49>
mov    -0x10(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   1040 <warn@plt>
movl   $0x1,-0x4(%rbp)
jmp    1330 <cp+0x1c0>
mov    -0x10(%rbp),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
jge    11f8 <cp+0x88>
mov    -0x10(%rbp),%rsi
lea    0xe19(%rip),%rdi
call   1040 <warn@plt>
movl   $0x1,-0x4(%rbp)
jmp    1330 <cp+0x1c0>
mov    -0x18(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    0x2dc4(%rip),%rax
or     (%rax),%esi
mov    0x2dab(%rip),%rax
or     (%rax),%esi
mov    -0x430(%rbp),%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x428(%rbp)
cmp    $0x0,%eax
jge    125a <cp+0xea>
mov    -0x18(%rbp),%rsi
lea    0xdc2(%rip),%rdi
call   1040 <warn@plt>
mov    -0x424(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1330 <cp+0x1c0>
jmp    125f <cp+0xef>
mov    -0x424(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %eax,-0x42c(%rbp)
cmp    $0x0,%eax
jle    12ec <cp+0x17c>
cmpl   $0x0,-0x42c(%rbp)
jge    12a7 <cp+0x137>
mov    -0x10(%rbp),%rsi
lea    0xd63(%rip),%rdi
call   1040 <warn@plt>
jmp    12f1 <cp+0x181>
mov    -0x428(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x42c(%rbp),%edx
call   1050 <write@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0x0,-0x42c(%rbp)
jge    12e7 <cp+0x177>
mov    -0x18(%rbp),%rsi
lea    0xd23(%rip),%rdi
call   1040 <warn@plt>
jmp    12f1 <cp+0x181>
jmp    125f <cp+0xef>
jmp    12f1 <cp+0x181>
mov    -0x424(%rbp),%edi
call   1060 <close@plt>
mov    -0x428(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x42c(%rbp)
jge    1329 <cp+0x1b9>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x2,-0x4(%rbp)
jmp    1330 <cp+0x1c0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
je     11b8 <cp+0x48>
lea    0xe65(%rip),%rdi
mov    %rbx,%rsi
call   1040 <warn@plt>
mov    $0x1,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     1194 <cp+0x24>
mov    %eax,%r15d
mov    0x2e1f(%rip),%rax
mov    0x2df0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd5(%rip),%rax
or     (%rax),%esi
mov    0x8(%rsp),%edx
mov    %r14,%rdi
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     1274 <cp+0x104>
mov    %eax,%r12d
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1248 <cp+0xd8>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <write@plt>
test   %eax,%eax
jns    1210 <cp+0xa0>
mov    %eax,%ebp
lea    0xdc0(%rip),%rdi
mov    %r14,%rsi
call   1040 <warn@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r12d,%edi
call   1060 <close@plt>
xor    %eax,%eax
test   %ebp,%ebp
jns    11a8 <cp+0x38>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    $0x2,%eax
jmp    11a8 <cp+0x38>
lea    0xd85(%rip),%rdi
mov    %r14,%rsi
call   1040 <warn@plt>
mov    %r15d,%edi
call   1060 <close@plt>
jmp    11a3 <cp+0x33>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"%s\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
define i32 @cp(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = call i64 @stat(i8* %11, %struct.stat* %10)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %76
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = load i32, i32* @O_RDONLY
%20 = call i32 (i8*, i32, ...) @open(i8* %18, i32 %19)
store i32 %20, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i8*, i8** %4
%24 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %23)
store i32 1, i32* %3
br label %76
25: ; preds = %17
%26 = load i8*, i8** %5
%27 = load i32, i32* @O_CREAT
%28 = load i32, i32* @O_EXCL
%29 = or i32 %27, %28
%30 = load i32, i32* @O_WRONLY
%31 = or i32 %29, %30
%32 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 (i8*, i32, ...) @open(i8* %26, i32 %31, i32 %33)
store i32 %34, i32* %8
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %25
%37 = load i8*, i8** %5
%38 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %37)
%39 = load i32, i32* %7
%40 = call i32 @close(i32 %39)
store i32 1, i32* %3
br label %76
41: ; preds = %25
br label %42
42: ; preds = %63, %41
%43 = load i32, i32* %7
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 512)
store i32 %45, i32* %9
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %64
47: ; preds = %42
%48 = load i32, i32* %9
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i8*, i8** %4
%52 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %51)
br label %65
53: ; preds = %47
%54 = load i32, i32* %8
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%56 = load i32, i32* %9
%57 = call i32 @write(i32 %54, i8* %55, i32 %56)
store i32 %57, i32* %9
%58 = load i32, i32* %9
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %53
%61 = load i8*, i8** %5
%62 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %61)
br label %65
63: ; preds = %53
br label %42
64: ; preds = %42
br label %65
65: ; preds = %64, %60, %50
%66 = load i32, i32* %7
%67 = call i32 @close(i32 %66)
%68 = load i32, i32* %8
%69 = call i32 @close(i32 %68)
%70 = load i32, i32* %9
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %65
%73 = load i8*, i8** %5
%74 = call i32 @unlink(i8* %73)
store i32 2, i32* %3
br label %76
75: ; preds = %65
store i32 0, i32* %3
br label %76
76: ; preds = %75, %72, %36, %22, %14
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"%s\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
define i32 @cp(i8* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = call i64 @stat(i8* %0, %struct.stat* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %45
11: ; preds = %2
%12 = load i32, i32* @O_RDONLY
%13 = call i32 (i8*, i32, ...) @open(i8* %0, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %45
17: ; preds = %11
%18 = load i32, i32* @O_CREAT
%19 = load i32, i32* @O_EXCL
%20 = or i32 %19, %18
%21 = load i32, i32* @O_WRONLY
%22 = or i32 %20, %21
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 (i8*, i32, ...) @open(i8* %1, i32 %22, i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1)
%29 = call i32 @close(i32 %13)
br label %45
30: ; preds = %17, %33
%31 = call i32 @read(i32 %13, i8* %5, i32 512)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = call i32 @write(i32 %25, i8* %5, i32 %31)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %30
36: ; preds = %33
%37 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %38
38: ; preds = %30, %36
%39 = phi i32 [ %34, %36 ], [ %31, %30 ]
%40 = call i32 @close(i32 %13)
%41 = call i32 @close(i32 %25)
%42 = icmp slt i32 %39, 0
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @unlink(i8* %1)
br label %45
45: ; preds = %38, %43, %27, %15, %9
%46 = phi i32 [ 1, %9 ], [ 1, %15 ], [ 1, %27 ], [ 2, %43 ], [ 0, %38 ]
ret i32 %46
}