#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 */
char *ecalloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int const);
char *get_process_name_by_pid(const int pid, char **unused) {
  if (unused) {
    *unused = ecalloc(1, 1);
  }
  char *name = ecalloc(1024, sizeof(char));
  if (name) {
    sprintf(name, "/proc/%d/cmdline", pid);
    FILE *f = fopen(name, "r");
    if (f) {
      size_t size;
      size = fread(name, sizeof(char), 1024, f);
      if (size > 0) {
        if ('\n' == name[size - 1])
          name[size - 1] = '\0';
      }
      fclose(f);
    }
  }
  return name;
}


x86 assembly (clang, -O0):
<get_process_name_by_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1180 <get_process_name_by_pid+0x30>
mov    $0x1,%esi
mov    %esi,%edi
call   1040 <ecalloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    $0x400,%edi
mov    $0x1,%esi
call   1040 <ecalloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1236 <get_process_name_by_pid+0xe6>
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe54(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1231 <get_process_name_by_pid+0xe1>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    1228 <get_process_name_by_pid+0xd8>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0xa,%eax
cmp    %ecx,%eax
jne    1223 <get_process_name_by_pid+0xd3>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1228 <get_process_name_by_pid+0xd8>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
jmp    1236 <get_process_name_by_pid+0xe6>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_process_name_by_pid>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
test   %rsi,%rsi
je     116f <get_process_name_by_pid+0x1f>
mov    %rsi,%rbx
mov    $0x1,%edi
mov    $0x1,%esi
call   1040 <ecalloc@plt>
mov    %rax,(%rbx)
mov    $0x400,%edi
mov    $0x1,%esi
call   1040 <ecalloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11dc <get_process_name_by_pid+0x8c>
lea    0xe73(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
lea    0xe73(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11dc <get_process_name_by_pid+0x8c>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11d4 <get_process_name_by_pid+0x84>
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11d4 <get_process_name_by_pid+0x84>
movb   $0x0,-0x1(%rbx,%rax,1)
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @get_process_name_by_pid(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i64
store i32 %0, i32* %3
store i8** %1, i8*** %4
%8 = load i8**, i8*** %4
%9 = icmp ne i8** %8, null
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i8* @ecalloc(i32 1, i32 1)
%12 = load i8**, i8*** %4
store i8* %11, i8** %12
br label %13
13: ; preds = %10, %2
%14 = call i8* @ecalloc(i32 1024, i32 1)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %49
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = load i32, i32* %3
%20 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i8*, i8** %5
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp ne i32* %23, null
br i1 %24, label %25, label %48
25: ; preds = %17
%26 = load i8*, i8** %5
%27 = load i32*, i32** %6
%28 = call i64 @fread(i8* %26, i32 1, i32 1024, i32* %27)
store i64 %28, i64* %7
%29 = load i64, i64* %7
%30 = icmp ugt i64 %29, 0
br i1 %30, label %31, label %45
31: ; preds = %25
%32 = load i8*, i8** %5
%33 = load i64, i64* %7
%34 = sub i64 %33, 1
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 10, %37
br i1 %38, label %39, label %44
39: ; preds = %31
%40 = load i8*, i8** %5
%41 = load i64, i64* %7
%42 = sub i64 %41, 1
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %39, %31
br label %45
45: ; preds = %44, %25
%46 = load i32*, i32** %6
%47 = call i32 @fclose(i32* %46)
br label %48
48: ; preds = %45, %17
br label %49
49: ; preds = %48, %13
%50 = load i8*, i8** %5
ret i8* %50
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @get_process_name_by_pid(i32 %0, i8** writeonly %1) {
%3 = icmp eq i8** %1, null
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i8* @ecalloc(i32 1, i32 1)
store i8* %5, i8** %1
br label %6
6: ; preds = %4, %2
%7 = tail call i8* @ecalloc(i32 1024, i32 1)
%8 = icmp eq i8* %7, null
br i1 %8, label %24, label %9
9: ; preds = %6
%10 = tail call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%11 = tail call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %24, label %13
13: ; preds = %9
%14 = tail call i64 @fread(i8* %7, i32 1, i32 1024, i32* %11)
%15 = icmp eq i64 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %13
%17 = add i64 %14, -1
%18 = getelementptr inbounds i8, i8* %7, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 10
br i1 %20, label %21, label %22
21: ; preds = %16
store i8 0, i8* %18
br label %22
22: ; preds = %16, %21, %13
%23 = tail call i32 @fclose(i32* %11)
br label %24
24: ; preds = %9, %22, %6
ret i8* %7
}