#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;
};

/* Variables and functions */
int /*<<< orphan*/ AT_FDCWD;
int /*<<< orphan*/ AT_SYMLINK_FOLLOW;
int /*<<< orphan*/ AT_SYMLINK_NOFOLLOW;
scalar_t__ EEXIST;
scalar_t__ ENOENT;
scalar_t__ errno;
scalar_t__ fstatat(int, char const *, struct stat *, int /*<<< orphan*/);
scalar_t__ linkat(int /*<<< orphan*/, char const *, int, char const *,
                  int /*<<< orphan*/);
scalar_t__ symlinkat(char const *, int, char const *);
scalar_t__ unlinkat(int, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *, char const *);
bool createLink(const char *target, int dirFd, const char *linkName,
                const char *linkPath, bool force, bool symbolic, bool logical) {
  struct stat st;
  bool fileExists = true;
  if (fstatat(dirFd, linkName, &st, AT_SYMLINK_NOFOLLOW) < 0) {
    if (errno != ENOENT) {
      warn("stat: '%s'", linkPath);
      return false;
    }
    fileExists = false;
  }
  if (fileExists) {
    if (!force) {
      errno = EEXIST;
      warn("cannot create link '%s'", linkPath);
      return false;
    }
    if (unlinkat(dirFd, linkName, 0) < 0) {
      warn("unlink: '%s'", linkPath);
      return false;
    }
  }
  if (symbolic) {
    if (symlinkat(target, dirFd, linkName) < 0) {
      warn("symlink: '%s'", linkPath);
      return false;
    }
  } else {
    if (linkat(AT_FDCWD, target, dirFd, linkName,
               logical ? AT_SYMLINK_FOLLOW : 0) < 0) {
      warn("link: '%s'", linkPath);
      return false;
    }
  }
  return true;
}


x86 assembly (clang, -O0):
<createLink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    %r9d,-0x30(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2e41(%rip),%rax
mov    (%rax),%ecx
lea    -0x38(%rbp),%rdx
call   1070 <fstatat@plt>
cmp    $0x0,%rax
jge    11d9 <createLink+0x89>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%rax
je     11d2 <createLink+0x82>
mov    -0x28(%rbp),%rsi
lea    0xe3f(%rip),%rdi
call   1030 <warn@plt>
movl   $0x0,-0x4(%rbp)
jmp    132b <createLink+0x1db>
movl   $0x0,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     1256 <createLink+0x106>
cmpl   $0x0,-0x2c(%rbp)
jne    121d <createLink+0xcd>
mov    0x2de4(%rip),%rax
mov    (%rax),%rcx
mov    0x2dd2(%rip),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rsi
lea    0xdff(%rip),%rdi
call   1030 <warn@plt>
movl   $0x0,-0x4(%rbp)
jmp    132b <createLink+0x1db>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1040 <unlinkat@plt>
cmp    $0x0,%rax
jge    1251 <createLink+0x101>
mov    -0x28(%rbp),%rsi
lea    0xde3(%rip),%rdi
call   1030 <warn@plt>
movl   $0x0,-0x4(%rbp)
jmp    132b <createLink+0x1db>
jmp    1256 <createLink+0x106>
cmpl   $0x0,-0x30(%rbp)
je     129b <createLink+0x14b>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1060 <symlinkat@plt>
cmp    $0x0,%rax
jge    1296 <createLink+0x146>
mov    -0x28(%rbp),%rsi
lea    0xdab(%rip),%rdi
call   1030 <warn@plt>
movl   $0x0,-0x4(%rbp)
jmp    132b <createLink+0x1db>
jmp    1324 <createLink+0x1d4>
mov    0x2d0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpl   $0x0,0x10(%rbp)
je     12d8 <createLink+0x188>
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
jmp    12e2 <createLink+0x192>
xor    %eax,%eax
mov    %eax,-0x60(%rbp)
jmp    12e2 <createLink+0x192>
mov    -0x48(%rbp),%rcx
mov    -0x4c(%rbp),%edx
mov    -0x58(%rbp),%rsi
mov    -0x5c(%rbp),%edi
mov    -0x60(%rbp),%r8d
call   1050 <linkat@plt>
cmp    $0x0,%rax
jge    131f <createLink+0x1cf>
mov    -0x28(%rbp),%rsi
lea    0xd25(%rip),%rdi
call   1030 <warn@plt>
movl   $0x0,-0x4(%rbp)
jmp    132b <createLink+0x1db>
jmp    1324 <createLink+0x1d4>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createLink>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r12d
mov    %r8d,%ebx
mov    %rcx,%r14
mov    %rdx,%r13
mov    %esi,%ebp
mov    %rdi,%r15
mov    0x2e55(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %esi,%edi
mov    %r13,%rsi
call   1070 <fstatat@plt>
test   %rax,%rax
js     11a8 <createLink+0x58>
test   %ebx,%ebx
je     11e6 <createLink+0x96>
mov    %ebp,%edi
mov    %r13,%rsi
xor    %edx,%edx
call   1040 <unlinkat@plt>
test   %rax,%rax
jns    11be <createLink+0x6e>
lea    0xe80(%rip),%rdi
jmp    124a <createLink+0xfa>
mov    0x2e21(%rip),%rax
mov    (%rax),%rax
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%rax
jne    1203 <createLink+0xb3>
test   %r12d,%r12d
je     120c <createLink+0xbc>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r13,%rdx
call   1060 <symlinkat@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jns    1254 <createLink+0x104>
lea    0xe4c(%rip),%rdi
jmp    124a <createLink+0xfa>
mov    0x2deb(%rip),%rax
mov    (%rax),%rax
mov    0x2dd9(%rip),%rcx
mov    %rax,(%rcx)
lea    0xe0a(%rip),%rdi
jmp    124a <createLink+0xfa>
lea    0xdf6(%rip),%rdi
jmp    124a <createLink+0xfa>
mov    0x40(%rsp),%r8d
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
test   %r8d,%r8d
je     1229 <createLink+0xd9>
mov    0x2dc2(%rip),%rax
mov    (%rax),%r8d
mov    %r15,%rsi
mov    %ebp,%edx
mov    %r13,%rcx
call   1050 <linkat@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jns    1254 <createLink+0x104>
lea    0xde9(%rip),%rdi
mov    %r14,%rsi
call   1030 <warn@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@AT_SYMLINK_NOFOLLOW = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str = [11 x i8] c"stat: '%s'\00"
@EEXIST = global i64 0
@.str.1 = [24 x i8] c"cannot create link '%s'\00"
@.str.2 = [13 x i8] c"unlink: '%s'\00"
@.str.3 = [14 x i8] c"symlink: '%s'\00"
@AT_FDCWD = global i32 0
@AT_SYMLINK_FOLLOW = global i32 0
@.str.4 = [11 x i8] c"link: '%s'\00"
define i32 @createLink(i8* %0, i32 %1, i8* %2, i8* %3, i32 %4, i32 %5, i32 %6) {
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.stat
%17 = alloca i32
store i8* %0, i8** %9
store i32 %1, i32* %10
store i8* %2, i8** %11
store i8* %3, i8** %12
store i32 %4, i32* %13
store i32 %5, i32* %14
store i32 %6, i32* %15
store i32 1, i32* %17
%18 = load i32, i32* %10
%19 = load i8*, i8** %11
%20 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%21 = call i64 @fstatat(i32 %18, i8* %19, %struct.stat* %16, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %7
%24 = load i64, i64* @errno
%25 = load i64, i64* @ENOENT
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i8*, i8** %12
%29 = call i32 @warn(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %28)
store i32 0, i32* %8
br label %82
30: ; preds = %23
store i32 0, i32* %17
br label %31
31: ; preds = %30, %7
%32 = load i32, i32* %17
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %50
34: ; preds = %31
%35 = load i32, i32* %13
%36 = icmp ne i32 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %34
%38 = load i64, i64* @EEXIST
store i64 %38, i64* @errno
%39 = load i8*, i8** %12
%40 = call i32 @warn(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %39)
store i32 0, i32* %8
br label %82
41: ; preds = %34
%42 = load i32, i32* %10
%43 = load i8*, i8** %11
%44 = call i64 @unlinkat(i32 %42, i8* %43, i32 0)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = load i8*, i8** %12
%48 = call i32 @warn(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %47)
store i32 0, i32* %8
br label %82
49: ; preds = %41
br label %50
50: ; preds = %49, %31
%51 = load i32, i32* %14
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %63
53: ; preds = %50
%54 = load i8*, i8** %9
%55 = load i32, i32* %10
%56 = load i8*, i8** %11
%57 = call i64 @symlinkat(i8* %54, i32 %55, i8* %56)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %53
%60 = load i8*, i8** %12
%61 = call i32 @warn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %60)
store i32 0, i32* %8
br label %82
62: ; preds = %53
br label %81
63: ; preds = %50
%64 = load i32, i32* @AT_FDCWD
%65 = load i8*, i8** %9
%66 = load i32, i32* %10
%67 = load i8*, i8** %11
%68 = load i32, i32* %15
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %63
%71 = load i32, i32* @AT_SYMLINK_FOLLOW
br label %73
72: ; preds = %63
br label %73
73: ; preds = %72, %70
%74 = phi i32 [ %71, %70 ], [ 0, %72 ]
%75 = call i64 @linkat(i32 %64, i8* %65, i32 %66, i8* %67, i32 %74)
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %73
%78 = load i8*, i8** %12
%79 = call i32 @warn(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %78)
store i32 0, i32* %8
br label %82
80: ; preds = %73
br label %81
81: ; preds = %80, %62
store i32 1, i32* %8
br label %82
82: ; preds = %81, %77, %59, %46, %37, %27
%83 = load i32, i32* %8
ret i32 %83
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@AT_SYMLINK_NOFOLLOW = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str = [11 x i8] c"stat: '%s'\00"
@EEXIST = global i64 0
@.str.1 = [24 x i8] c"cannot create link '%s'\00"
@.str.2 = [13 x i8] c"unlink: '%s'\00"
@.str.3 = [14 x i8] c"symlink: '%s'\00"
@AT_FDCWD = global i32 0
@AT_SYMLINK_FOLLOW = global i32 0
@.str.4 = [11 x i8] c"link: '%s'\00"
define i32 @createLink(i8* %0, i32 %1, i8* %2, i8* %3, i32 %4, i32 %5, i32 %6) {
%8 = alloca %struct.stat
%9 = bitcast %struct.stat* %8 to i8*
%10 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%11 = call i64 @fstatat(i32 %1, i8* %2, %struct.stat* %8, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %7
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp eq i64 %14, %15
br i1 %16, label %24, label %36
17: ; preds = %7
%18 = icmp eq i32 %4, 0
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = load i64, i64* @EEXIST
store i64 %20, i64* @errno
br label %36
21: ; preds = %17
%22 = call i64 @unlinkat(i32 %1, i8* %2, i32 0)
%23 = icmp slt i64 %22, 0
br i1 %23, label %36, label %24
24: ; preds = %13, %21
%25 = icmp eq i32 %5, 0
br i1 %25, label %29, label %26
26: ; preds = %24
%27 = call i64 @symlinkat(i8* %0, i32 %1, i8* %2)
%28 = icmp slt i64 %27, 0
br i1 %28, label %36, label %39
29: ; preds = %24
%30 = load i32, i32* @AT_FDCWD
%31 = icmp eq i32 %6, 0
%32 = load i32, i32* @AT_SYMLINK_FOLLOW
%33 = select i1 %31, i32 0, i32 %32
%34 = call i64 @linkat(i32 %30, i8* %0, i32 %1, i8* %2, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %29, %26, %21, %13, %19
%37 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), %19 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %13 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), %21 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), %26 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), %29 ]
%38 = call i32 @warn(i8* %37, i8* %3)
br label %39
39: ; preds = %36, %26, %29
%40 = phi i32 [ 1, %29 ], [ 1, %26 ], [ 0, %36 ]
ret i32 %40
}