#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct stat {
  int dummy;
};
struct TYPE_3__ {
  scalar_t__ (*gl_stat)(char *, struct stat *);
};
typedef TYPE_1__ glob_t;

/* Variables and functions */
int GLOB_ALTDIRFUNC;
scalar_t__ alloca(size_t);
char *mempcpy(char *, char const *, size_t);
scalar_t__ stat(char *, struct stat *);
size_t strlen(char const *);
scalar_t__ stub1(char *, struct stat *);
int link_exists_p(const char *dir, size_t dirlen, const char *fname,
                  glob_t *pglob, int flags) {
  size_t fnamelen = strlen(fname);
  char *fullname = (char *)alloca(dirlen + 1 + fnamelen + 1);
  struct stat st;
  struct stat st64;
  mempcpy(mempcpy(mempcpy(fullname, dir, dirlen), "/", 1), fname, fnamelen + 1);
  return (((flags & GLOB_ALTDIRFUNC) ? (*pglob->gl_stat)(fullname, &st)
                                     : stat(fullname, &st64)) == 0);
}


x86 assembly (clang, -O0):
<link_exists_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
add    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1040 <alloca@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    %rdi,-0x58(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    %rdx,-0x50(%rbp)
call   1060 <memcpy@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rax
add    %rax,%rdi
movb   $0x2f,(%rdi)
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
add    $0x1,%rdx
call   1060 <memcpy@plt>
mov    -0x24(%rbp),%eax
mov    0x2e20(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11ed <link_exists_p+0xad>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    -0x38(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   *%rax
mov    %rax,-0x60(%rbp)
jmp    11fe <link_exists_p+0xbe>
mov    -0x38(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1050 <stat@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<link_exists_p>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r15d
mov    %rcx,(%rsp)
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %rdi,%r13
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r14
lea    (%rax,%rbp,1),%rdi
add    $0x2,%rdi
call   1040 <alloca@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1060 <memcpy@plt>
movb   $0x2f,(%rbx,%rbp,1)
lea    (%rbx,%rbp,1),%rdi
add    $0x1,%rdi
add    $0x1,%r14
mov    %r12,%rsi
mov    %r14,%rdx
call   1060 <memcpy@plt>
mov    0x2e3f(%rip),%rax
test   %r15d,(%rax)
je     11be <link_exists_p+0x7e>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    (%rsp),%rax
call   *(%rax)
jmp    11cb <link_exists_p+0x8b>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64 (i8*, %struct.stat*)* }
%struct.stat = type { i32 }
@.str = [2 x i8] c"/\00"
@GLOB_ALTDIRFUNC = global i32 0
define i32 @link_exists_p(i8* %0, i64 %1, i8* %2, %struct.TYPE_3__* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca %struct.TYPE_3__*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i8*
%13 = alloca %struct.stat
%14 = alloca %struct.stat
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store %struct.TYPE_3__* %3, %struct.TYPE_3__** %9
store i32 %4, i32* %10
%15 = load i8*, i8** %8
%16 = call i64 @strlen(i8* %15)
store i64 %16, i64* %11
%17 = load i64, i64* %7
%18 = add i64 %17, 1
%19 = load i64, i64* %11
%20 = add i64 %18, %19
%21 = add i64 %20, 1
%22 = call i64 @alloca(i64 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %12
%24 = load i8*, i8** %12
%25 = load i8*, i8** %6
%26 = load i64, i64* %7
%27 = call i8* @mempcpy(i8* %24, i8* %25, i64 %26)
%28 = call i8* @mempcpy(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i64 1)
%29 = load i8*, i8** %8
%30 = load i64, i64* %11
%31 = add i64 %30, 1
%32 = call i8* @mempcpy(i8* %28, i8* %29, i64 %31)
%33 = load i32, i32* %10
%34 = load i32, i32* @GLOB_ALTDIRFUNC
%35 = and i32 %33, %34
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %5
%38 = load %struct.TYPE_3__*, %struct.TYPE_3__** %9
%39 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %38, i32 0, i32 0
%40 = load i64 (i8*, %struct.stat*)*, i64 (i8*, %struct.stat*)** %39
%41 = load i8*, i8** %12
%42 = call i64 %40(i8* %41, %struct.stat* %13)
br label %46
43: ; preds = %5
%44 = load i8*, i8** %12
%45 = call i64 @stat(i8* %44, %struct.stat* %14)
br label %46
46: ; preds = %43, %37
%47 = phi i64 [ %42, %37 ], [ %45, %43 ]
%48 = icmp eq i64 %47, 0
%49 = zext i1 %48 to i32
ret i32 %49
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64 (i8*, %struct.stat*)* }
%struct.stat = type { i32 }
@GLOB_ALTDIRFUNC = global i32 0
define i32 @link_exists_p(i8* nocapture %0, i64 %1, i8* nocapture %2, %struct.TYPE_3__* nocapture %3, i32 %4) {
%6 = alloca %struct.stat
%7 = alloca %struct.stat
%8 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%9 = add i64 %1, 2
%10 = add i64 %9, %8
%11 = tail call i64 @alloca(i64 %10)
%12 = inttoptr i64 %11 to i8*
%13 = bitcast %struct.stat* %6 to i8*
%14 = bitcast %struct.stat* %7 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %12, i8* align 1 %0, i64 %1, i1 false)
%15 = getelementptr inbounds i8, i8* %12, i64 %1
store i8 47, i8* %15
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = add i64 %8, 1
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %16, i8* align 1 %2, i64 %17, i1 false)
%18 = load i32, i32* @GLOB_ALTDIRFUNC
%19 = and i32 %18, %4
%20 = icmp eq i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %5
%22 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %3, i64 0, i32 0
%23 = load i64 (i8*, %struct.stat*)*, i64 (i8*, %struct.stat*)** %22
%24 = call i64 %23(i8* %12, %struct.stat* %6)
br label %27
25: ; preds = %5
%26 = call i64 @stat(i8* %12, %struct.stat* %7)
br label %27
27: ; preds = %25, %21
%28 = phi i64 [ %24, %21 ], [ %26, %25 ]
%29 = icmp eq i64 %28, 0
%30 = zext i1 %29 to i32
ret i32 %30
}