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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int asprintf(char **, char *, int /*<<< orphan*/ const);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
size_t read(int, unsigned char *, int);
char *strdup(char *);
scalar_t__ strlen(char *);
char *pid_proc_cmdline(const pid_t pid) {
  char *fname;
  int fd;
  if (asprintf(&fname, "/proc/%d/cmdline", pid) == -1)
    return NULL;
  if ((fd = open(fname, O_RDONLY)) < 0) {
    free(fname);
    return NULL;
  }
  free(fname);
  unsigned char buffer[4096];
  ssize_t len;
  if ((len = read(fd, buffer, sizeof(buffer) - 1)) <= 0) {
    close(fd);
    return NULL;
  }
  buffer[len] = '\0';
  close(fd);
  int i;
  for (i = 0; i < len; i++) {
    if (buffer[i] == '\0')
      buffer[i] = ' ';
    if (buffer[i] >= 0x80)
      return NULL;
  }
  char *rv = strdup((char *)buffer);
  if (strlen(rv) == 0) {
    free(rv);
    return NULL;
  }
  return rv;
}


x86 assembly (clang, -O0):
<pid_proc_cmdline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11a7 <pid_proc_cmdline+0x37>
movq   $0x0,-0x8(%rbp)
jmp    1307 <pid_proc_cmdline+0x197>
mov    -0x18(%rbp),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11db <pid_proc_cmdline+0x6b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1307 <pid_proc_cmdline+0x197>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0xfff,%edx
call   1060 <read@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
ja     121e <pid_proc_cmdline+0xae>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1307 <pid_proc_cmdline+0x197>
mov    -0x1028(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x102c(%rbp)
movslq -0x102c(%rbp),%rax
cmp    -0x1028(%rbp),%rax
jae    12ba <pid_proc_cmdline+0x14a>
movslq -0x102c(%rbp),%rax
movzbl -0x1020(%rbp,%rax,1),%eax
ff
cmp    $0x0,%eax
jne    127a <pid_proc_cmdline+0x10a>
movslq -0x102c(%rbp),%rax
movb   $0x20,-0x1020(%rbp,%rax,1)
20
movslq -0x102c(%rbp),%rax
movzbl -0x1020(%rbp,%rax,1),%eax
ff
cmp    $0x80,%eax
jl     12a1 <pid_proc_cmdline+0x131>
movq   $0x0,-0x8(%rbp)
jmp    1307 <pid_proc_cmdline+0x197>
jmp    12a6 <pid_proc_cmdline+0x136>
mov    -0x102c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x102c(%rbp)
jmp    123f <pid_proc_cmdline+0xcf>
lea    -0x1020(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x1038(%rbp)
mov    -0x1038(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    12fc <pid_proc_cmdline+0x18c>
mov    -0x1038(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1307 <pid_proc_cmdline+0x197>
mov    -0x1038(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_proc_cmdline>:
push   %rbp
push   %rbx
sub    $0x1018,%rsp
mov    %edi,%edx
lea    0xe8e(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
je     121e <pid_proc_cmdline+0xbe>
mov    0x8(%rsp),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
test   %ebp,%ebp
js     121e <pid_proc_cmdline+0xbe>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xfff,%edx
call   1050 <read@plt>
test   %rax,%rax
je     1217 <pid_proc_cmdline+0xb7>
mov    %rax,%rbx
movb   $0x0,0x10(%rsp,%rax,1)
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
mov    $0x20,%ecx
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
movzbl 0x10(%rsp,%rdx,1),%esi
test   %sil,%sil
cmove  %ecx,%esi
mov    %sil,0x10(%rsp,%rdx,1)
test   %sil,%sil
js     1220 <pid_proc_cmdline+0xc0>
add    $0x1,%rdx
cmp    %rdx,%rbx
jne    11e0 <pid_proc_cmdline+0x80>
lea    0x10(%rsp),%rdi
call   1080 <strdup@plt>
cmpb   $0x0,(%rax)
jne    1220 <pid_proc_cmdline+0xc0>
mov    %rax,%rdi
call   1030 <free@plt>
jmp    121e <pid_proc_cmdline+0xbe>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@O_RDONLY = global i32 0
define i8* @pid_proc_cmdline(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [4096 x i8]
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %3
%10 = load i32, i32* %3
%11 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i8* null, i8** %2
br label %76
14: ; preds = %1
%15 = load i8*, i8** %4
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i8*, i8** %4
%21 = call i32 @free(i8* %20)
store i8* null, i8** %2
br label %76
22: ; preds = %14
%23 = load i8*, i8** %4
%24 = call i32 @free(i8* %23)
%25 = load i32, i32* %5
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%27 = call i64 @read(i32 %25, i8* %26, i32 4095)
store i64 %27, i64* %7
%28 = icmp ule i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
store i8* null, i8** %2
br label %76
32: ; preds = %22
%33 = load i64, i64* %7
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 %33
store i8 0, i8* %34
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
store i32 0, i32* %8
br label %37
37: ; preds = %62, %32
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = load i64, i64* %7
%41 = icmp ult i64 %39, %40
br i1 %41, label %42, label %65
42: ; preds = %37
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 %44
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %42
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 %51
store i8 32, i8* %52
br label %53
53: ; preds = %49, %42
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = zext i8 %57 to i32
%59 = icmp sge i32 %58, 128
br i1 %59, label %60, label %61
60: ; preds = %53
store i8* null, i8** %2
br label %76
61: ; preds = %53
br label %62
62: ; preds = %61
%63 = load i32, i32* %8
%64 = add nsw i32 %63, 1
store i32 %64, i32* %8
br label %37
65: ; preds = %37
%66 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%67 = call i8* @strdup(i8* %66)
store i8* %67, i8** %9
%68 = load i8*, i8** %9
%69 = call i64 @strlen(i8* %68)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %65
%72 = load i8*, i8** %9
%73 = call i32 @free(i8* %72)
store i8* null, i8** %2
br label %76
74: ; preds = %65
%75 = load i8*, i8** %9
store i8* %75, i8** %2
br label %76
76: ; preds = %74, %71, %60, %29, %19, %13
%77 = load i8*, i8** %2
ret i8* %77
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@O_RDONLY = global i32 0
define noalias i8* @pid_proc_cmdline(i32 %0) {
%2 = alloca i8*
%3 = alloca [4096 x i8]
%4 = bitcast i8** %2 to i8*
%5 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %41, label %7
7: ; preds = %1
%8 = load i8*, i8** %2
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
%11 = icmp slt i32 %10, 0
%12 = load i8*, i8** %2
%13 = call i32 @free(i8* %12)
br i1 %11, label %41, label %14
14: ; preds = %7
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%16 = call i64 @read(i32 %10, i8* %15, i32 4095)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @close(i32 %10)
br label %39
20: ; preds = %14
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %16
store i8 0, i8* %21
%22 = call i32 @close(i32 %10)
br label %25
23: ; preds = %25
%24 = icmp eq i64 %32, %16
br i1 %24, label %33, label %25
25: ; preds = %20, %23
%26 = phi i64 [ 0, %20 ], [ %32, %23 ]
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
%30 = select i1 %29, i8 32, i8 %28
store i8 %30, i8* %27
%31 = icmp slt i8 %30, 0
%32 = add nuw i64 %26, 1
br i1 %31, label %39, label %23
33: ; preds = %23
%34 = call i8* @strdup(i8* %15)
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @free(i8* %34)
br label %39
39: ; preds = %25, %33, %37, %18
%40 = phi i8* [ null, %18 ], [ null, %37 ], [ %34, %33 ], [ null, %25 ]
br label %41
41: ; preds = %7, %1, %39
%42 = phi i8* [ %40, %39 ], [ null, %1 ], [ null, %7 ]
ret i8* %42
}