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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strtok(char *, char *);
char *get_name_of(pid_t pid, char *nameof_buff, int size) {
  char statfile[20];
  sprintf(statfile, "/proc/%d/stat", pid);
  FILE *fd = fopen(statfile, "r");
  if (fd == NULL)
    return NULL;
  if (fgets(nameof_buff, size, fd) == NULL) {
    fclose(fd);
    return NULL;
  }
  fclose(fd);
  char *buffer = strtok(nameof_buff, " ");
  buffer = strtok(NULL, " ");
  return buffer;
}


x86 assembly (clang, -O0):
<get_name_of>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe90(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x30(%rbp),%rdi
lea    0xe8e(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11a1 <get_name_of+0x51>
movq   $0x0,-0x8(%rbp)
jmp    120a <get_name_of+0xba>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x38(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    11d1 <get_name_of+0x81>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    120a <get_name_of+0xba>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
lea    0xe2b(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe17(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_name_of>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%edx
lea    0xe9a(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xe96(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11c9 <get_name_of+0x79>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rbp,%rbp
je     11c9 <get_name_of+0x79>
lea    0xe64(%rip),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <strtok@plt>
xor    %edi,%edi
mov    %rbx,%rsi
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <strtok@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c" \00"
define i8* @get_name_of(i32 %0, i8* %1, i32 %2) {
%4 = alloca [20 x i8]
%5 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %5, 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 %16, label %9
9: ; preds = %3
%10 = tail call i32* @fgets(i8* %1, i32 %2, i32* %7)
%11 = icmp eq i32* %10, null
%12 = tail call i32 @fclose(i32* %7)
br i1 %11, label %16, label %13
13: ; preds = %9
%14 = tail call i8* @strtok(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %9, %3, %13
%17 = phi i8* [ %15, %13 ], [ null, %3 ], [ null, %9 ]
ret i8* %17
}