#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 */
scalar_t__ calloc(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);
const char *get_process_name_by_pid(const int pid) {
  char *name = (char *)calloc(1024, sizeof(char));
  if (name != NULL) {
    sprintf(name, "/proc/%d/cmdline", pid);
    FILE *f = fopen(name, "r");
    if (f != NULL) {
      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    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x400,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1211 <get_process_name_by_pid+0xc1>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe79(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     120c <get_process_name_by_pid+0xbc>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    1203 <get_process_name_by_pid+0xb3>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0xa,%eax
cmp    %ecx,%eax
jne    11fe <get_process_name_by_pid+0xae>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1203 <get_process_name_by_pid+0xb3>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1211 <get_process_name_by_pid+0xc1>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_process_name_by_pid>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
mov    $0x400,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11c3 <get_process_name_by_pid+0x73>
lea    0xe8c(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
lea    0xe8c(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11c3 <get_process_name_by_pid+0x73>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11bb <get_process_name_by_pid+0x6b>
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11bb <get_process_name_by_pid+0x6b>
movb   $0x0,-0x1(%rbx,%rax,1)
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
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) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
store i32 %0, i32* %2
%6 = call i64 @calloc(i32 1024, i32 1)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %42
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = load i32, i32* %2
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i8*, i8** %3
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %4
%16 = load i32*, i32** %4
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %41
18: ; preds = %10
%19 = load i8*, i8** %3
%20 = load i32*, i32** %4
%21 = call i64 @fread(i8* %19, i32 1, i32 1024, i32* %20)
store i64 %21, i64* %5
%22 = load i64, i64* %5
%23 = icmp ugt i64 %22, 0
br i1 %23, label %24, label %38
24: ; preds = %18
%25 = load i8*, i8** %3
%26 = load i64, i64* %5
%27 = sub i64 %26, 1
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 10, %30
br i1 %31, label %32, label %37
32: ; preds = %24
%33 = load i8*, i8** %3
%34 = load i64, i64* %5
%35 = sub i64 %34, 1
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %32, %24
br label %38
38: ; preds = %37, %18
%39 = load i32*, i32** %4
%40 = call i32 @fclose(i32* %39)
br label %41
41: ; preds = %38, %10
br label %42
42: ; preds = %41, %1
%43 = load i8*, i8** %3
ret i8* %43
}

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) {
%2 = tail call i64 @calloc(i32 1024, i32 1)
%3 = inttoptr i64 %2 to i8*
%4 = icmp eq i64 %2, 0
br i1 %4, label %20, label %5
5: ; preds = %1
%6 = tail call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %20, label %9
9: ; preds = %5
%10 = tail call i64 @fread(i8* %3, i32 1, i32 1024, i32* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %9
%13 = add i64 %10, -1
%14 = getelementptr inbounds i8, i8* %3, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 10
br i1 %16, label %17, label %18
17: ; preds = %12
store i8 0, i8* %14
br label %18
18: ; preds = %12, %17, %9
%19 = tail call i32 @fclose(i32* %7)
br label %20
20: ; preds = %5, %18, %1
ret i8* %3
}