#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 */
int readlink(char *, char *, size_t);
int snprintf(char *, int, char *, int);
int resolve_procfd_symlink(int fd, char *buf, size_t buflen) {
  int ret;
  char procfd[4096];
  ret = snprintf(procfd, 4096, "/proc/self/fd/%d", fd);
  if (ret < 0 || ret >= 4096)
    return -1;
  ret = readlink(procfd, buf, buflen);
  if (ret < 0 || (size_t)ret >= buflen)
    return -1;
  buf[ret] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<resolve_procfd_symlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x1000,%esi
lea    0xeb4(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     116b <resolve_procfd_symlink+0x4b>
cmpl   $0x1000,-0x1c(%rbp)
jl     1177 <resolve_procfd_symlink+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <resolve_procfd_symlink+0xa5>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <readlink@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11a6 <resolve_procfd_symlink+0x86>
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11b2 <resolve_procfd_symlink+0x92>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <resolve_procfd_symlink+0xa5>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_procfd_symlink>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ecx
lea    0xec6(%rip),%rdx
mov    %rsp,%rdi
mov    $0x1000,%esi
call   1040 <snprintf@plt>
mov    $0xffffffff,%ebp
cmp    $0xfff,%eax
ja     1173 <resolve_procfd_symlink+0x53>
mov    %rsp,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <readlink@plt>
test   %eax,%eax
js     1173 <resolve_procfd_symlink+0x53>
mov    %eax,%eax
cmp    %rbx,%rax
jae    1173 <resolve_procfd_symlink+0x53>
movb   $0x0,(%r14,%rax,1)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
define i32 @resolve_procfd_symlink(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca [4096 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%11 = load i32, i32* %5
%12 = call i32 @snprintf(i8* %10, i32 4096, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i32, i32* %8
%17 = icmp sge i32 %16, 4096
br i1 %17, label %18, label %19
18: ; preds = %15, %3
store i32 -1, i32* %4
br label %37
19: ; preds = %15
%20 = getelementptr inbounds [4096 x i8], [4096 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* %8
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %19
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = load i64, i64* %7
%30 = icmp uge i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %26, %19
store i32 -1, i32* %4
br label %37
32: ; preds = %26
%33 = load i8*, i8** %6
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
store i32 0, i32* %4
br label %37
37: ; preds = %32, %31, %18
%38 = load i32, i32* %4
ret i32 %38
}

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