#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 */

/* Variables and functions */
scalar_t__ ENOTDIR;
int O_CLOEXEC;
int O_DIRECTORY;
int O_NOFOLLOW;
int /*<<< orphan*/ assert(char const *);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ is_dir(char const *);
int safe_fd(char const *, int);
int has_link(const char *dir) {
  assert(dir);
  int fd = safe_fd(dir, 010000000 | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
  if (fd == -1) {
    if (errno == ENOTDIR && is_dir(dir))
      return 1;
  } else
    close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<has_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
or     $0x200000,%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
call   1040 <safe_fd@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ca <has_link+0x8a>
mov    0x2e35(%rip),%rax
mov    (%rax),%rax
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
jne    11c5 <has_link+0x85>
mov    -0x10(%rbp),%rdi
call   1030 <is_dir@plt>
cmp    $0x0,%rax
je     11c5 <has_link+0x85>
movl   $0x1,-0x4(%rbp)
jmp    11d9 <has_link+0x99>
jmp    11d2 <has_link+0x92>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<has_link>:
push   %rbx
mov    %rdi,%rbx
call   1060 <assert@plt>
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    0x2e9f(%rip),%rax
or     (%rax),%esi
mov    0x2e56(%rip),%rax
or     (%rax),%esi
or     $0x200000,%esi
mov    %rbx,%rdi
call   1040 <safe_fd@plt>
cmp    $0xffffffff,%eax
je     1180 <has_link+0x40>
mov    %eax,%edi
call   1050 <close@plt>
jmp    11ad <has_link+0x6d>
mov    0x2e41(%rip),%rax
mov    (%rax),%rax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%rax
jne    11ad <has_link+0x6d>
mov    %rbx,%rdi
call   1030 <is_dir@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
je     11ad <has_link+0x6d>
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_DIRECTORY = global i32 0
@O_NOFOLLOW = global i32 0
@O_CLOEXEC = global i32 0
@errno = global i64 0
@ENOTDIR = global i64 0
define i32 @has_link(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @assert(i8* %5)
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_DIRECTORY
%9 = or i32 2097152, %8
%10 = load i32, i32* @O_NOFOLLOW
%11 = or i32 %9, %10
%12 = load i32, i32* @O_CLOEXEC
%13 = or i32 %11, %12
%14 = call i32 @safe_fd(i8* %7, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %27
17: ; preds = %1
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOTDIR
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i8*, i8** %3
%23 = call i64 @is_dir(i8* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 1, i32* %2
br label %31
26: ; preds = %21, %17
br label %30
27: ; preds = %1
%28 = load i32, i32* %4
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %27, %26
store i32 0, i32* %2
br label %31
31: ; preds = %30, %25
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@O_DIRECTORY = global i32 0
@O_NOFOLLOW = global i32 0
@O_CLOEXEC = global i32 0
@errno = global i64 0
@ENOTDIR = global i64 0
define i32 @has_link(i8* %0) {
%2 = tail call i32 @assert(i8* %0)
%3 = load i32, i32* @O_DIRECTORY
%4 = load i32, i32* @O_NOFOLLOW
%5 = load i32, i32* @O_CLOEXEC
%6 = or i32 %3, %4
%7 = or i32 %6, %5
%8 = or i32 %7, 2097152
%9 = tail call i32 @safe_fd(i8* %0, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %18
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @ENOTDIR
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = tail call i64 @is_dir(i8* %0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %21
18: ; preds = %1
%19 = tail call i32 @close(i32 %9)
br label %20
20: ; preds = %11, %15, %18
br label %21
21: ; preds = %15, %20
%22 = phi i32 [ 0, %20 ], [ 1, %15 ]
ret i32 %22
}