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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ isspace(char);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int strlen(char *);
char *strstr(char *, char *);
char *command_name_for_pid(int pid) {
  static char buf[128];
  char fname[64];
  snprintf(fname, sizeof(fname), "/proc/%d/status", pid);
  FILE *fs = fopen(fname, "r");
  if (!fs) {
    return NULL;
  } else {
    while (fgets(buf, 128, fs)) {
      if (strstr(buf, "Name:") == buf) {
        char *p = &buf[5];
        while (isspace(*p)) {
          p++;
        }
        if (p[strlen(p) - 1] == '\n') {
          p[strlen(p) - 1] = '\0';
        }
        fclose(fs);
        return p;
      }
    }
    fclose(fs);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<command_name_for_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x40,%esi
lea    0xe72(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    11bf <command_name_for_pid+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    12b3 <command_name_for_pid+0x143>
jmp    11c4 <command_name_for_pid+0x54>
mov    -0x58(%rbp),%rdx
lea    0x2ea1(%rip),%rdi
mov    $0x80,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12a2 <command_name_for_pid+0x132>
lea    0x2e86(%rip),%rdi
lea    0xe21(%rip),%rsi
call   1090 <strstr@plt>
lea    0x2e73(%rip),%rcx
cmp    %rcx,%rax
jne    129d <command_name_for_pid+0x12d>
lea    0x2e63(%rip),%rax
add    $0x5,%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
movsbl (%rax),%edi
call   1060 <isspace@plt>
cmp    $0x0,%rax
je     123c <command_name_for_pid+0xcc>
mov    -0x60(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x60(%rbp)
jmp    1215 <command_name_for_pid+0xa5>
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x68(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1287 <command_name_for_pid+0x117>
mov    -0x60(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x58(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x60(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12b3 <command_name_for_pid+0x143>
jmp    11c4 <command_name_for_pid+0x54>
mov    -0x58(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<command_name_for_pid>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ecx
lea    0xe7c(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1060 <snprintf@plt>
lea    0xe75(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     123c <command_name_for_pid+0xcc>
mov    %rax,%r14
lea    0x2eba(%rip),%r15
xor    %ebx,%ebx
lea    0xe53(%rip),%r12
nop
mov    %r15,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1232 <command_name_for_pid+0xc2>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11c0 <command_name_for_pid+0x50>
lea    0x2e7e(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1070 <isspace@plt>
test   %rax,%rax
jne    1200 <command_name_for_pid+0x90>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1232 <command_name_for_pid+0xc2>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    123e <command_name_for_pid+0xce>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@command_name_for_pid.buf = internal global [128 x i8] zeroinitializer
@.str = [16 x i8] c"/proc/%d/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"Name:\00"
define i8* @command_name_for_pid(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = alloca i32*
%6 = alloca i8*
store i32 %0, i32* %3
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 64, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %1
store i8* null, i8** %2
br label %58
15: ; preds = %1
br label %16
16: ; preds = %53, %15
%17 = load i32*, i32** %5
%18 = call i64 @fgets(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @command_name_for_pid.buf, i64 0, i64 0), i32 128, i32* %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %54
20: ; preds = %16
%21 = call i8* @strstr(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @command_name_for_pid.buf, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i8* %21, getelementptr inbounds ([128 x i8], [128 x i8]* @command_name_for_pid.buf, i64 0, i64 0)
br i1 %22, label %23, label %53
23: ; preds = %20
store i8* getelementptr inbounds ([128 x i8], [128 x i8]* @command_name_for_pid.buf, i64 0, i64 5), i8** %6
br label %24
24: ; preds = %29, %23
%25 = load i8*, i8** %6
%26 = load i8, i8* %25
%27 = call i64 @isspace(i8 signext %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i8*, i8** %6
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %6
br label %24
32: ; preds = %24
%33 = load i8*, i8** %6
%34 = load i8*, i8** %6
%35 = call i32 @strlen(i8* %34)
%36 = sub nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %33, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 10
br i1 %41, label %42, label %49
42: ; preds = %32
%43 = load i8*, i8** %6
%44 = load i8*, i8** %6
%45 = call i32 @strlen(i8* %44)
%46 = sub nsw i32 %45, 1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %43, i64 %47
store i8 0, i8* %48
br label %49
49: ; preds = %42, %32
%50 = load i32*, i32** %5
%51 = call i32 @fclose(i32* %50)
%52 = load i8*, i8** %6
store i8* %52, i8** %2
br label %58
53: ; preds = %20
br label %16
54: ; preds = %16
%55 = load i32*, i32** %5
%56 = call i32 @fclose(i32* %55)
br label %57
57: ; preds = %54
store i8* null, i8** %2
br label %58
58: ; preds = %57, %49, %14
%59 = load i8*, i8** %2
ret i8* %59
}

LLVM IR (-O2):
@command_name_for_pid.buf = internal global [128 x i8] zeroinitializer
@.str = [16 x i8] c"/proc/%d/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"Name:\00"
define i8* @command_name_for_pid(i32 %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 64, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %35, label %7
7: ; preds = %1, %10
%8 = call i64 @fgets(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @command_name_for_pid.buf, i64 0, i64 0), i32 128, i32* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %32, label %10
10: ; preds = %7
%11 = call i64 bitcast (i32 (i8*)* @strlen to i64 (i8*)*)(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 @strncmp(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @command_name_for_pid.buf, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %7
14: ; preds = %10, %14
%15 = phi i8* [ %19, %14 ], [ getelementptr inbounds ([128 x i8], [128 x i8]* @command_name_for_pid.buf, i64 0, i64 5), %10 ]
%16 = load i8, i8* %15
%17 = call i64 @isspace(i8 signext %16)
%18 = icmp eq i64 %17, 0
%19 = getelementptr inbounds i8, i8* %15, i64 1
br i1 %18, label %20, label %14
20: ; preds = %14
%21 = call i32 @strlen(i8* %15)
%22 = add nsw i32 %21, -1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %15, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 10
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = call i32 @strlen(i8* %15)
%29 = add nsw i32 %28, -1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %15, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %7, %20, %27
%33 = phi i8* [ %15, %27 ], [ %15, %20 ], [ null, %7 ]
%34 = call i32 @fclose(i32* %5)
br label %35
35: ; preds = %32, %1
%36 = phi i8* [ null, %1 ], [ %33, %32 ]
ret i8* %36
}