#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 MAXPATHLEN;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
char const *sasutil_extract_name(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
const char *sasutil_get_proc_name(pid_t pid) {
  char path[MAXPATHLEN];
  static char procname[512];
  FILE *arq;
  procname[0] = '\0';
  snprintf(path, MAXPATHLEN, "/proc/%d/cmdline", pid);
  arq = fopen(path, "r");
  if (!arq) {
    path[0] = '\0';
  } else {
    int ret = fread(procname, 512, 1, arq);
    fclose(arq);
    if (ret >= 1)
      return sasutil_extract_name(procname);
  }
  return procname;
}


x86 assembly (clang, -O0):
<sasutil_get_proc_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
movb   $0x0,0x2ee2(%rip)
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%ecx
lea    0xe5f(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xe60(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d1 <sasutil_get_proc_name+0x81>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
jmp    1222 <sasutil_get_proc_name+0xd2>
mov    -0x28(%rbp),%rcx
lea    0x2e94(%rip),%rdi
mov    $0x200,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x1,-0x2c(%rbp)
jl     121d <sasutil_get_proc_name+0xcd>
lea    0x2e68(%rip),%rdi
call   1030 <sasutil_extract_name@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    1234 <sasutil_get_proc_name+0xe4>
jmp    1222 <sasutil_get_proc_name+0xd2>
lea    0x2e47(%rip),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sasutil_get_proc_name>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ecx
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movb   $0x0,0x2ef0(%rip)
lea    0xe79(%rip),%rdx
mov    %rbx,%rdi
call   1060 <snprintf@plt>
lea    0xe7b(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e3 <sasutil_get_proc_name+0x93>
mov    %rax,%r14
lea    0x2ec3(%rip),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1050 <fclose@plt>
test   %r15d,%r15d
jle    11ed <sasutil_get_proc_name+0x9d>
lea    0x2e97(%rip),%rdi
call   1030 <sasutil_extract_name@plt>
mov    %rax,%rbx
jmp    11ed <sasutil_get_proc_name+0x9d>
movb   $0x0,(%rbx)
lea    0x2e83(%rip),%rbx
mov    %rbx,%rax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MAXPATHLEN = global i32 0
@sasutil_get_proc_name.procname = internal global [512 x i8] zeroinitializer
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @sasutil_get_proc_name(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
%9 = load i32, i32* @MAXPATHLEN
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %4
%12 = alloca i8, i64 %10
store i64 %10, i64* %5
store i8 0, i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sasutil_get_proc_name.procname, i64 0, i64 0)
%13 = load i32, i32* @MAXPATHLEN
%14 = load i32, i32* %3
%15 = call i32 @snprintf(i8* %12, i32 %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp ne i32* %17, null
br i1 %18, label %21, label %19
19: ; preds = %1
%20 = getelementptr inbounds i8, i8* %12, i64 0
store i8 0, i8* %20
br label %31
21: ; preds = %1
%22 = load i32*, i32** %6
%23 = call i32 @fread(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sasutil_get_proc_name.procname, i64 0, i64 0), i32 512, i32 1, i32* %22)
store i32 %23, i32* %7
%24 = load i32*, i32** %6
%25 = call i32 @fclose(i32* %24)
%26 = load i32, i32* %7
%27 = icmp sge i32 %26, 1
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = call i8* @sasutil_extract_name(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sasutil_get_proc_name.procname, i64 0, i64 0))
store i8* %29, i8** %2
store i32 1, i32* %8
br label %32
30: ; preds = %21
br label %31
31: ; preds = %30, %19
store i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sasutil_get_proc_name.procname, i64 0, i64 0), i8** %2
store i32 1, i32* %8
br label %32
32: ; preds = %31, %28
%33 = load i8*, i8** %4
%34 = load i8*, i8** %2
ret i8* %34
}

LLVM IR (-O2):
@MAXPATHLEN = global i32 0
@sasutil_get_proc_name.procname = internal global [512 x i8] zeroinitializer
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @sasutil_get_proc_name(i32 %0) {
%2 = load i32, i32* @MAXPATHLEN
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
store i8 0, i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sasutil_get_proc_name.procname, i64 0, i64 0)
%5 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i8 0, i8* %4
br label %15
9: ; preds = %1
%10 = call i32 @fread(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sasutil_get_proc_name.procname, i64 0, i64 0), i32 512, i32 1, i32* %6)
%11 = call i32 @fclose(i32* %6)
%12 = icmp sgt i32 %10, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i8* @sasutil_extract_name(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @sasutil_get_proc_name.procname, i64 0, i64 0))
br label %15
15: ; preds = %8, %9, %13
%16 = phi i8* [ %14, %13 ], [ getelementptr inbounds ([512 x i8], [512 x i8]* @sasutil_get_proc_name.procname, i64 0, i64 0), %9 ], [ getelementptr inbounds ([512 x i8], [512 x i8]* @sasutil_get_proc_name.procname, i64 0, i64 0), %8 ]
ret i8* %16
}