#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 dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ EINTR;
int EraseObject(char *, int);
scalar_t__ errno;
int readlink(char *, char *, int);
scalar_t__ symlink(char *, char *);
int CopyLink(char *sourceP, char *targetP, int repl, struct stat *statP) {
  int l, rc;
  char buf[1024];
  do {
    errno = 0;
    l = readlink(sourceP, buf, sizeof(buf));
  } while (l < 0 && errno == EINTR);
  if (l < 0)
    return errno;
  buf[l] = 0;
  if (symlink(buf, targetP) == 0)
    return 0;
  else if (errno != EEXIST || !repl)
    return errno;
  if ((rc = EraseObject(targetP, repl)) != 0)
    return rc;
  if (symlink(buf, targetP) == 0)
    return 0;
  else
    return errno;
}


x86 assembly (clang, -O0):
<CopyLink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0x2e87(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0x431(%rbp)
jge    119f <CopyLink+0x6f>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x431(%rbp)
mov    -0x431(%rbp),%al
test   $0x1,%al
jne    114a <CopyLink+0x1a>
cmpl   $0x0,-0x2c(%rbp)
jge    11c9 <CopyLink+0x99>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    128c <CopyLink+0x15c>
movslq -0x2c(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <symlink@plt>
cmp    $0x0,%rax
jne    11fb <CopyLink+0xcb>
movl   $0x0,-0x4(%rbp)
jmp    128c <CopyLink+0x15c>
mov    0x2dd6(%rip),%rax
mov    (%rax),%rax
mov    0x2dd4(%rip),%rcx
cmp    (%rcx),%rax
jne    121f <CopyLink+0xef>
cmpl   $0x0,-0x1c(%rbp)
jne    1231 <CopyLink+0x101>
mov    0x2db2(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    128c <CopyLink+0x15c>
jmp    1236 <CopyLink+0x106>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <EraseObject@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
je     1259 <CopyLink+0x129>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    128c <CopyLink+0x15c>
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <symlink@plt>
cmp    $0x0,%rax
jne    127f <CopyLink+0x14f>
movl   $0x0,-0x4(%rbp)
jmp    128c <CopyLink+0x15c>
mov    0x2d52(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CopyLink>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
mov    0x2e87(%rip),%r12
mov    %rsp,%r13
mov    0x2e75(%rip),%rbx
nopl   0x0(%rax,%rax,1)
movq   $0x0,(%r12)
mov    %rbp,%rdi
mov    %r13,%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    %eax,%ecx
mov    (%r12),%rax
test   %ecx,%ecx
jns    1187 <CopyLink+0x57>
cmp    (%rbx),%rax
je     1160 <CopyLink+0x30>
test   %ecx,%ecx
js     11e6 <CopyLink+0xb6>
mov    %ecx,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
mov    %r15,%rsi
call   1050 <symlink@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     11e6 <CopyLink+0xb6>
mov    (%r12),%rax
test   %r14d,%r14d
je     11e6 <CopyLink+0xb6>
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
jne    11e6 <CopyLink+0xb6>
mov    %r15,%rdi
mov    %r14d,%esi
call   1040 <EraseObject@plt>
test   %eax,%eax
jne    11e6 <CopyLink+0xb6>
mov    %rsp,%rdi
mov    %r15,%rsi
call   1050 <symlink@plt>
mov    %rax,%rcx
mov    $0x0,%eax
test   %rcx,%rcx
je     11e6 <CopyLink+0xb6>
mov    (%r12),%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@EEXIST = global i64 0
define i32 @CopyLink(i8* %0, i8* %1, i32 %2, %struct.stat* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.stat*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [1024 x i8]
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store %struct.stat* %3, %struct.stat** %9
br label %13
13: ; preds = %24, %4
store i64 0, i64* @errno
%14 = load i8*, i8** %6
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%16 = call i32 @readlink(i8* %14, i8* %15, i32 1024)
store i32 %16, i32* %10
br label %17
17: ; preds = %13
%18 = load i32, i32* %10
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br label %24
24: ; preds = %20, %17
%25 = phi i1 [ false, %17 ], [ %23, %20 ]
br i1 %25, label %13, label %26
26: ; preds = %24
%27 = load i32, i32* %10
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i64, i64* @errno
%31 = trunc i64 %30 to i32
store i32 %31, i32* %5
br label %68
32: ; preds = %26
%33 = load i32, i32* %10
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 %34
store i8 0, i8* %35
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%37 = load i8*, i8** %7
%38 = call i64 @symlink(i8* %36, i8* %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %32
store i32 0, i32* %5
br label %68
41: ; preds = %32
%42 = load i64, i64* @errno
%43 = load i64, i64* @EEXIST
%44 = icmp ne i64 %42, %43
br i1 %44, label %48, label %45
45: ; preds = %41
%46 = load i32, i32* %8
%47 = icmp ne i32 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %45, %41
%49 = load i64, i64* @errno
%50 = trunc i64 %49 to i32
store i32 %50, i32* %5
br label %68
51: ; preds = %45
br label %52
52: ; preds = %51
%53 = load i8*, i8** %7
%54 = load i32, i32* %8
%55 = call i32 @EraseObject(i8* %53, i32 %54)
store i32 %55, i32* %11
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = load i32, i32* %11
store i32 %58, i32* %5
br label %68
59: ; preds = %52
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%61 = load i8*, i8** %7
%62 = call i64 @symlink(i8* %60, i8* %61)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %59
store i32 0, i32* %5
br label %68
65: ; preds = %59
%66 = load i64, i64* @errno
%67 = trunc i64 %66 to i32
store i32 %67, i32* %5
br label %68
68: ; preds = %65, %64, %57, %48, %40, %29
%69 = load i32, i32* %5
ret i32 %69
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@EEXIST = global i64 0
define i32 @CopyLink(i8* nocapture %0, i8* %1, i32 %2, %struct.stat* nocapture readnone %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
br label %7
7: ; preds = %7, %4
store i64 0, i64* @errno
%8 = call i32 @readlink(i8* %0, i8* %6, i32 1024)
%9 = icmp slt i32 %8, 0
%10 = load i64, i64* @errno
%11 = load i64, i64* @EINTR
%12 = icmp eq i64 %10, %11
%13 = select i1 %9, i1 %12, i1 false
br i1 %13, label %7, label %14
14: ; preds = %7
br i1 %9, label %15, label %17
15: ; preds = %14
%16 = trunc i64 %10 to i32
br label %39
17: ; preds = %14
%18 = zext i32 %8 to i64
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %18
store i8 0, i8* %19
%20 = call i64 @symlink(i8* %6, i8* %1)
%21 = icmp eq i64 %20, 0
br i1 %21, label %39, label %22
22: ; preds = %17
%23 = load i64, i64* @errno
%24 = load i64, i64* @EEXIST
%25 = icmp eq i64 %23, %24
%26 = icmp ne i32 %2, 0
%27 = and i1 %26, %25
br i1 %27, label %30, label %28
28: ; preds = %22
%29 = trunc i64 %23 to i32
br label %39
30: ; preds = %22
%31 = call i32 @EraseObject(i8* %1, i32 %2)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = call i64 @symlink(i8* %6, i8* %1)
%35 = icmp eq i64 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = load i64, i64* @errno
%38 = trunc i64 %37 to i32
br label %39
39: ; preds = %33, %30, %17, %36, %28, %15
%40 = phi i32 [ %16, %15 ], [ %38, %36 ], [ %29, %28 ], [ 0, %17 ], [ %31, %30 ], [ 0, %33 ]
ret i32 %40
}