#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 PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char **);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
int strlen(char *);
char *grab_full_cmd_name_of_pid(pid_t pid, int *sz) {
  char cmdfile[PATH_MAX + 1];
  FILE *f;
  char *ret = 0;
  snprintf(cmdfile, PATH_MAX, "%s/%d/cmdline", "/proc", pid);
  cmdfile[PATH_MAX] = 0;
  f = fopen(cmdfile, "r");
  if (!f) {
    return ret;
  }
  fscanf(f, "%as", &ret);
  fclose(f);
  if (sz)
    *sz = (ret ? strlen(ret) : 0);
  return ret;
}


x86 assembly (clang, -O0):
<grab_full_cmd_name_of_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%r8d
lea    0xe56(%rip),%rdx
lea    0xe5d(%rip),%rcx
call   1050 <snprintf@plt>
mov    -0x48(%rbp),%rdi
mov    0x2e2f(%rip),%rax
movslq (%rax),%rax
movb   $0x0,(%rdi,%rax,1)
lea    0xe45(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f7 <grab_full_cmd_name_of_pid+0xa7>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    125d <grab_full_cmd_name_of_pid+0x10d>
mov    -0x30(%rbp),%rdi
lea    0xe14(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   1060 <fscanf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x18(%rbp)
je     124e <grab_full_cmd_name_of_pid+0xfe>
cmpq   $0x0,-0x38(%rbp)
je     123b <grab_full_cmd_name_of_pid+0xeb>
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x4c(%rbp)
jmp    1245 <grab_full_cmd_name_of_pid+0xf5>
xor    %eax,%eax
mov    %eax,-0x4c(%rbp)
jmp    1245 <grab_full_cmd_name_of_pid+0xf5>
mov    -0x4c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grab_full_cmd_name_of_pid>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r8d
mov    0x2e89(%rip),%r15
mov    (%r15),%esi
lea    0x1(%rsi),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movq   $0x0,-0x20(%rbp)
lea    0xe73(%rip),%rdx
lea    0xe7a(%rip),%rcx
mov    %rbx,%rdi
call   1050 <snprintf@plt>
movslq (%r15),%rax
movb   $0x0,(%rax,%rbx,1)
lea    0xe6a(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ef <grab_full_cmd_name_of_pid+0x9f>
mov    %rax,%rbx
lea    0xe55(%rip),%rsi
lea    -0x20(%rbp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %r14,%r14
je     11ef <grab_full_cmd_name_of_pid+0x9f>
mov    -0x20(%rbp),%rdi
test   %rdi,%rdi
je     11ea <grab_full_cmd_name_of_pid+0x9a>
call   1040 <strlen@plt>
jmp    11ec <grab_full_cmd_name_of_pid+0x9c>
xor    %eax,%eax
mov    %eax,(%r14)
mov    -0x20(%rbp),%rax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [14 x i8] c"%s/%d/cmdline\00"
@.str.1 = [6 x i8] c"/proc\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [4 x i8] c"%as\00"
define i8* @grab_full_cmd_name_of_pid(i32 %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%11 = load i32, i32* @PATH_MAX
%12 = add nsw i32 %11, 1
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
store i8* null, i8** %9
%16 = load i32, i32* @PATH_MAX
%17 = load i32, i32* %4
%18 = call i32 @snprintf(i8* %15, i32 %16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = load i32, i32* @PATH_MAX
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %15, i64 %20
store i8 0, i8* %21
%22 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %22, i32** %8
%23 = load i32*, i32** %8
%24 = icmp ne i32* %23, null
br i1 %24, label %27, label %25
25: ; preds = %2
%26 = load i8*, i8** %9
store i8* %26, i8** %3
store i32 1, i32* %10
br label %46
27: ; preds = %2
%28 = load i32*, i32** %8
%29 = call i32 @fscanf(i32* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %9)
%30 = load i32*, i32** %8
%31 = call i32 @fclose(i32* %30)
%32 = load i32*, i32** %5
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %44
34: ; preds = %27
%35 = load i8*, i8** %9
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i8*, i8** %9
%39 = call i32 @strlen(i8* %38)
br label %41
40: ; preds = %34
br label %41
41: ; preds = %40, %37
%42 = phi i32 [ %39, %37 ], [ 0, %40 ]
%43 = load i32*, i32** %5
store i32 %42, i32* %43
br label %44
44: ; preds = %41, %27
%45 = load i8*, i8** %9
store i8* %45, i8** %3
store i32 1, i32* %10
br label %46
46: ; preds = %44, %25
%47 = load i8*, i8** %6
%48 = load i8*, i8** %3
ret i8* %48
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [14 x i8] c"%s/%d/cmdline\00"
@.str.1 = [6 x i8] c"/proc\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [4 x i8] c"%as\00"
define i8* @grab_full_cmd_name_of_pid(i32 %0, i32* writeonly %1) {
%3 = alloca i8*
%4 = load i32, i32* @PATH_MAX
%5 = add nsw i32 %4, 1
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%9 = call i32 @snprintf(i8* %7, i32 %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%10 = load i32, i32* @PATH_MAX
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %7, i64 %11
store i8 0, i8* %12
%13 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %26, label %15
15: ; preds = %2
%16 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8** %3)
%17 = call i32 @fclose(i32* %13)
%18 = icmp eq i32* %1, null
br i1 %18, label %26, label %19
19: ; preds = %15
%20 = load i8*, i8** %3
%21 = icmp eq i8* %20, null
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = call i32 @strlen(i8* %20)
br label %24
24: ; preds = %19, %22
%25 = phi i32 [ %23, %22 ], [ 0, %19 ]
store i32 %25, i32* %1
br label %26
26: ; preds = %15, %24, %2
%27 = load i8*, i8** %3
ret i8* %27
}