#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*/ AT_SYMLINK_NOFOLLOW;
int /*<<< orphan*/ S_ISLNK(int /*<<< orphan*/);
int errno;
scalar_t__ fstatat(int, char const *, struct stat *, int /*<<< orphan*/);
scalar_t__ lstat(char const *, struct stat *);
int is_symlink_helper(int dirfd, const char *name) {
  struct stat stat;
  if (dirfd < 0) {
    if (lstat(name, &stat) < 0) {
      return -errno;
    }
  } else {
    if (fstatat(dirfd, name, &stat, AT_SYMLINK_NOFOLLOW) < 0) {
      return -errno;
    }
  }
  return !!S_ISLNK(stat.st_mode);
}


x86 assembly (clang, -O0):
<is_symlink_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1178 <is_symlink_helper+0x48>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0x0,%rax
jge    1173 <is_symlink_helper+0x43>
xor    %eax,%eax
mov    0x2e77(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11cd <is_symlink_helper+0x9d>
jmp    11b3 <is_symlink_helper+0x83>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e52(%rip),%rax
mov    (%rax),%ecx
lea    -0x18(%rbp),%rdx
call   1050 <fstatat@plt>
cmp    $0x0,%rax
jge    11ae <is_symlink_helper+0x7e>
xor    %eax,%eax
mov    0x2e3c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11cd <is_symlink_helper+0x9d>
jmp    11b3 <is_symlink_helper+0x83>
mov    -0x18(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_symlink_helper>:
push   %rax
test   %edi,%edi
js     115e <is_symlink_helper+0x2e>
mov    0x2e9c(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
call   1050 <fstatat@plt>
test   %rax,%rax
js     1171 <is_symlink_helper+0x41>
mov    (%rsp),%edi
call   1030 <S_ISLNK@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
setne  %al
pop    %rcx
ret
mov    %rsp,%rax
mov    %rsi,%rdi
mov    %rax,%rsi
call   1040 <lstat@plt>
test   %rax,%rax
jns    114b <is_symlink_helper+0x1b>
mov    0x2e68(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i32 0
@AT_SYMLINK_NOFOLLOW = global i32 0
define i32 @is_symlink_helper(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.stat
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %2
%10 = load i8*, i8** %5
%11 = call i64 @lstat(i8* %10, %struct.stat* %6)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @errno
%15 = sub nsw i32 0, %14
store i32 %15, i32* %3
br label %35
16: ; preds = %9
br label %27
17: ; preds = %2
%18 = load i32, i32* %4
%19 = load i8*, i8** %5
%20 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%21 = call i64 @fstatat(i32 %18, i8* %19, %struct.stat* %6, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i32, i32* @errno
%25 = sub nsw i32 0, %24
store i32 %25, i32* %3
br label %35
26: ; preds = %17
br label %27
27: ; preds = %26, %16
%28 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @S_ISLNK(i32 %29)
%31 = icmp ne i32 %30, 0
%32 = xor i1 %31, true
%33 = xor i1 %32, true
%34 = zext i1 %33 to i32
store i32 %34, i32* %3
br label %35
35: ; preds = %27, %23, %13
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i32 0
@AT_SYMLINK_NOFOLLOW = global i32 0
define i32 @is_symlink_helper(i32 %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 0
br i1 %5, label %6, label %12
6: ; preds = %2
%7 = call i64 @lstat(i8* %1, %struct.stat* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %19
9: ; preds = %6
%10 = load i32, i32* @errno
%11 = sub nsw i32 0, %10
br label %25
12: ; preds = %2
%13 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%14 = call i64 @fstatat(i32 %0, i8* %1, %struct.stat* %3, i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
br label %25
19: ; preds = %12, %6
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @S_ISLNK(i32 %21)
%23 = icmp ne i32 %22, 0
%24 = zext i1 %23 to i32
br label %25
25: ; preds = %19, %16, %9
%26 = phi i32 [ %11, %9 ], [ %24, %19 ], [ %18, %16 ]
ret i32 %26
}