#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 */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ bake_args(int, char **, int *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int link(char *, char *);
int optind;
int /*<<< orphan*/ perror(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int symlink(char *, char *);
int unlink(char *);
int main(int argc, char **argv) {
  int opt;
  bake_args(argc, argv, &opt);
  if (argc - optind < 2) {
    fprintf(stderr, "ln: not enough arguments\n");
    return 1;
  }
  int ret = 0;
  char *target = argv[optind], *link_name = argv[optind + 1];
  if (opt & (1 << 1)) {
    struct stat st;
    int ret = stat(link_name, &st);
    if (!ret && !S_ISDIR(st.st_mode))
      ret = unlink(link_name);
  }
  if (opt & (1 << 0))
    ret = symlink(target, link_name);
  else
    ret = link(target, link_name);
  if (ret < 0) {
    perror("ln");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   10a0 <bake_args@plt>
mov    -0x8(%rbp),%eax
mov    0x2e38(%rip),%rcx
sub    (%rcx),%eax
cmp    $0x2,%eax
jge    11dc <main+0x5c>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bc <main+0x13c>
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2dfa(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2de4(%rip),%rcx
mov    (%rcx),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     125f <main+0xdf>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    125a <main+0xda>
mov    -0x30(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    125a <main+0xda>
mov    -0x28(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x34(%rbp)
jmp    125f <main+0xdf>
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1283 <main+0x103>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <symlink@plt>
mov    %eax,-0x18(%rbp)
jmp    1293 <main+0x113>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <link@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12b5 <main+0x135>
lea    0xd76(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bc <main+0x13c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x4(%rsp),%rdx
call   10a0 <bake_args@plt>
mov    0x2e4a(%rip),%rax
movslq (%rax),%rax
sub    %eax,%ebp
cmp    $0x1,%ebp
jg     11c4 <main+0x44>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    121d <main+0x9d>
mov    (%rbx,%rax,8),%r14
mov    0x8(%rbx,%rax,8),%rbx
mov    0x4(%rsp),%eax
test   $0x2,%al
je     11ff <main+0x7f>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %eax,%eax
jne    11fb <main+0x7b>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
jne    11fb <main+0x7b>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x4(%rsp),%eax
mov    %r14,%rdi
mov    %rbx,%rsi
test   $0x1,%al
jne    1210 <main+0x90>
call   1090 <link@plt>
jmp    1215 <main+0x95>
call   1050 <symlink@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
js     1226 <main+0xa6>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xded(%rip),%rdi
call   1080 <perror@plt>
jmp    11bd <main+0x3d>

LLVM IR (-O0):
%struct.stat = type { i32 }
@optind = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"ln: not enough arguments\0A\00"
@.str.1 = [3 x i8] c"ln\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca %struct.stat
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = load i8**, i8*** %5
%14 = call i32 @bake_args(i32 %12, i8** %13, i32* %6)
%15 = load i32, i32* %4
%16 = load i32, i32* @optind
%17 = sub nsw i32 %15, %16
%18 = icmp slt i32 %17, 2
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %69
22: ; preds = %2
store i32 0, i32* %7
%23 = load i8**, i8*** %5
%24 = load i32, i32* @optind
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8*, i8** %23, i64 %25
%27 = load i8*, i8** %26
store i8* %27, i8** %8
%28 = load i8**, i8*** %5
%29 = load i32, i32* @optind
%30 = add nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8*, i8** %28, i64 %31
%33 = load i8*, i8** %32
store i8* %33, i8** %9
%34 = load i32, i32* %6
%35 = and i32 %34, 2
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %51
37: ; preds = %22
%38 = load i8*, i8** %9
%39 = call i32 @stat(i8* %38, %struct.stat* %10)
store i32 %39, i32* %11
%40 = load i32, i32* %11
%41 = icmp ne i32 %40, 0
br i1 %41, label %50, label %42
42: ; preds = %37
%43 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @S_ISDIR(i32 %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %42
%48 = load i8*, i8** %9
%49 = call i32 @unlink(i8* %48)
store i32 %49, i32* %11
br label %50
50: ; preds = %47, %42, %37
br label %51
51: ; preds = %50, %22
%52 = load i32, i32* %6
%53 = and i32 %52, 1
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %51
%56 = load i8*, i8** %8
%57 = load i8*, i8** %9
%58 = call i32 @symlink(i8* %56, i8* %57)
store i32 %58, i32* %7
br label %63
59: ; preds = %51
%60 = load i8*, i8** %8
%61 = load i8*, i8** %9
%62 = call i32 @link(i8* %60, i8* %61)
store i32 %62, i32* %7
br label %63
63: ; preds = %59, %55
%64 = load i32, i32* %7
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %63
%67 = call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %69
68: ; preds = %63
store i32 0, i32* %3
br label %69
69: ; preds = %68, %66, %19
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@optind = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"ln: not enough arguments\0A\00"
@.str.1 = [3 x i8] c"ln\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca %struct.stat
%5 = bitcast i32* %3 to i8*
%6 = call i32 @bake_args(i32 %0, i8** %1, i32* %3)
%7 = load i32, i32* @optind
%8 = sub nsw i32 %0, %7
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %50
13: ; preds = %2
%14 = sext i32 %7 to i64
%15 = getelementptr inbounds i8*, i8** %1, i64 %14
%16 = load i8*, i8** %15
%17 = add nsw i32 %7, 1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8*, i8** %1, i64 %18
%20 = load i8*, i8** %19
%21 = load i32, i32* %3
%22 = and i32 %21, 2
%23 = icmp eq i32 %22, 0
br i1 %23, label %37, label %24
24: ; preds = %13
%25 = bitcast %struct.stat* %4 to i8*
%26 = call i32 @stat(i8* %20, %struct.stat* %4)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %24
%29 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @S_ISDIR(i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @unlink(i8* %20)
br label %35
35: ; preds = %33, %28, %24
%36 = load i32, i32* %3
br label %37
37: ; preds = %35, %13
%38 = phi i32 [ %36, %35 ], [ %21, %13 ]
%39 = and i32 %38, 1
%40 = icmp eq i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %37
%42 = call i32 @symlink(i8* %16, i8* %20)
br label %45
43: ; preds = %37
%44 = call i32 @link(i8* %16, i8* %20)
br label %45
45: ; preds = %43, %41
%46 = phi i32 [ %42, %41 ], [ %44, %43 ]
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %45, %10
%51 = phi i32 [ 1, %10 ], [ 1, %48 ], [ 0, %45 ]
ret i32 %51
}