#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__ ENOENT;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ link(char *, char *);
scalar_t__ lstat(char *, struct stat *);
scalar_t__ unlink(char *);
int main(int argc, char **argv) {
  struct stat stats;
  if (argc != 3)
    return 1;
  if (lstat(argv[2], &stats) == 0) {
    if (S_ISDIR(stats.st_mode)) {
      return 1;
    } else if (unlink(argv[2]) && errno != ENOENT) {
      return 1;
    }
  } else if (errno != ENOENT) {
    return 1;
  }
  if (link(argv[1], argv[2]) == 0) {
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     116c <main+0x2c>
movl   $0x1,-0x4(%rbp)
jmp    1249 <main+0x109>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <lstat@plt>
cmp    $0x0,%rax
jne    11ec <main+0xac>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11a5 <main+0x65>
movl   $0x1,-0x4(%rbp)
jmp    1249 <main+0x109>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
je     11e2 <main+0xa2>
mov    0x2e1d(%rip),%rax
mov    (%rax),%rax
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%rax
je     11e2 <main+0xa2>
movl   $0x1,-0x4(%rbp)
jmp    1249 <main+0x109>
jmp    11e7 <main+0xa7>
jmp    1217 <main+0xd7>
mov    0x2ded(%rip),%rax
mov    (%rax),%rax
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
je     1212 <main+0xd2>
movl   $0x1,-0x4(%rbp)
jmp    1249 <main+0x109>
jmp    1217 <main+0xd7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1060 <link@plt>
cmp    $0x0,%rax
jne    1242 <main+0x102>
movl   $0x0,-0x4(%rbp)
jmp    1249 <main+0x109>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x1,%ebx
cmp    $0x3,%edi
jne    119c <main+0x5c>
mov    %rsi,%r14
mov    0x10(%rsi),%rdi
mov    %rsp,%rsi
call   1050 <lstat@plt>
test   %rax,%rax
je     118f <main+0x4f>
mov    0x2e77(%rip),%rax
mov    (%rax),%rax
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%rax
jne    119c <main+0x5c>
mov    0x8(%r14),%rdi
mov    0x10(%r14),%rsi
call   1060 <link@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
jmp    119c <main+0x5c>
mov    (%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %rax,%rax
je     11a6 <main+0x66>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x10(%r14),%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jne    1162 <main+0x22>
jmp    1178 <main+0x38>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 3
br i1 %8, label %9, label %10
9: ; preds = %2
store i32 1, i32* %3
br label %52
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 2
%13 = load i8*, i8** %12
%14 = call i64 @lstat(i8* %13, %struct.stat* %6)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %35
16: ; preds = %10
%17 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i64 @S_ISDIR(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 1, i32* %3
br label %52
22: ; preds = %16
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i64 @unlink(i8* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = load i64, i64* @errno
%30 = load i64, i64* @ENOENT
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 1, i32* %3
br label %52
33: ; preds = %28, %22
br label %34
34: ; preds = %33
br label %41
35: ; preds = %10
%36 = load i64, i64* @errno
%37 = load i64, i64* @ENOENT
%38 = icmp ne i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 1, i32* %3
br label %52
40: ; preds = %35
br label %41
41: ; preds = %40, %34
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 2
%47 = load i8*, i8** %46
%48 = call i64 @link(i8* %44, i8* %47)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %41
store i32 0, i32* %3
br label %52
51: ; preds = %41
store i32 1, i32* %3
br label %52
52: ; preds = %51, %50, %39, %32, %21, %9
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %6, label %35
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = call i64 @lstat(i8* %8, %struct.stat* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %6
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i64 @S_ISDIR(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %35
16: ; preds = %11
%17 = load i8*, i8** %7
%18 = tail call i64 @unlink(i8* %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %28, label %20
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = load i64, i64* @ENOENT
%23 = icmp eq i64 %21, %22
br i1 %23, label %28, label %35
24: ; preds = %6
%25 = load i64, i64* @errno
%26 = load i64, i64* @ENOENT
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %35
28: ; preds = %24, %20, %16
%29 = getelementptr inbounds i8*, i8** %1, i64 1
%30 = load i8*, i8** %29
%31 = load i8*, i8** %7
%32 = tail call i64 @link(i8* %30, i8* %31)
%33 = icmp ne i64 %32, 0
%34 = zext i1 %33 to i32
br label %35
35: ; preds = %28, %24, %20, %11, %2
%36 = phi i32 [ 1, %2 ], [ 1, %11 ], [ 1, %20 ], [ 1, %24 ], [ %34, %28 ]
ret i32 %36
}