#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 */
typedef int /*<<< orphan*/ tmp;
typedef int ssize_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int dirfd(int /*<<< orphan*/ *);
int readlink(char *, char *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int get_dir_name(DIR *dirp, char *buf, size_t size) {
  int fd = dirfd(dirp);
  if (fd == -1) {
    return 0;
  }
  char tmp[64];
  snprintf(tmp, sizeof(tmp), "/proc/self/fd/%d", fd);
  ssize_t ret = readlink(tmp, buf, size);
  if (ret == -1) {
    return 0;
  }
  buf[ret] = 0;
  return 1;
}


x86 assembly (clang, -O0):
<get_dir_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <dirfd@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1169 <get_dir_name+0x39>
movl   $0x0,-0x4(%rbp)
jmp    11be <get_dir_name+0x8e>
lea    -0x70(%rbp),%rdi
mov    -0x24(%rbp),%ecx
mov    $0x40,%esi
lea    0xe84(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <readlink@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0xffffffff,-0x74(%rbp)
jne    11ab <get_dir_name+0x7b>
movl   $0x0,-0x4(%rbp)
jmp    11be <get_dir_name+0x8e>
mov    -0x18(%rbp),%rax
movslq -0x74(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_dir_name>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
call   1050 <dirfd@plt>
cmp    $0xffffffff,%eax
je     1182 <get_dir_name+0x52>
lea    0xeb0(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x40,%esi
mov    %eax,%ecx
call   1040 <snprintf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <readlink@plt>
cmp    $0xffffffff,%eax
je     1182 <get_dir_name+0x52>
cltq
movb   $0x0,(%rbx,%rax,1)
mov    $0x1,%eax
jmp    1184 <get_dir_name+0x54>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
define i32 @get_dir_name(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca [64 x i8]
%10 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i32*, i32** %5
%12 = call i32 @dirfd(i32* %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 0, i32* %4
br label %32
16: ; preds = %3
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%18 = load i32, i32* %8
%19 = call i32 @snprintf(i8* %17, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%21 = load i8*, i8** %6
%22 = load i64, i64* %7
%23 = call i32 @readlink(i8* %20, i8* %21, i64 %22)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %16
store i32 0, i32* %4
br label %32
27: ; preds = %16
%28 = load i8*, i8** %6
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
store i32 1, i32* %4
br label %32
32: ; preds = %27, %26, %15
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
define i32 @get_dir_name(i32* %0, i8* nocapture %1, i64 %2) {
%4 = alloca [64 x i8]
%5 = tail call i32 @dirfd(i32* %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %17, label %7
7: ; preds = %3
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%9 = call i32 @snprintf(i8* %8, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %5)
%10 = call i32 @readlink(i8* %8, i8* %1, i64 %2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = sext i32 %10 to i64
%14 = getelementptr inbounds i8, i8* %1, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %7, %12
%16 = phi i32 [ 1, %12 ], [ 0, %7 ]
br label %17
17: ; preds = %3, %15
%18 = phi i32 [ %16, %15 ], [ 0, %3 ]
ret i32 %18
}