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

/* Variables and functions */
int readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
const char *pname(int pid) {
  static char buf[1024 * 8], fname[4 + 10 + 3 + 4];
  snprintf(fname, sizeof(fname), "/proc/%d/exe", pid);
  ssize_t l = readlink(fname, buf, sizeof(buf));
  if (l == -1)
    strcpy(buf, "cannot read link");
  else
    buf[l < sizeof(buf) ? l : sizeof(buf) - 1] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<pname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%ecx
lea    0x4f0b(%rip),%rdi
mov    $0x15,%esi
lea    0xeaf(%rip),%rdx
call   1050 <snprintf@plt>
lea    0x4ef3(%rip),%rdi
lea    0x2eec(%rip),%rsi
mov    $0x2000,%edx
call   1040 <readlink@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1193 <pname+0x63>
lea    0x2ece(%rip),%rdi
lea    0xe84(%rip),%rsi
call   1030 <strcpy@plt>
jmp    11cd <pname+0x9d>
movslq -0x8(%rbp),%rax
cmp    $0x2000,%rax
jae    11b0 <pname+0x80>
movslq -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    11be <pname+0x8e>
mov    $0x1fff,%eax
mov    %rax,-0x10(%rbp)
jmp    11be <pname+0x8e>
mov    -0x10(%rbp),%rcx
lea    0x2e87(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e7c(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pname>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ecx
lea    0x4f13(%rip),%rbx
lea    0xebc(%rip),%rdx
mov    %rbx,%rdi
mov    $0x15,%esi
call   1050 <snprintf@plt>
lea    0x2ef8(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x2000,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     1185 <pname+0x55>
cltq
cmp    $0x1fff,%rax
mov    $0x1fff,%ecx
cmovb  %rax,%rcx
movb   $0x0,(%rcx,%r14,1)
jmp    1198 <pname+0x68>
lea    0x2ec4(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x2eb1(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@pname.buf = internal global [8192 x i8] zeroinitializer
@pname.fname = internal global [21 x i8] zeroinitializer
@.str = [13 x i8] c"/proc/%d/exe\00"
@.str.1 = [17 x i8] c"cannot read link\00"
define i8* @pname(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i32 @snprintf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @pname.fname, i64 0, i64 0), i32 21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32 @readlink(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @pname.fname, i64 0, i64 0), i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @pname.buf, i64 0, i64 0), i32 8192)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @strcpy(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @pname.buf, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %22
11: ; preds = %1
%12 = load i32, i32* %3
%13 = sext i32 %12 to i64
%14 = icmp ult i64 %13, 8192
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
br label %19
18: ; preds = %11
br label %19
19: ; preds = %18, %15
%20 = phi i64 [ %17, %15 ], [ 8191, %18 ]
%21 = getelementptr inbounds [8192 x i8], [8192 x i8]* @pname.buf, i64 0, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %19, %9
ret i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @pname.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@pname.buf = internal global [8192 x i8] zeroinitializer
@pname.fname = internal global [21 x i8] zeroinitializer
@.str = [13 x i8] c"/proc/%d/exe\00"
@.str.1 = [17 x i8] c"cannot read link\00"
define i8* @pname(i32 %0) {
%2 = tail call i32 @snprintf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @pname.fname, i64 0, i64 0), i32 21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = tail call i32 @readlink(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @pname.fname, i64 0, i64 0), i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @pname.buf, i64 0, i64 0), i32 8192)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @strcpy(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @pname.buf, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %12
7: ; preds = %1
%8 = sext i32 %3 to i64
%9 = icmp ult i64 %8, 8191
%10 = select i1 %9, i64 %8, i64 8191
%11 = getelementptr inbounds [8192 x i8], [8192 x i8]* @pname.buf, i64 0, i64 %10
store i8 0, i8* %11
br label %12
12: ; preds = %7, %5
ret i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @pname.buf, i64 0, i64 0)
}