#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*/ pid_t;
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int asprintf(char **, char *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *strdup(char *);
char *pid_to_name(pid_t pid) {
  char *filename;
  char *f;
  if (asprintf(&filename, "/proc/%d/cmdline", pid) == -1)
    return strdup("");
  FILE *fp = fopen(filename, "r");
  free(filename);
  if (fp) {
    char line[256];
    f = fgets(line, sizeof(line), fp);
    fclose(fp);
    if (f == NULL)
      return strdup("");
    else
      return strdup(line);
  } else
    return strdup("");
}


x86 assembly (clang, -O0):
<pid_to_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xe84(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    119f <pid_to_name+0x3f>
lea    0xe7f(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    122e <pid_to_name+0xce>
mov    -0x18(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x28(%rbp)
je     121e <pid_to_name+0xbe>
lea    -0x130(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1209 <pid_to_name+0xa9>
lea    0xe15(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    122e <pid_to_name+0xce>
lea    -0x130(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    122e <pid_to_name+0xce>
lea    0xdeb(%rip),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_to_name>:
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %edi,%edx
lea    0xe8d(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
je     11ce <pid_to_name+0x6e>
mov    0x8(%rsp),%rdi
lea    0xe83(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     11ce <pid_to_name+0x6e>
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
je     11ce <pid_to_name+0x6e>
lea    0x10(%rsp),%rdi
jmp    11d5 <pid_to_name+0x75>
lea    0xe3b(%rip),%rdi
call   1080 <strdup@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [2 x i8] c"r\00"
define i8* @pid_to_name(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [256 x i8]
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i8* %12, i8** %2
br label %35
13: ; preds = %1
%14 = load i8*, i8** %4
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %15, i32** %6
%16 = load i8*, i8** %4
%17 = call i32 @free(i8* %16)
%18 = load i32*, i32** %6
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %33
20: ; preds = %13
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%22 = load i32*, i32** %6
%23 = call i8* @fgets(i8* %21, i32 256, i32* %22)
store i8* %23, i8** %5
%24 = load i32*, i32** %6
%25 = call i32 @fclose(i32* %24)
%26 = load i8*, i8** %5
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i8* %29, i8** %2
br label %35
30: ; preds = %20
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%32 = call i8* @strdup(i8* %31)
store i8* %32, i8** %2
br label %35
33: ; preds = %13
%34 = call i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i8* %34, i8** %2
br label %35
35: ; preds = %33, %30, %28, %11
%36 = load i8*, i8** %2
ret i8* %36
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [2 x i8] c"r\00"
define noalias i8* @pid_to_name(i32 %0) {
%2 = alloca i8*
%3 = alloca [256 x i8]
%4 = bitcast i8** %2 to i8*
%5 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %28
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = load i8*, i8** %2
%13 = call i32 @free(i8* %12)
%14 = icmp eq i32* %11, null
br i1 %14, label %26, label %15
15: ; preds = %9
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%17 = call i8* @fgets(i8* %16, i32 256, i32* %11)
%18 = call i32 @fclose(i32* %11)
%19 = icmp eq i8* %17, null
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %24
22: ; preds = %15
%23 = call i8* @strdup(i8* %16)
br label %24
24: ; preds = %22, %20
%25 = phi i8* [ %21, %20 ], [ %23, %22 ]
br label %28
26: ; preds = %9
%27 = call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %24, %26, %7
%29 = phi i8* [ %8, %7 ], [ %25, %24 ], [ %27, %26 ]
ret i8* %29
}