#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ isspace(char);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strdup(char *);
scalar_t__ strncmp(char *, char *, int);
char *getProcName(int pid) {
  char path[40], line[100], *p;
  FILE *statusf;
  snprintf(path, 40, "/proc/%d/status", pid);
  printf("opening proc %s\n", path);
  statusf = fopen(path, "r");
  if (!statusf) {
    printf("not found file.\n");
    return NULL;
  }
  while (fgets(line, 100, statusf)) {
    if (strncmp(line, "Name:", 5) != 0)
      continue;
    p = line + 6;
    while (isspace(*p))
      ++p;
    break;
  }
  fclose(statusf);
  printf("name is %s\n", p);
  return strdup(p);
}


x86 assembly (clang, -O0):
<getProcName>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0xc(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x28,%esi
lea    0xe5f(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x40(%rbp),%rsi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x40(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0xc0(%rbp)
cmpq   $0x0,-0xc0(%rbp)
jne    11f8 <getProcName+0x78>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12be <getProcName+0x13e>
jmp    11fd <getProcName+0x7d>
lea    -0xb0(%rbp),%rdi
mov    -0xc0(%rbp),%rdx
mov    $0x64,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     128d <getProcName+0x10d>
lea    -0xb0(%rbp),%rdi
lea    0xe07(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1246 <getProcName+0xc6>
jmp    11fd <getProcName+0x7d>
lea    -0xb0(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0xb8(%rbp)
mov    -0xb8(%rbp),%rax
movsbl (%rax),%edi
call   1070 <isspace@plt>
cmp    $0x0,%rax
je     1288 <getProcName+0x108>
mov    -0xb8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xb8(%rbp)
jmp    1258 <getProcName+0xd8>
jmp    128d <getProcName+0x10d>
mov    -0xc0(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0xb8(%rbp),%rsi
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xb8(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getProcName>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %edi,%ecx
lea    0xe59(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x28,%esi
call   1070 <snprintf@plt>
lea    0xe52(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe52(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1255 <getProcName+0xc5>
mov    %rax,%r14
lea    0x30(%rsp),%r15
lea    0xe38(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x64,%esi
mov    %r14,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     1232 <getProcName+0xa2>
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11f0 <getProcName+0x60>
lea    0x35(%rsp),%rbx
nop
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1080 <isspace@plt>
test   %rax,%rax
jne    1220 <getProcName+0x90>
mov    %r14,%rdi
call   1050 <fclose@plt>
lea    0xde8(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   10b0 <strdup@plt>
jmp    1263 <getProcName+0xd3>
lea    0xdd9(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/%d/status\00"
@.str.1 = [17 x i8] c"opening proc %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [17 x i8] c"not found file.\0A\00"
@.str.4 = [6 x i8] c"Name:\00"
@.str.5 = [12 x i8] c"name is %s\0A\00"
define i8* @getProcName(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [40 x i8]
%5 = alloca [100 x i8]
%6 = alloca i8*
%7 = alloca i32*
store i32 %0, i32* %3
%8 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %8, i32 40, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %19, label %17
17: ; preds = %1
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i8* null, i8** %2
br label %49
19: ; preds = %1
br label %20
20: ; preds = %29, %19
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%22 = load i32*, i32** %7
%23 = call i64 @fgets(i8* %21, i32 100, i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %42
25: ; preds = %20
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%27 = call i64 @strncmp(i8* %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 5)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
br label %20
30: ; preds = %25
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%32 = getelementptr inbounds i8, i8* %31, i64 6
store i8* %32, i8** %6
br label %33
33: ; preds = %38, %30
%34 = load i8*, i8** %6
%35 = load i8, i8* %34
%36 = call i64 @isspace(i8 signext %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %6
br label %33
41: ; preds = %33
br label %42
42: ; preds = %41, %20
%43 = load i32*, i32** %7
%44 = call i32 @fclose(i32* %43)
%45 = load i8*, i8** %6
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %45)
%47 = load i8*, i8** %6
%48 = call i8* @strdup(i8* %47)
store i8* %48, i8** %2
br label %49
49: ; preds = %42, %17
%50 = load i8*, i8** %2
ret i8* %50
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/%d/status\00"
@.str.1 = [17 x i8] c"opening proc %s\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.4 = [6 x i8] c"Name:\00"
@.str.5 = [12 x i8] c"name is %s\0A\00"
@str = [16 x i8] c"not found file.\00"
define noalias i8* @getProcName(i32 %0) {
%2 = alloca [40 x i8]
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [40 x i8], [40 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 40, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%8 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %31
12: ; preds = %1, %15
%13 = call i64 @fgets(i8* %5, i32 100, i32* %8)
%14 = icmp eq i64 %13, 0
br i1 %14, label %26, label %15
15: ; preds = %12
%16 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 5)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %12
18: ; preds = %15
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 6
br label %20
20: ; preds = %20, %18
%21 = phi i8* [ %19, %18 ], [ %25, %20 ]
%22 = load i8, i8* %21
%23 = call i64 @isspace(i8 signext %22)
%24 = icmp eq i64 %23, 0
%25 = getelementptr inbounds i8, i8* %21, i64 1
br i1 %24, label %26, label %20
26: ; preds = %12, %20
%27 = phi i8* [ %21, %20 ], [ undef, %12 ]
%28 = call i32 @fclose(i32* %8)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %27)
%30 = call i8* @strdup(i8* %27)
br label %31
31: ; preds = %26, %10
%32 = phi i8* [ %30, %26 ], [ null, %10 ]
ret i8* %32
}