#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 /*<<< orphan*/ memmove(char *, char *, scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int sys_readlink(char *, char *, size_t);
bool get_fd_path(int fd, char *buf, size_t len) {
  char proc[32];
  int ret;
  sprintf(proc, "/proc/self/fd/%d", fd);
  ret = sys_readlink(proc, buf, len - 1);
  if (ret <= 0)
    return false;
  buf[ret] = 0;
  if (strncmp(buf, "/Volumes/SystemRoot/dev/", 24) == 0) {
    memmove(buf, buf + 19, strlen(buf + 19) + 1);
  }
  return true;
}


x86 assembly (clang, -O0):
<get_fd_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe8f(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1050 <sys_readlink@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jg     11a4 <get_fd_path+0x54>
movl   $0x0,-0x4(%rbp)
jmp    120b <get_fd_path+0xbb>
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
lea    0xe56(%rip),%rsi
mov    $0x18,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1204 <get_fd_path+0xb4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
add    $0x13,%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x13,%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1060 <memmove@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_fd_path>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %edi,%edx
lea    0xe99(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1070 <sprintf@plt>
add    $0xffffffffffffffff,%rbp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1050 <sys_readlink@plt>
test   %eax,%eax
jle    11c9 <get_fd_path+0x79>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
lea    0xe7c(%rip),%rsi
mov    %rbx,%rdi
mov    $0x18,%edx
call   1030 <strncmp@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    11cb <get_fd_path+0x7b>
lea    0x13(%rbx),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <memmove@plt>
jmp    11cb <get_fd_path+0x7b>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [25 x i8] c"/Volumes/SystemRoot/dev/\00"
define i32 @get_fd_path(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [32 x i8]
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%11 = load i32, i32* %5
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%14 = load i8*, i8** %6
%15 = load i64, i64* %7
%16 = sub i64 %15, 1
%17 = call i32 @sys_readlink(i8* %13, i8* %14, i64 %16)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp sle i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 0, i32* %4
br label %39
21: ; preds = %3
%22 = load i8*, i8** %6
%23 = load i32, i32* %9
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 0, i8* %25
%26 = load i8*, i8** %6
%27 = call i64 @strncmp(i8* %26, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 24)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %38
29: ; preds = %21
%30 = load i8*, i8** %6
%31 = load i8*, i8** %6
%32 = getelementptr inbounds i8, i8* %31, i64 19
%33 = load i8*, i8** %6
%34 = getelementptr inbounds i8, i8* %33, i64 19
%35 = call i64 @strlen(i8* %34)
%36 = add nsw i64 %35, 1
%37 = call i32 @memmove(i8* %30, i8* %32, i64 %36)
br label %38
38: ; preds = %29, %21
store i32 1, i32* %4
br label %39
39: ; preds = %38, %20
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@.str.1 = [25 x i8] c"/Volumes/SystemRoot/dev/\00"
define i32 @get_fd_path(i32 %0, i8* %1, i64 %2) {
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = add i64 %2, -1
%8 = call i32 @sys_readlink(i8* %5, i8* %1, i64 %7)
%9 = icmp slt i32 %8, 1
br i1 %9, label %20, label %10
10: ; preds = %3
%11 = zext i32 %8 to i64
%12 = getelementptr inbounds i8, i8* %1, i64 %11
store i8 0, i8* %12
%13 = call i64 @strncmp(i8* %1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 24)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %1, i64 19
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %17, 1
%19 = call i32 @memmove(i8* %1, i8* %16, i64 %18)
br label %20
20: ; preds = %10, %15, %3
%21 = phi i32 [ 0, %3 ], [ 1, %15 ], [ 1, %10 ]
ret i32 %21
}