#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ asprintf(char **, char *, long);
int /*<<< orphan*/ fgets(char *, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ sprintf(char *, char *);
long strlen(char *);
char *getProcessNameFromPid(long pid, char *data, long size) {
  char *fileName;
  asprintf(&fileName, "/proc/%d/comm", pid);
  FILE *file = fopen(fileName, "rt");
  if (file) {
    fgets(data, size, file);
    long length = strlen(data);
    if (length > 0) {
      data[length - 1] = '\0';
    }
  } else {
    sprintf(data, "__unknown");
  }
  free(fileName);
  return data;
}


x86 assembly (clang, -O0):
<getProcessNameFromPid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1060 <asprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11e5 <getProcessNameFromPid+0x85>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <fgets@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jle    11e0 <getProcessNameFromPid+0x80>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11f5 <getProcessNameFromPid+0x95>
mov    -0x10(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getProcessNameFromPid>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rdx
lea    0xe9c(%rip),%rsi
mov    %rsp,%rdi
call   1060 <asprintf@plt>
mov    (%rsp),%rdi
lea    0xe97(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11a3 <getProcessNameFromPid+0x53>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
jle    11b6 <getProcessNameFromPid+0x66>
movb   $0x0,-0x1(%rax,%rbx,1)
jmp    11b6 <getProcessNameFromPid+0x66>
movabs $0x776f6e6b6e755f5f,%rax
6e 6f 77
mov    %rax,(%rbx)
movw   $0x6e,0x8(%rbx)
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/comm\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [10 x i8] c"__unknown\00"
define i8* @getProcessNameFromPid(i64 %0, i8* returned %1, i64 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 %0)
%7 = load i8*, i8** %4
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %17, label %10
10: ; preds = %3
%11 = call i32 @fgets(i8* %1, i64 %2, i32* %8)
%12 = call i64 @strlen(i8* dereferenceable(1) %1)
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = add nsw i64 %12, -1
%16 = getelementptr inbounds i8, i8* %1, i64 %15
store i8 0, i8* %16
br label %18
17: ; preds = %3
br label %18
18: ; preds = %10, %14, %17
%19 = load i8*, i8** %4
%20 = call i32 @free(i8* %19)
ret i8* %1
}