#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
scalar_t__ S_ISLNK(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int creat(char const *, int /*<<< orphan*/);
scalar_t__ errno;
int lstat(char const *, struct stat *);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int readlink(char const *, char *, int);
int symlink(char *, char const *);
int write(int, char *, int);
int create_copy_file(const char *source, const char *target) {
  struct stat src_stat;
  if (lstat(source, &src_stat) == -1)
    return -1;
  if (S_ISLNK(src_stat.st_mode)) {
    char lnk[1024];
    int lnk_size;
    if ((lnk_size = readlink(source, lnk, 1023)) == -1)
      return -2;
    lnk[lnk_size] = '\0';
    if (symlink(lnk, target) == -1)
      return -3;
  } else if (S_ISREG(src_stat.st_mode)) {
    int src, dst;
    int rsize;
    char buf[1024];
    if ((src = open(source, O_RDONLY)) == -1) {
      close(dst);
      return -4;
    }
    if ((dst = creat(target, src_stat.st_mode)) == -1)
      return -5;
    while ((rsize = read(src, buf, 1024))) {
      if (rsize == -1 && errno == EINTR)
        continue;
      if (rsize == -1) {
        close(src);
        close(dst);
        return -6;
      }
      while (write(dst, buf, rsize) == -1)
        if (errno != EINTR) {
          close(src);
          close(dst);
          return -7;
        }
    }
    close(src);
    close(dst);
  } else {
    return -8;
  }
  return 0;
}


x86 assembly (clang, -O0):
<create_copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <lstat@plt>
cmp    $0xffffffff,%eax
jne    11d5 <create_copy_file+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e6 <create_copy_file+0x246>
mov    -0x20(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%rax
je     1250 <create_copy_file+0xb0>
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x3ff,%edx
call   1040 <readlink@plt>
mov    %eax,-0x424(%rbp)
cmp    $0xffffffff,%eax
jne    1217 <create_copy_file+0x77>
movl   $0xfffffffe,-0x4(%rbp)
jmp    13e6 <create_copy_file+0x246>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <symlink@plt>
cmp    $0xffffffff,%eax
jne    124b <create_copy_file+0xab>
movl   $0xfffffffd,-0x4(%rbp)
jmp    13e6 <create_copy_file+0x246>
jmp    13df <create_copy_file+0x23f>
mov    -0x20(%rbp),%edi
call   10a0 <S_ISREG@plt>
cmp    $0x0,%rax
je     13ce <create_copy_file+0x22e>
mov    -0x10(%rbp),%rdi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x428(%rbp)
cmp    $0xffffffff,%eax
jne    129a <create_copy_file+0xfa>
mov    -0x42c(%rbp),%edi
call   1070 <close@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    13e6 <create_copy_file+0x246>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   10c0 <creat@plt>
mov    %eax,-0x42c(%rbp)
cmp    $0xffffffff,%eax
jne    12c1 <create_copy_file+0x121>
movl   $0xfffffffb,-0x4(%rbp)
jmp    13e6 <create_copy_file+0x246>
jmp    12c6 <create_copy_file+0x126>
mov    -0x428(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x430(%rbp)
cmp    $0x0,%eax
je     13b3 <create_copy_file+0x213>
cmpl   $0xffffffff,-0x430(%rbp)
jne    1318 <create_copy_file+0x178>
mov    0x2cd8(%rip),%rax
mov    (%rax),%rax
mov    0x2cc6(%rip),%rcx
cmp    (%rcx),%rax
jne    1318 <create_copy_file+0x178>
jmp    12c6 <create_copy_file+0x126>
cmpl   $0xffffffff,-0x430(%rbp)
jne    1347 <create_copy_file+0x1a7>
mov    -0x428(%rbp),%edi
call   1070 <close@plt>
mov    -0x42c(%rbp),%edi
call   1070 <close@plt>
movl   $0xfffffffa,-0x4(%rbp)
jmp    13e6 <create_copy_file+0x246>
jmp    134c <create_copy_file+0x1ac>
mov    -0x42c(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    -0x430(%rbp),%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
jne    13ae <create_copy_file+0x20e>
mov    0x2c64(%rip),%rax
mov    (%rax),%rax
mov    0x2c52(%rip),%rcx
cmp    (%rcx),%rax
je     13a9 <create_copy_file+0x209>
mov    -0x428(%rbp),%edi
call   1070 <close@plt>
mov    -0x42c(%rbp),%edi
call   1070 <close@plt>
movl   $0xfffffff9,-0x4(%rbp)
jmp    13e6 <create_copy_file+0x246>
jmp    134c <create_copy_file+0x1ac>
jmp    12c6 <create_copy_file+0x126>
mov    -0x428(%rbp),%edi
call   1070 <close@plt>
mov    -0x42c(%rbp),%edi
call   1070 <close@plt>
jmp    13da <create_copy_file+0x23a>
movl   $0xfffffff8,-0x4(%rbp)
jmp    13e6 <create_copy_file+0x246>
jmp    13df <create_copy_file+0x23f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_copy_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0x8(%rsp),%rsi
call   1090 <lstat@plt>
cmp    $0xffffffff,%eax
je     1215 <create_copy_file+0x75>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1030 <S_ISLNK@plt>
test   %rax,%rax
je     121f <create_copy_file+0x7f>
lea    0x10(%rsp),%rsi
mov    %rbp,%rdi
mov    $0x3ff,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     12d1 <create_copy_file+0x131>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
mov    %r15,%rsi
call   1060 <symlink@plt>
cmp    $0xffffffff,%eax
je     12d8 <create_copy_file+0x138>
xor    %ebp,%ebp
jmp    12eb <create_copy_file+0x14b>
mov    $0xffffffff,%ebp
jmp    12eb <create_copy_file+0x14b>
mov    %ebx,%edi
call   10a0 <S_ISREG@plt>
test   %rax,%rax
je     12df <create_copy_file+0x13f>
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10b0 <open@plt>
mov    %eax,%r14d
mov    %r15,%rdi
mov    %ebx,%esi
call   10c0 <creat@plt>
cmp    $0xffffffff,%eax
je     12e6 <create_copy_file+0x146>
mov    %eax,%ebx
lea    0x10(%rsp),%r15
mov    0x2d74(%rip),%r12
mov    0x2d65(%rip),%r13
jmp    127b <create_copy_file+0xdb>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    12ff <create_copy_file+0x15f>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     126d <create_copy_file+0xcd>
mov    %eax,%ebp
test   %eax,%eax
je     1306 <create_copy_file+0x166>
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
jne    127b <create_copy_file+0xdb>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     12a0 <create_copy_file+0x100>
mov    $0xfffffff9,%ebp
mov    %r14d,%edi
call   1070 <close@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    12eb <create_copy_file+0x14b>
mov    $0xfffffffe,%ebp
jmp    12eb <create_copy_file+0x14b>
mov    $0xfffffffd,%ebp
jmp    12eb <create_copy_file+0x14b>
mov    $0xfffffff8,%ebp
jmp    12eb <create_copy_file+0x14b>
mov    $0xfffffffb,%ebp
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xfffffffa,%ebp
jmp    12c0 <create_copy_file+0x120>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    120e <create_copy_file+0x6e>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @create_copy_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [1024 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
%13 = load i8*, i8** %4
%14 = call i32 @lstat(i8* %13, %struct.stat* %6)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %106
17: ; preds = %2
%18 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i64 @S_ISLNK(i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %38
22: ; preds = %17
%23 = load i8*, i8** %4
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%25 = call i32 @readlink(i8* %23, i8* %24, i32 1023)
store i32 %25, i32* %8
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 -2, i32* %3
br label %106
28: ; preds = %22
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %30
store i8 0, i8* %31
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%33 = load i8*, i8** %5
%34 = call i32 @symlink(i8* %32, i8* %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %28
store i32 -3, i32* %3
br label %106
37: ; preds = %28
br label %105
38: ; preds = %17
%39 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i64 @S_ISREG(i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %103
43: ; preds = %38
%44 = load i8*, i8** %4
%45 = load i32, i32* @O_RDONLY
%46 = call i32 @open(i8* %44, i32 %45)
store i32 %46, i32* %9
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* %10
%50 = call i32 @close(i32 %49)
store i32 -4, i32* %3
br label %106
51: ; preds = %43
%52 = load i8*, i8** %5
%53 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @creat(i8* %52, i32 %54)
store i32 %55, i32* %10
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %58
57: ; preds = %51
store i32 -5, i32* %3
br label %106
58: ; preds = %51
br label %59
59: ; preds = %97, %71, %58
%60 = load i32, i32* %9
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%62 = call i32 @read(i32 %60, i8* %61, i32 1024)
store i32 %62, i32* %11
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %98
64: ; preds = %59
%65 = load i32, i32* %11
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %72
67: ; preds = %64
%68 = load i64, i64* @errno
%69 = load i64, i64* @EINTR
%70 = icmp eq i64 %68, %69
br i1 %70, label %71, label %72
71: ; preds = %67
br label %59
72: ; preds = %67, %64
%73 = load i32, i32* %11
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %80
75: ; preds = %72
%76 = load i32, i32* %9
%77 = call i32 @close(i32 %76)
%78 = load i32, i32* %10
%79 = call i32 @close(i32 %78)
store i32 -6, i32* %3
br label %106
80: ; preds = %72
br label %81
81: ; preds = %96, %80
%82 = load i32, i32* %10
%83 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%84 = load i32, i32* %11
%85 = call i32 @write(i32 %82, i8* %83, i32 %84)
%86 = icmp eq i32 %85, -1
br i1 %86, label %87, label %97
87: ; preds = %81
%88 = load i64, i64* @errno
%89 = load i64, i64* @EINTR
%90 = icmp ne i64 %88, %89
br i1 %90, label %91, label %96
91: ; preds = %87
%92 = load i32, i32* %9
%93 = call i32 @close(i32 %92)
%94 = load i32, i32* %10
%95 = call i32 @close(i32 %94)
store i32 -7, i32* %3
br label %106
96: ; preds = %87
br label %81
97: ; preds = %81
br label %59
98: ; preds = %59
%99 = load i32, i32* %9
%100 = call i32 @close(i32 %99)
%101 = load i32, i32* %10
%102 = call i32 @close(i32 %101)
br label %104
103: ; preds = %38
store i32 -8, i32* %3
br label %106
104: ; preds = %98
br label %105
105: ; preds = %104, %37
store i32 0, i32* %3
br label %106
106: ; preds = %105, %103, %91, %75, %57, %48, %36, %27, %16
%107 = load i32, i32* %3
ret i32 %107
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @create_copy_file(i8* nocapture %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = bitcast %struct.stat* %3 to i8*
%7 = call i32 @lstat(i8* %0, %struct.stat* %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %58, label %9
9: ; preds = %2
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i64 @S_ISLNK(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %24, label %14
14: ; preds = %9
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%16 = call i32 @readlink(i8* %0, i8* %15, i32 1023)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %14
br label %58
19: ; preds = %14
%20 = sext i32 %16 to i64
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %20
store i8 0, i8* %21
%22 = call i32 @symlink(i8* %15, i8* %1)
%23 = icmp eq i32 %22, -1
br i1 %23, label %58, label %57
24: ; preds = %9
%25 = tail call i64 @S_ISREG(i32 %11)
%26 = icmp eq i64 %25, 0
br i1 %26, label %58, label %27
27: ; preds = %24
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%29 = load i32, i32* @O_RDONLY
%30 = tail call i32 @open(i8* %0, i32 %29)
%31 = icmp ne i32 %30, -1
%32 = tail call i32 @creat(i8* %1, i32 %11)
%33 = icmp eq i32 %32, -1
br i1 %33, label %52, label %34
34: ; preds = %27, %40
%35 = call i32 @read(i32 %30, i8* %28, i32 1024)
switch i32 %35, label %41 [
i32 0, label %54
i32 -1, label %36
]
36: ; preds = %34
%37 = load i64, i64* @errno
%38 = load i64, i64* @EINTR
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %48
40: ; preds = %41, %36
br label %34
41: ; preds = %34, %44
%42 = call i32 @write(i32 %32, i8* %28, i32 %35)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %40
44: ; preds = %41
%45 = load i64, i64* @errno
%46 = load i64, i64* @EINTR
%47 = icmp eq i64 %45, %46
br i1 %47, label %41, label %48
48: ; preds = %36, %44
%49 = phi i32 [ -7, %44 ], [ -6, %36 ]
%50 = tail call i32 @close(i32 %30)
%51 = tail call i32 @close(i32 %32)
br label %52
52: ; preds = %48, %27
%53 = phi i32 [ -5, %27 ], [ %49, %48 ]
br label %58
54: ; preds = %34
%55 = tail call i32 @close(i32 %30)
%56 = tail call i32 @close(i32 %32)
br label %57
57: ; preds = %54, %19
br label %58
58: ; preds = %52, %18, %24, %2, %19, %57
%59 = phi i32 [ 0, %57 ], [ -3, %19 ], [ -1, %2 ], [ -8, %24 ], [ -2, %18 ], [ %53, %52 ]
ret i32 %59
}