#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int get_link(int pid, char *name, char *buf) {
  char file[80];
  int ret;
  sprintf(file, "/proc/%d/%s", pid, name);
  if ((ret = readlink(file, buf, 512 - 1)) < 0) {
    fprintf(stderr, "readlink: %s: %s\n", file, strerror(errno));
    ret = 0;
    return 0;
  }
  buf[ret] = 0;
  return ret + 1;
}


x86 assembly (clang, -O0):
<get_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    0xe98(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x1ff,%edx
call   1030 <readlink@plt>
mov    %eax,-0x74(%rbp)
cmp    $0x0,%eax
jge    11dc <get_link+0x9c>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rdx
mov    %rax,%rcx
lea    0xe48(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x74(%rbp)
movl   $0x0,-0x4(%rbp)
jmp    11f1 <get_link+0xb1>
mov    -0x18(%rbp),%rax
movslq -0x74(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_link>:
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %rdx,%rbx
mov    %rsi,%rcx
mov    %edi,%edx
lea    0xeaa(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x1ff,%edx
call   1030 <readlink@plt>
test   %eax,%eax
js     1180 <get_link+0x40>
mov    %eax,%ecx
movb   $0x0,(%rbx,%rcx,1)
add    $0x1,%eax
jmp    11ad <get_link+0x6d>
mov    0x2e59(%rip),%rax
mov    (%rax),%ebx
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe6e(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/proc/%d/%s\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"readlink: %s: %s\0A\00"
@errno = global i32 0
define i32 @get_link(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [80 x i8]
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %11, i8* %12)
%14 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%15 = load i8*, i8** %7
%16 = call i32 @readlink(i8* %14, i8* %15, i32 511)
store i32 %16, i32* %9
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %3
%19 = load i32, i32* @stderr
%20 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %20, i8* %22)
store i32 0, i32* %9
store i32 0, i32* %4
br label %31
24: ; preds = %3
%25 = load i8*, i8** %7
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
%29 = load i32, i32* %9
%30 = add nsw i32 %29, 1
store i32 %30, i32* %4
br label %31
31: ; preds = %24, %18
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@.str = [12 x i8] c"/proc/%d/%s\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"readlink: %s: %s\0A\00"
@errno = global i32 0
define i32 @get_link(i32 %0, i8* %1, i8* nocapture %2) {
%4 = alloca [80 x i8]
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%7 = call i32 @readlink(i8* %5, i8* %2, i32 511)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %5, i8* %12)
br label %18
14: ; preds = %3
%15 = zext i32 %7 to i64
%16 = getelementptr inbounds i8, i8* %2, i64 %15
store i8 0, i8* %16
%17 = add nuw nsw i32 %7, 1
br label %18
18: ; preds = %14, %9
%19 = phi i32 [ 0, %9 ], [ %17, %14 ]
ret i32 %19
}