#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 */
scalar_t__ ENOENT;
int _POSIX_PATH_MAX;
int compare_stat(struct stat *, struct stat *);
scalar_t__ errno;
int /*<<< orphan*/ *getcwd(char *, int);
int stat(char const *, struct stat *);
int /*<<< orphan*/ strcat(char *, char const *);
int strlen(char const *);
int symlink(char const *, char const *);
int unlink(char const *);
int safe_symlink(const char *oldpath, const char *newpath) {
  struct stat osb, nsb;
  if (!oldpath || !newpath)
    return -1;
  if (unlink(newpath) == -1 && errno != ENOENT)
    return -1;
  if (oldpath[0] == '/') {
    if (symlink(oldpath, newpath) == -1)
      return -1;
  } else {
    char abs_oldpath[_POSIX_PATH_MAX];
    if ((getcwd(abs_oldpath, sizeof abs_oldpath) == NULL) ||
        (strlen(abs_oldpath) + 1 + strlen(oldpath) + 1 > sizeof abs_oldpath))
      return -1;
    strcat(abs_oldpath, "/");
    strcat(abs_oldpath, oldpath);
    if (symlink(abs_oldpath, newpath) == -1)
      return -1;
  }
  if (stat(oldpath, &osb) == -1 || stat(newpath, &nsb) == -1 ||
      compare_stat(&osb, &nsb) == -1) {
    unlink(newpath);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<safe_symlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1196 <safe_symlink+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    11a2 <safe_symlink+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    1362 <safe_symlink+0x1f2>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    11da <safe_symlink+0x6a>
mov    0x2e1d(%rip),%rax
mov    (%rax),%rax
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%rax
je     11da <safe_symlink+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1362 <safe_symlink+0x1f2>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1211 <safe_symlink+0xa1>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <symlink@plt>
cmp    $0xffffffff,%eax
jne    120c <safe_symlink+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1362 <safe_symlink+0x1f2>
jmp    1304 <safe_symlink+0x194>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    %eax,%esi
call   1060 <getcwd@plt>
cmp    $0x0,%rax
je     1283 <safe_symlink+0x113>
mov    -0x48(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rcx
mov    %eax,%edx
mov    -0x54(%rbp),%eax
add    %edx,%eax
add    $0x1,%eax
cltq
cmp    %rcx,%rax
jbe    1296 <safe_symlink+0x126>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12e3 <safe_symlink+0x173>
mov    -0x48(%rbp),%rdi
lea    0xd5f(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <symlink@plt>
cmp    $0xffffffff,%eax
jne    12dc <safe_symlink+0x16c>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12e3 <safe_symlink+0x173>
movl   $0x0,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x3c(%rbp),%eax
test   %eax,%eax
je     12ff <safe_symlink+0x18f>
jmp    12fa <safe_symlink+0x18a>
jmp    1362 <safe_symlink+0x1f2>
jmp    1304 <safe_symlink+0x194>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     1346 <safe_symlink+0x1d6>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     1346 <safe_symlink+0x1d6>
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <compare_stat@plt>
cmp    $0xffffffff,%eax
jne    135b <safe_symlink+0x1eb>
mov    -0x18(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1362 <safe_symlink+0x1f2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_symlink>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     12b0 <safe_symlink+0x140>
mov    %rsi,%r15
test   %rsi,%rsi
je     12b0 <safe_symlink+0x140>
mov    %rdi,%rbx
mov    %r15,%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    11c6 <safe_symlink+0x56>
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%rax
jne    12b0 <safe_symlink+0x140>
cmpb   $0x2f,(%rbx)
jne    11e4 <safe_symlink+0x74>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <symlink@plt>
cmp    $0xffffffff,%eax
jne    126b <safe_symlink+0xfb>
jmp    12b0 <safe_symlink+0x140>
mov    %rsp,-0x30(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%r12
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %r12,%rdi
mov    %r13d,%esi
call   1060 <getcwd@plt>
test   %rax,%rax
je     1237 <safe_symlink+0xc7>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rcx
add    %ecx,%eax
add    $0x2,%eax
cltq
cmp    %r13,%rax
jbe    123d <safe_symlink+0xcd>
mov    -0x30(%rbp),%rsp
jmp    12b0 <safe_symlink+0x140>
lea    0xdbc(%rip),%rsi
mov    %r12,%rdi
call   1090 <strcat@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <symlink@plt>
cmp    $0xffffffff,%eax
mov    -0x30(%rbp),%rsp
je     12b0 <safe_symlink+0x140>
lea    -0x40(%rbp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     12a2 <safe_symlink+0x132>
lea    -0x38(%rbp),%rsi
mov    %r15,%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     12a2 <safe_symlink+0x132>
lea    -0x40(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <compare_stat@plt>
xor    %r14d,%r14d
cmp    $0xffffffff,%eax
jne    12b0 <safe_symlink+0x140>
mov    %r15,%rdi
call   1030 <unlink@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%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
@ENOENT = global i64 0
@_POSIX_PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
define i32 @safe_symlink(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca %struct.stat
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %13, %2
store i32 -1, i32* %3
br label %85
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = call i32 @unlink(i8* %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i64, i64* @errno
%23 = load i64, i64* @ENOENT
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1, i32* %3
br label %85
26: ; preds = %21, %17
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 47
br i1 %31, label %32, label %39
32: ; preds = %26
%33 = load i8*, i8** %4
%34 = load i8*, i8** %5
%35 = call i32 @symlink(i8* %33, i8* %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 -1, i32* %3
br label %85
38: ; preds = %32
br label %70
39: ; preds = %26
%40 = load i32, i32* @_POSIX_PATH_MAX
%41 = zext i32 %40 to i64
%42 = call i8* @llvm.stacksave()
store i8* %42, i8** %8
%43 = alloca i8, i64 %41
store i64 %41, i64* %9
%44 = trunc i64 %41 to i32
%45 = call i32* @getcwd(i8* %43, i32 %44)
%46 = icmp eq i32* %45, null
br i1 %46, label %56, label %47
47: ; preds = %39
%48 = call i32 @strlen(i8* %43)
%49 = add nsw i32 %48, 1
%50 = load i8*, i8** %4
%51 = call i32 @strlen(i8* %50)
%52 = add nsw i32 %49, %51
%53 = add nsw i32 %52, 1
%54 = sext i32 %53 to i64
%55 = icmp ugt i64 %54, %41
br i1 %55, label %56, label %57
56: ; preds = %47, %39
store i32 -1, i32* %3
store i32 1, i32* %10
br label %66
57: ; preds = %47
%58 = call i32 @strcat(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%59 = load i8*, i8** %4
%60 = call i32 @strcat(i8* %43, i8* %59)
%61 = load i8*, i8** %5
%62 = call i32 @symlink(i8* %43, i8* %61)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %65
64: ; preds = %57
store i32 -1, i32* %3
store i32 1, i32* %10
br label %66
65: ; preds = %57
store i32 0, i32* %10
br label %66
66: ; preds = %65, %64, %56
%67 = load i8*, i8** %8
%68 = load i32, i32* %10
switch i32 %68, label %87 [
i32 0, label %69
i32 1, label %85
]
69: ; preds = %66
br label %70
70: ; preds = %69, %38
%71 = load i8*, i8** %4
%72 = call i32 @stat(i8* %71, %struct.stat* %6)
%73 = icmp eq i32 %72, -1
br i1 %73, label %81, label %74
74: ; preds = %70
%75 = load i8*, i8** %5
%76 = call i32 @stat(i8* %75, %struct.stat* %7)
%77 = icmp eq i32 %76, -1
br i1 %77, label %81, label %78
78: ; preds = %74
%79 = call i32 @compare_stat(%struct.stat* %6, %struct.stat* %7)
%80 = icmp eq i32 %79, -1
br i1 %80, label %81, label %84
81: ; preds = %78, %74, %70
%82 = load i8*, i8** %5
%83 = call i32 @unlink(i8* %82)
store i32 -1, i32* %3
br label %85
84: ; preds = %78
store i32 0, i32* %3
br label %85
85: ; preds = %84, %81, %66, %37, %25, %16
%86 = load i32, i32* %3
ret i32 %86
87: ; preds = %66
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@_POSIX_PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
define i32 @safe_symlink(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp ne i8* %0, null
%8 = icmp ne i8* %1, null
%9 = and i1 %7, %8
br i1 %9, label %10, label %55
10: ; preds = %2
%11 = tail call i32 @unlink(i8* %1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %55
17: ; preds = %13, %10
%18 = load i8, i8* %0
%19 = icmp eq i8 %18, 47
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = tail call i32 @symlink(i8* %0, i8* %1)
%22 = icmp eq i32 %21, -1
br i1 %22, label %55, label %42
23: ; preds = %17
%24 = load i32, i32* @_POSIX_PATH_MAX
%25 = zext i32 %24 to i64
%26 = tail call i8* @llvm.stacksave()
%27 = alloca i8, i64 %25
%28 = call i32* @getcwd(i8* %27, i32 %24)
%29 = icmp eq i32* %28, null
br i1 %29, label %54, label %30
30: ; preds = %23
%31 = call i32 @strlen(i8* %27)
%32 = call i32 @strlen(i8* %0)
%33 = add i32 %31, 2
%34 = add i32 %33, %32
%35 = sext i32 %34 to i64
%36 = icmp ugt i64 %35, %25
br i1 %36, label %53, label %37
37: ; preds = %30
%38 = call i32 @strcat(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%39 = call i32 @strcat(i8* %27, i8* %0)
%40 = call i32 @symlink(i8* %27, i8* %1)
%41 = icmp eq i32 %40, -1
br i1 %41, label %55, label %42
42: ; preds = %37, %20
%43 = call i32 @stat(i8* %0, %struct.stat* %3)
%44 = icmp eq i32 %43, -1
br i1 %44, label %51, label %45
45: ; preds = %42
%46 = call i32 @stat(i8* %1, %struct.stat* %4)
%47 = icmp eq i32 %46, -1
br i1 %47, label %51, label %48
48: ; preds = %45
%49 = call i32 @compare_stat(%struct.stat* %3, %struct.stat* %4)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %55
51: ; preds = %48, %45, %42
%52 = call i32 @unlink(i8* %1)
br label %55
53: ; preds = %30
br label %55
54: ; preds = %23
br label %55
55: ; preds = %48, %54, %53, %20, %13, %2, %37, %51
%56 = phi i32 [ -1, %51 ], [ -1, %37 ], [ -1, %2 ], [ -1, %13 ], [ -1, %20 ], [ -1, %53 ], [ -1, %54 ], [ 0, %48 ]
ret i32 %56
}