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

/* Variables and functions */
int S_IFLNK;
int S_IFMT;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
scalar_t__ lstat(char const *, struct stat *);
int readlink(char const *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlink(char const *);
void mgr_unlink(const char *path) {
  struct stat sb;
  if (lstat(path, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFLNK) {
    char buf[256];
    int n = readlink(path, buf, 255);
    if (n > 0) {
      buf[n] = 0;
      unlink(buf);
    } else {
      fprintf(stderr, "readlink failed: %s\n", path);
    }
  }
  unlink(path);
}


x86 assembly (clang, -O0):
<mgr_unlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <lstat@plt>
cmp    $0x0,%rax
jne    11e7 <mgr_unlink+0xa7>
mov    -0x10(%rbp),%eax
mov    0x2e68(%rip),%rcx
and    (%rcx),%eax
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%eax
jne    11e7 <mgr_unlink+0xa7>
mov    -0x8(%rbp),%rdi
lea    -0x110(%rbp),%rsi
mov    $0xff,%edx
call   1040 <readlink@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jle    11c9 <mgr_unlink+0x89>
movslq -0x114(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
jmp    11e2 <mgr_unlink+0xa2>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe23(%rip),%rsi
call   1060 <fprintf@plt>
jmp    11e7 <mgr_unlink+0xa7>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mgr_unlink>:
push   %rbx
sub    $0x110,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <lstat@plt>
test   %rax,%rax
jne    11b3 <mgr_unlink+0x73>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%eax
jne    11b3 <mgr_unlink+0x73>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xff,%edx
call   1040 <readlink@plt>
test   %eax,%eax
jle    119b <mgr_unlink+0x5b>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
jmp    11b3 <mgr_unlink+0x73>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
add    $0x110,%rsp
pop    %rbx
jmp    1030 <unlink@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IFMT = global i32 0
@S_IFLNK = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"readlink failed: %s\0A\00"
define void @mgr_unlink(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca [256 x i8]
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i64 @lstat(i8* %6, %struct.stat* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %33
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @S_IFMT
%13 = and i32 %11, %12
%14 = load i32, i32* @S_IFLNK
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %33
16: ; preds = %9
%17 = load i8*, i8** %2
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%19 = call i32 @readlink(i8* %17, i8* %18, i32 255)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %16
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %24
store i8 0, i8* %25
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%27 = call i32 @unlink(i8* %26)
br label %32
28: ; preds = %16
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %2
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %28, %22
br label %33
33: ; preds = %32, %9, %1
%34 = load i8*, i8** %2
%35 = call i32 @unlink(i8* %34)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IFMT = global i32 0
@S_IFLNK = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"readlink failed: %s\0A\00"
define void @mgr_unlink(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca [256 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = call i64 @lstat(i8* %0, %struct.stat* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %26
7: ; preds = %1
%8 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @S_IFMT
%11 = and i32 %10, %9
%12 = load i32, i32* @S_IFLNK
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %7
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%16 = call i32 @readlink(i8* %0, i8* %15, i32 255)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = zext i32 %16 to i64
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i32 @unlink(i8* %15)
br label %25
22: ; preds = %14
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %25
25: ; preds = %22, %18
br label %26
26: ; preds = %25, %7, %1
%27 = tail call i32 @unlink(i8* %0)
ret void
}