#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 {
  scalar_t__ st_dev;
  scalar_t__ st_ino;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ mode_t;
typedef scalar_t__ ino_t;
typedef scalar_t__ dev_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ do_cp(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char const *, int, ...);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
void cp(const char *dest, const char *src) {
  dev_t dev;
  ino_t ino;
  int srcfd;
  int destfd;
  struct stat st;
  mode_t mode;
  if (stat(src, &st) == -1) {
    fprintf(stderr, "cp: cannot stat %s\n", src);
    exit(EXIT_FAILURE);
  }
  if (S_ISDIR(st.st_mode)) {
    fprintf(stderr, "cp: %s is not a regular file\n", src);
    exit(EXIT_FAILURE);
  }
  dev = st.st_dev;
  ino = st.st_ino;
  mode = st.st_mode;
  if (stat(dest, &st) != -1) {
    if (!S_ISREG(st.st_mode)) {
      fprintf(stderr, "cp: cannot truncate %s\n", dest);
      exit(EXIT_FAILURE);
    }
    if ((st.st_dev == dev) && (st.st_ino == ino)) {
      fprintf(stderr, "cp: cannot copy a file onto itself\n");
      exit(EXIT_FAILURE);
    }
  }
  srcfd = open(src, O_RDONLY);
  if (srcfd == -1) {
    fprintf(stderr, "cp: cannot open %s\n", src);
    exit(EXIT_FAILURE);
  }
  destfd = open(dest, O_WRONLY | O_TRUNC | O_CREAT, mode);
  if (destfd == -1) {
    fprintf(stderr, "cp: cannot open %s\n", dest);
    exit(EXIT_FAILURE);
  }
  do_cp(srcfd, destfd);
  close(destfd);
  close(srcfd);
}


x86 assembly (clang, -O0):
<cp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11cf <cp+0x4f>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     120a <cp+0x8a>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     12b6 <cp+0x136>
mov    -0x30(%rbp),%edi
call   1080 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1270 <cp+0xf0>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    12b1 <cp+0x131>
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    12b1 <cp+0x131>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    12b6 <cp+0x136>
mov    -0x10(%rbp),%rdi
mov    0x2d17(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1300 <cp+0x180>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd83(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    0x2ca5(%rip),%rax
mov    (%rax),%esi
mov    0x2cdc(%rip),%rax
or     (%rax),%esi
mov    0x2cdb(%rip),%rax
or     (%rax),%esi
mov    -0x44(%rbp),%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    135f <cp+0x1df>
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd24(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2c68(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1050 <do_cp@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     1266 <cp+0xe6>
mov    0x18(%rsp),%ebp
mov    %ebp,%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
jne    1278 <cp+0xf8>
mov    0x8(%rsp),%rbx
mov    0x10(%rsp),%r12
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     11fb <cp+0x7b>
mov    0x18(%rsp),%edi
call   1080 <S_ISREG@plt>
test   %eax,%eax
je     12b1 <cp+0x131>
cmp    %rbx,0x8(%rsp)
jne    11fb <cp+0x7b>
cmp    %r12,0x10(%rsp)
je     12d9 <cp+0x159>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     128a <cp+0x10a>
mov    %eax,%ebx
mov    0x2d94(%rip),%rax
mov    0x2dcd(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dca(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     129f <cp+0x11f>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %eax,%esi
call   1050 <do_cp@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%edi
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xd8a(%rip),%rsi
jmp    129a <cp+0x11a>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    0xd8c(%rip),%rsi
jmp    129a <cp+0x11a>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
mov    %r15,%rdx
jmp    12c4 <cp+0x144>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
jmp    12c1 <cp+0x141>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
lea    0xd71(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
lea    0xd61(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12cb <cp+0x14b>

LLVM IR (-O0):
%struct.stat = type { i64, i64, i32 }
@stderr = global i32 0
@.str = [20 x i8] c"cp: cannot stat %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [30 x i8] c"cp: %s is not a regular file\0A\00"
@.str.2 = [24 x i8] c"cp: cannot truncate %s\0A\00"
@.str.3 = [36 x i8] c"cp: cannot copy a file onto itself\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [20 x i8] c"cp: cannot open %s\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define void @cp(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.stat
%10 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%11 = load i8*, i8** %4
%12 = call i32 @stat(i8* %11, %struct.stat* %9)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %4
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 2
%22 = load i32, i32* %21
%23 = call i64 @S_ISDIR(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %4
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %20
%32 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%33 = load i64, i64* %32
store i64 %33, i64* %5
%34 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%35 = load i64, i64* %34
store i64 %35, i64* %6
%36 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 2
%37 = load i32, i32* %36
store i32 %37, i32* %10
%38 = load i8*, i8** %3
%39 = call i32 @stat(i8* %38, %struct.stat* %9)
%40 = icmp ne i32 %39, -1
br i1 %40, label %41, label %68
41: ; preds = %31
%42 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 2
%43 = load i32, i32* %42
%44 = call i32 @S_ISREG(i32 %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %52, label %46
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %3
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %41
%53 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%54 = load i64, i64* %53
%55 = load i64, i64* %5
%56 = icmp eq i64 %54, %55
br i1 %56, label %57, label %67
57: ; preds = %52
%58 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%59 = load i64, i64* %58
%60 = load i64, i64* %6
%61 = icmp eq i64 %59, %60
br i1 %61, label %62, label %67
62: ; preds = %57
%63 = load i32, i32* @stderr
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
67: ; preds = %57, %52
br label %68
68: ; preds = %67, %31
%69 = load i8*, i8** %4
%70 = load i32, i32* @O_RDONLY
%71 = call i32 (i8*, i32, ...) @open(i8* %69, i32 %70)
store i32 %71, i32* %7
%72 = load i32, i32* %7
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %80
74: ; preds = %68
%75 = load i32, i32* @stderr
%76 = load i8*, i8** %4
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %76)
%78 = load i32, i32* @EXIT_FAILURE
%79 = call i32 @exit(i32 %78)
unreachable
80: ; preds = %68
%81 = load i8*, i8** %3
%82 = load i32, i32* @O_WRONLY
%83 = load i32, i32* @O_TRUNC
%84 = or i32 %82, %83
%85 = load i32, i32* @O_CREAT
%86 = or i32 %84, %85
%87 = load i32, i32* %10
%88 = call i32 (i8*, i32, ...) @open(i8* %81, i32 %86, i32 %87)
store i32 %88, i32* %8
%89 = load i32, i32* %8
%90 = icmp eq i32 %89, -1
br i1 %90, label %91, label %97
91: ; preds = %80
%92 = load i32, i32* @stderr
%93 = load i8*, i8** %3
%94 = call i32 (i32, i8*, ...) @fprintf(i32 %92, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %93)
%95 = load i32, i32* @EXIT_FAILURE
%96 = call i32 @exit(i32 %95)
unreachable
97: ; preds = %80
%98 = load i32, i32* %7
%99 = load i32, i32* %8
%100 = call i32 @do_cp(i32 %98, i32 %99)
%101 = load i32, i32* %8
%102 = call i32 @close(i32 %101)
%103 = load i32, i32* %7
%104 = call i32 @close(i32 %103)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64, i64, i32 }
@stderr = global i32 0
@.str = [20 x i8] c"cp: cannot stat %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [30 x i8] c"cp: %s is not a regular file\0A\00"
@.str.2 = [24 x i8] c"cp: cannot truncate %s\0A\00"
@.str.3 = [36 x i8] c"cp: cannot copy a file onto itself\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [20 x i8] c"cp: cannot open %s\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define void @cp(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @stat(i8* %1, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %1)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%14 = load i32, i32* %13
%15 = tail call i64 @S_ISDIR(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %12
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%24 = load i64, i64* %23
%25 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%26 = load i64, i64* %25
%27 = call i32 @stat(i8* %0, %struct.stat* %3)
%28 = icmp eq i32 %27, -1
br i1 %28, label %49, label %29
29: ; preds = %22
%30 = load i32, i32* %13
%31 = tail call i32 @S_ISREG(i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%36 = load i32, i32* @EXIT_FAILURE
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %29
%39 = load i64, i64* %23
%40 = icmp eq i64 %39, %24
%41 = load i64, i64* %25
%42 = icmp eq i64 %41, %26
%43 = select i1 %40, i1 %42, i1 false
br i1 %43, label %44, label %49
44: ; preds = %38
%45 = load i32, i32* @stderr
%46 = tail call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = tail call i32 @exit(i32 %47)
unreachable
49: ; preds = %38, %22
%50 = load i32, i32* @O_RDONLY
%51 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %58
53: ; preds = %49
%54 = load i32, i32* @stderr
%55 = tail call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %1)
%56 = load i32, i32* @EXIT_FAILURE
%57 = tail call i32 @exit(i32 %56)
unreachable
58: ; preds = %49
%59 = load i32, i32* @O_WRONLY
%60 = load i32, i32* @O_TRUNC
%61 = or i32 %60, %59
%62 = load i32, i32* @O_CREAT
%63 = or i32 %61, %62
%64 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %63, i32 %14)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %71
66: ; preds = %58
%67 = load i32, i32* @stderr
%68 = tail call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%69 = load i32, i32* @EXIT_FAILURE
%70 = tail call i32 @exit(i32 %69)
unreachable
71: ; preds = %58
%72 = tail call i32 @do_cp(i32 %51, i32 %64)
%73 = tail call i32 @close(i32 %64)
%74 = tail call i32 @close(i32 %51)
ret void
}