#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] = ' ';
  }
  char *rv = strdup((char *)buffer);
  if (!rv)
    return NULL;
  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    12fb <pid_proc_cmdline+0x18b>
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    12fb <pid_proc_cmdline+0x18b>
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    12fb <pid_proc_cmdline+0x18b>
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    1293 <pid_proc_cmdline+0x123>
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
jmp    127f <pid_proc_cmdline+0x10f>
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)
cmpq   $0x0,-0x1038(%rbp)
jne    12c1 <pid_proc_cmdline+0x151>
movq   $0x0,-0x8(%rbp)
jmp    12fb <pid_proc_cmdline+0x18b>
mov    -0x1038(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    12f0 <pid_proc_cmdline+0x180>
mov    -0x1038(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12fb <pid_proc_cmdline+0x18b>
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    0xeae(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
je     13c3 <pid_proc_cmdline+0x263>
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     13c3 <pid_proc_cmdline+0x263>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xfff,%edx
call   1050 <read@plt>
test   %rax,%rax
je     11de <pid_proc_cmdline+0x7e>
mov    %rax,%rbx
movb   $0x0,0x10(%rsp,%rax,1)
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0x8,%rbx
jae    11ea <pid_proc_cmdline+0x8a>
xor    %eax,%eax
jmp    1382 <pid_proc_cmdline+0x222>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    13c3 <pid_proc_cmdline+0x263>
cmp    $0x20,%rbx
jae    11f7 <pid_proc_cmdline+0x97>
xor    %eax,%eax
jmp    1331 <pid_proc_cmdline+0x1d1>
mov    %rbx,%rax
and    $0xffffffffffffffe0,%rax
lea    -0x20(%rax),%rcx
mov    %rcx,%rdx
shr    $0x5,%rdx
add    $0x1,%rdx
test   %rcx,%rcx
je     13cf <pid_proc_cmdline+0x26f>
mov    %rdx,%rsi
and    $0xfffffffffffffffe,%rsi
xor    %ecx,%ecx
pxor   %xmm8,%xmm8
movdqa 0xdd4(%rip),%xmm1
nopl   0x0(%rax)
movdqa 0x10(%rsp,%rcx,1),%xmm2
movdqa 0x20(%rsp,%rcx,1),%xmm3
movdqa 0x30(%rsp,%rcx,1),%xmm4
movdqa 0x40(%rsp,%rcx,1),%xmm5
movdqa %xmm2,%xmm6
pcmpeqb %xmm8,%xmm6
movdqa %xmm3,%xmm7
pcmpeqb %xmm8,%xmm7
movdqa %xmm6,%xmm0
pandn  %xmm2,%xmm0
pand   %xmm1,%xmm6
por    %xmm0,%xmm6
movdqa %xmm7,%xmm0
pandn  %xmm3,%xmm0
pand   %xmm1,%xmm7
por    %xmm0,%xmm7
movdqa %xmm6,0x10(%rsp,%rcx,1)
movdqa %xmm7,0x20(%rsp,%rcx,1)
movdqa %xmm4,%xmm0
pcmpeqb %xmm8,%xmm0
movdqa %xmm5,%xmm2
pcmpeqb %xmm8,%xmm2
movdqa %xmm0,%xmm3
pandn  %xmm4,%xmm3
pand   %xmm1,%xmm0
por    %xmm3,%xmm0
movdqa %xmm2,%xmm3
pandn  %xmm5,%xmm3
pand   %xmm1,%xmm2
por    %xmm3,%xmm2
movdqa %xmm0,0x30(%rsp,%rcx,1)
movdqa %xmm2,0x40(%rsp,%rcx,1)
add    $0x40,%rcx
add    $0xfffffffffffffffe,%rsi
jne    1230 <pid_proc_cmdline+0xd0>
test   $0x1,%dl
je     1327 <pid_proc_cmdline+0x1c7>
movdqa 0x10(%rsp,%rcx,1),%xmm0
movdqa 0x20(%rsp,%rcx,1),%xmm1
pxor   %xmm2,%xmm2
movdqa %xmm0,%xmm3
pcmpeqb %xmm2,%xmm3
pcmpeqb %xmm1,%xmm2
movdqa %xmm3,%xmm4
pandn  %xmm0,%xmm4
movdqa 0xcfd(%rip),%xmm0
pand   %xmm0,%xmm3
por    %xmm4,%xmm3
movdqa %xmm2,%xmm4
pandn  %xmm1,%xmm4
pand   %xmm0,%xmm2
por    %xmm4,%xmm2
movdqa %xmm3,0x10(%rsp,%rcx,1)
movdqa %xmm2,0x20(%rsp,%rcx,1)
cmp    %rax,%rbx
je     13a7 <pid_proc_cmdline+0x247>
test   $0x18,%bl
je     1382 <pid_proc_cmdline+0x222>
mov    %rax,%rcx
mov    %rbx,%rax
and    $0xfffffffffffffff8,%rax
pxor   %xmm0,%xmm0
movdqa 0xcc9(%rip),%xmm1
nopw   0x0(%rax,%rax,1)
movq   0x10(%rsp,%rcx,1),%xmm2
movdqa %xmm2,%xmm3
pcmpeqb %xmm0,%xmm3
movdqa %xmm3,%xmm4
pandn  %xmm2,%xmm4
pand   %xmm1,%xmm3
por    %xmm4,%xmm3
movq   %xmm3,0x10(%rsp,%rcx,1)
add    $0x8,%rcx
cmp    %rcx,%rax
jne    1350 <pid_proc_cmdline+0x1f0>
cmp    %rax,%rbx
je     13a7 <pid_proc_cmdline+0x247>
mov    $0x20,%ecx
nopw   0x0(%rax,%rax,1)
movzbl 0x10(%rsp,%rax,1),%edx
test   %dl,%dl
cmove  %ecx,%edx
mov    %dl,0x10(%rsp,%rax,1)
add    $0x1,%rax
cmp    %rax,%rbx
jne    1390 <pid_proc_cmdline+0x230>
lea    0x10(%rsp),%rdi
call   1080 <strdup@plt>
test   %rax,%rax
je     13c3 <pid_proc_cmdline+0x263>
cmpb   $0x0,(%rax)
jne    13c5 <pid_proc_cmdline+0x265>
mov    %rax,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %rbp
ret
xor    %ecx,%ecx
test   $0x1,%dl
jne    12d7 <pid_proc_cmdline+0x177>
jmp    1327 <pid_proc_cmdline+0x1c7>

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 %72
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 %72
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 %72
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 = %54, %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 %57
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
br label %54
54: ; preds = %53
%55 = load i32, i32* %8
%56 = add nsw i32 %55, 1
store i32 %56, i32* %8
br label %37
57: ; preds = %37
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%59 = call i8* @strdup(i8* %58)
store i8* %59, i8** %9
%60 = load i8*, i8** %9
%61 = icmp ne i8* %60, null
br i1 %61, label %63, label %62
62: ; preds = %57
store i8* null, i8** %2
br label %72
63: ; preds = %57
%64 = load i8*, i8** %9
%65 = call i64 @strlen(i8* %64)
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %63
%68 = load i8*, i8** %9
%69 = call i32 @free(i8* %68)
store i8* null, i8** %2
br label %72
70: ; preds = %63
%71 = load i8*, i8** %9
store i8* %71, i8** %2
br label %72
72: ; preds = %70, %67, %62, %29, %19, %13
%73 = load i8*, i8** %2
ret i8* %73
}

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 %122, 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 %122, 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 %120
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)
%23 = icmp ult i64 %16, 8
br i1 %23, label %102, label %24
24: ; preds = %20
%25 = icmp ult i64 %16, 32
br i1 %25, label %87, label %26
26: ; preds = %24
%27 = and i64 %16, -32
%28 = add i64 %27, -32
%29 = lshr exact i64 %28, 5
%30 = add nuw nsw i64 %29, 1
%31 = and i64 %30, 1
%32 = icmp eq i64 %28, 0
br i1 %32, label %66, label %33
33: ; preds = %26
%34 = and i64 %30, 1152921504606846974
br label %35
35: ; preds = %35, %33
%36 = phi i64 [ 0, %33 ], [ %63, %35 ]
%37 = phi i64 [ 0, %33 ], [ %64, %35 ]
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %36
%39 = bitcast i8* %38 to <16 x i8>*
%40 = load <16 x i8>, <16 x i8>* %39
%41 = getelementptr inbounds i8, i8* %38, i64 16
%42 = bitcast i8* %41 to <16 x i8>*
%43 = load <16 x i8>, <16 x i8>* %42
%44 = icmp eq <16 x i8> %40, zeroinitializer
%45 = icmp eq <16 x i8> %43, zeroinitializer
%46 = select <16 x i1> %44, <16 x i8> <i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32>, <16 x i8> %40
%47 = select <16 x i1> %45, <16 x i8> <i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32>, <16 x i8> %43
%48 = bitcast i8* %38 to <16 x i8>*
store <16 x i8> %46, <16 x i8>* %48
%49 = bitcast i8* %41 to <16 x i8>*
store <16 x i8> %47, <16 x i8>* %49
%50 = or i64 %36, 32
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %50
%52 = bitcast i8* %51 to <16 x i8>*
%53 = load <16 x i8>, <16 x i8>* %52
%54 = getelementptr inbounds i8, i8* %51, i64 16
%55 = bitcast i8* %54 to <16 x i8>*
%56 = load <16 x i8>, <16 x i8>* %55
%57 = icmp eq <16 x i8> %53, zeroinitializer
%58 = icmp eq <16 x i8> %56, zeroinitializer
%59 = select <16 x i1> %57, <16 x i8> <i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32>, <16 x i8> %53
%60 = select <16 x i1> %58, <16 x i8> <i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32>, <16 x i8> %56
%61 = bitcast i8* %51 to <16 x i8>*
store <16 x i8> %59, <16 x i8>* %61
%62 = bitcast i8* %54 to <16 x i8>*
store <16 x i8> %60, <16 x i8>* %62
%63 = add nuw i64 %36, 64
%64 = add i64 %37, 2
%65 = icmp eq i64 %64, %34
br i1 %65, label %66, label %35
66: ; preds = %35, %26
%67 = phi i64 [ 0, %26 ], [ %63, %35 ]
%68 = icmp eq i64 %31, 0
br i1 %68, label %82, label %69
69: ; preds = %66
%70 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %67
%71 = bitcast i8* %70 to <16 x i8>*
%72 = load <16 x i8>, <16 x i8>* %71
%73 = getelementptr inbounds i8, i8* %70, i64 16
%74 = bitcast i8* %73 to <16 x i8>*
%75 = load <16 x i8>, <16 x i8>* %74
%76 = icmp eq <16 x i8> %72, zeroinitializer
%77 = icmp eq <16 x i8> %75, zeroinitializer
%78 = select <16 x i1> %76, <16 x i8> <i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32>, <16 x i8> %72
%79 = select <16 x i1> %77, <16 x i8> <i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32>, <16 x i8> %75
%80 = bitcast i8* %70 to <16 x i8>*
store <16 x i8> %78, <16 x i8>* %80
%81 = bitcast i8* %73 to <16 x i8>*
store <16 x i8> %79, <16 x i8>* %81
br label %82
82: ; preds = %66, %69
%83 = icmp eq i64 %16, %27
br i1 %83, label %112, label %84
84: ; preds = %82
%85 = and i64 %16, 24
%86 = icmp eq i64 %85, 0
br i1 %86, label %102, label %87
87: ; preds = %24, %84
%88 = phi i64 [ %27, %84 ], [ 0, %24 ]
%89 = and i64 %16, -8
br label %90
90: ; preds = %90, %87
%91 = phi i64 [ %88, %87 ], [ %98, %90 ]
%92 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %91
%93 = bitcast i8* %92 to <8 x i8>*
%94 = load <8 x i8>, <8 x i8>* %93
%95 = icmp eq <8 x i8> %94, zeroinitializer
%96 = select <8 x i1> %95, <8 x i8> <i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32>, <8 x i8> %94
%97 = bitcast i8* %92 to <8 x i8>*
store <8 x i8> %96, <8 x i8>* %97
%98 = add nuw i64 %91, 8
%99 = icmp eq i64 %98, %89
br i1 %99, label %100, label %90
100: ; preds = %90
%101 = icmp eq i64 %16, %89
br i1 %101, label %112, label %102
102: ; preds = %20, %84, %100
%103 = phi i64 [ 0, %20 ], [ %27, %84 ], [ %89, %100 ]
br label %104
104: ; preds = %102, %104
%105 = phi i64 [ %110, %104 ], [ %103, %102 ]
%106 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %105
%107 = load i8, i8* %106
%108 = icmp eq i8 %107, 0
%109 = select i1 %108, i8 32, i8 %107
store i8 %109, i8* %106
%110 = add nuw nsw i64 %105, 1
%111 = icmp eq i64 %110, %16
br i1 %111, label %112, label %104
112: ; preds = %104, %100, %82
%113 = call i8* @strdup(i8* %15)
%114 = icmp eq i8* %113, null
br i1 %114, label %120, label %115
115: ; preds = %112
%116 = load i8, i8* %113
%117 = icmp eq i8 %116, 0
br i1 %117, label %118, label %120
118: ; preds = %115
%119 = call i32 @free(i8* %113)
br label %120
120: ; preds = %118, %112, %115, %18
%121 = phi i8* [ null, %18 ], [ null, %118 ], [ null, %112 ], [ %113, %115 ]
br label %122
122: ; preds = %7, %1, %120
%123 = phi i8* [ %121, %120 ], [ null, %1 ], [ null, %7 ]
ret i8* %123
}