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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ basename(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int const);
char *strdup(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *get_pid_cmdline(const pid_t pid) {
  char buffer[(4096)];
  int fd;
  ssize_t ret = 0;
  *buffer = '\0';
  (void)snprintf(buffer, sizeof(buffer), "/proc/%d/cmdline", pid);
  if ((fd = open(buffer, O_RDONLY)) > -1) {
    ret = read(fd, buffer, sizeof(buffer) - 1);
    (void)close(fd);
    if (ret > -1)
      buffer[ret] = '\0';
  }
  if (!*buffer) {
    (void)snprintf(buffer, sizeof(buffer), "/proc/%d/comm", pid);
    if ((fd = open(buffer, O_RDONLY)) > -1) {
      ret = read(fd, buffer, sizeof(buffer) - 1);
      (void)close(fd);
      if (ret > 0)
        buffer[ret - 1] = '\0';
    }
  }
  if (ret < 1) {
    (void)strncpy(buffer, "<unknown>", sizeof(buffer));
  } else {
    char *ptr;
    for (ptr = buffer; *ptr && (ptr < buffer + ret); ptr++) {
      if (*ptr == ' ') {
        *ptr = '\0';
        break;
      }
    }
  }
  return strdup(basename(buffer));
}


x86 assembly (clang, -O0):
<get_pid_cmdline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x1018(%rbp)
movb   $0x0,-0x1010(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x1000,%esi
lea    0xe5b(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x1010(%rbp),%rdi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0xffffffff,%eax
jle    1217 <get_pid_cmdline+0xa7>
mov    -0x1014(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0xfff,%edx
call   1060 <read@plt>
mov    %eax,-0x1018(%rbp)
mov    -0x1014(%rbp),%edi
call   1050 <close@plt>
cmpl   $0xffffffff,-0x1018(%rbp)
jle    1212 <get_pid_cmdline+0xa2>
movslq -0x1018(%rbp),%rax
movb   $0x0,-0x1010(%rbp,%rax,1)
jmp    1217 <get_pid_cmdline+0xa7>
cmpb   $0x0,-0x1010(%rbp)
jne    12b5 <get_pid_cmdline+0x145>
lea    -0x1010(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x1000,%esi
lea    0xdd7(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x1010(%rbp),%rdi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0xffffffff,%eax
jle    12b0 <get_pid_cmdline+0x140>
mov    -0x1014(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0xfff,%edx
call   1060 <read@plt>
mov    %eax,-0x1018(%rbp)
mov    -0x1014(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x1018(%rbp)
jle    12ab <get_pid_cmdline+0x13b>
mov    -0x1018(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x1010(%rbp,%rax,1)
jmp    12b0 <get_pid_cmdline+0x140>
jmp    12b5 <get_pid_cmdline+0x145>
cmpl   $0x1,-0x1018(%rbp)
jge    12df <get_pid_cmdline+0x16f>
lea    -0x1010(%rbp),%rdi
lea    0xd4f(%rip),%rsi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
jmp    1382 <get_pid_cmdline+0x212>
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1020(%rbp)
mov    -0x1020(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1021(%rbp)
je     132c <get_pid_cmdline+0x1bc>
mov    -0x1020(%rbp),%rax
lea    -0x1010(%rbp),%rcx
movslq -0x1018(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
setb   %al
mov    %al,-0x1021(%rbp)
mov    -0x1021(%rbp),%al
test   $0x1,%al
jne    133f <get_pid_cmdline+0x1cf>
jmp    137d <get_pid_cmdline+0x20d>
mov    -0x1020(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    1361 <get_pid_cmdline+0x1f1>
mov    -0x1020(%rbp),%rax
movb   $0x0,(%rax)
jmp    137d <get_pid_cmdline+0x20d>
jmp    1366 <get_pid_cmdline+0x1f6>
mov    -0x1020(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1020(%rbp)
jmp    12ed <get_pid_cmdline+0x17d>
jmp    1382 <get_pid_cmdline+0x212>
lea    -0x1010(%rbp),%rdi
call   1070 <basename@plt>
mov    %eax,%edi
call   1090 <strdup@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pid_cmdline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %edi,%r14d
movb   $0x0,(%rsp)
lea    0xe75(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r14d,%ecx
call   1040 <snprintf@plt>
mov    0x2e43(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11e0 <get_pid_cmdline+0x70>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xfff,%edx
call   1060 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
test   %ebx,%ebx
js     11d8 <get_pid_cmdline+0x68>
mov    %ebx,%eax
movb   $0x0,(%rsp,%rax,1)
cmpb   $0x0,(%rsp)
jne    123a <get_pid_cmdline+0xca>
jmp    11e8 <get_pid_cmdline+0x78>
xor    %ebx,%ebx
cmpb   $0x0,(%rsp)
jne    123a <get_pid_cmdline+0xca>
lea    0xe22(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    %r14d,%ecx
call   1040 <snprintf@plt>
mov    (%r15),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     123a <get_pid_cmdline+0xca>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xfff,%edx
call   1060 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
test   %ebx,%ebx
jle    1269 <get_pid_cmdline+0xf9>
mov    %ebx,%eax
add    $0xffffffff,%eax
movb   $0x0,(%rsp,%rax,1)
jmp    123e <get_pid_cmdline+0xce>
test   %ebx,%ebx
jle    1269 <get_pid_cmdline+0xf9>
mov    (%rsp),%al
test   %al,%al
je     1283 <get_pid_cmdline+0x113>
mov    %ebx,%ecx
add    %rsp,%rcx
lea    0x1(%rsp),%rdx
nop
cmp    $0x20,%al
je     127f <get_pid_cmdline+0x10f>
movzbl (%rdx),%eax
test   %al,%al
je     1283 <get_pid_cmdline+0x113>
lea    0x1(%rdx),%rsi
cmp    %rcx,%rdx
mov    %rsi,%rdx
jb     1250 <get_pid_cmdline+0xe0>
jmp    1283 <get_pid_cmdline+0x113>
lea    0xdaf(%rip),%rsi
mov    %rsp,%rdi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
jmp    1283 <get_pid_cmdline+0x113>
movb   $0x0,-0x1(%rdx)
mov    %rsp,%rdi
call   1070 <basename@plt>
mov    %eax,%edi
call   1090 <strdup@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"/proc/%d/comm\00"
@.str.2 = [10 x i8] c"<unknown>\00"
define i8* @get_pid_cmdline(i32 %0) {
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 %0, i32* %2
store i32 0, i32* %5
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
store i8 0, i8* %7
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%9 = load i32, i32* %2
%10 = call i32 @snprintf(i8* %8, i32 4096, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %4
%14 = icmp sgt i32 %13, -1
br i1 %14, label %15, label %28
15: ; preds = %1
%16 = load i32, i32* %4
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 4095)
store i32 %18, i32* %5
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %5
%22 = icmp sgt i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %15
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %25
store i8 0, i8* %26
br label %27
27: ; preds = %23, %15
br label %28
28: ; preds = %27, %1
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%30 = load i8, i8* %29
%31 = icmp ne i8 %30, 0
br i1 %31, label %55, label %32
32: ; preds = %28
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%34 = load i32, i32* %2
%35 = call i32 @snprintf(i8* %33, i32 4096, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%37 = load i32, i32* @O_RDONLY
%38 = call i32 @open(i8* %36, i32 %37)
store i32 %38, i32* %4
%39 = icmp sgt i32 %38, -1
br i1 %39, label %40, label %54
40: ; preds = %32
%41 = load i32, i32* %4
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 4095)
store i32 %43, i32* %5
%44 = load i32, i32* %4
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %5
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %40
%49 = load i32, i32* %5
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %51
store i8 0, i8* %52
br label %53
53: ; preds = %48, %40
br label %54
54: ; preds = %53, %32
br label %55
55: ; preds = %54, %28
%56 = load i32, i32* %5
%57 = icmp slt i32 %56, 1
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%60 = call i32 @strncpy(i8* %59, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 4096)
br label %89
61: ; preds = %55
%62 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
store i8* %62, i8** %6
br label %63
63: ; preds = %85, %61
%64 = load i8*, i8** %6
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %75
68: ; preds = %63
%69 = load i8*, i8** %6
%70 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%71 = load i32, i32* %5
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %70, i64 %72
%74 = icmp ult i8* %69, %73
br label %75
75: ; preds = %68, %63
%76 = phi i1 [ false, %63 ], [ %74, %68 ]
br i1 %76, label %77, label %88
77: ; preds = %75
%78 = load i8*, i8** %6
%79 = load i8, i8* %78
%80 = sext i8 %79 to i32
%81 = icmp eq i32 %80, 32
br i1 %81, label %82, label %84
82: ; preds = %77
%83 = load i8*, i8** %6
store i8 0, i8* %83
br label %88
84: ; preds = %77
br label %85
85: ; preds = %84
%86 = load i8*, i8** %6
%87 = getelementptr inbounds i8, i8* %86, i32 1
store i8* %87, i8** %6
br label %63
88: ; preds = %82, %75
br label %89
89: ; preds = %88, %58
%90 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%91 = call i32 @basename(i8* %90)
%92 = call i8* @strdup(i32 %91)
ret i8* %92
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"/proc/%d/comm\00"
@.str.2 = [10 x i8] c"<unknown>\00"
define i8* @get_pid_cmdline(i32 %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
store i8 0, i8* %3
%4 = call i32 @snprintf(i8* %3, i32 4096, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp sgt i32 %6, -1
br i1 %7, label %8, label %15
8: ; preds = %1
%9 = call i32 @read(i32 %6, i8* %3, i32 4095)
%10 = call i32 @close(i32 %6)
%11 = icmp sgt i32 %9, -1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = zext i32 %9 to i64
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %8, %12, %1
%16 = phi i32 [ %9, %12 ], [ %9, %8 ], [ 0, %1 ]
%17 = load i8, i8* %3
%18 = icmp eq i8 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %15
%20 = call i32 @snprintf(i8* %3, i32 4096, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %3, i32 %21)
%23 = icmp sgt i32 %22, -1
br i1 %23, label %24, label %32
24: ; preds = %19
%25 = call i32 @read(i32 %22, i8* %3, i32 4095)
%26 = call i32 @close(i32 %22)
%27 = icmp sgt i32 %25, 0
br i1 %27, label %28, label %40
28: ; preds = %24
%29 = add nsw i32 %25, -1
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %30
store i8 0, i8* %31
br label %34
32: ; preds = %19, %15
%33 = icmp slt i32 %16, 1
br i1 %33, label %40, label %34
34: ; preds = %28, %32
%35 = phi i32 [ %25, %28 ], [ %16, %32 ]
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %36
%38 = load i8, i8* %3
%39 = icmp eq i8 %38, 0
br i1 %39, label %53, label %42
40: ; preds = %24, %32
%41 = call i32 @strncpy(i8* %3, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 4096)
br label %53
42: ; preds = %34, %47
%43 = phi i8 [ %49, %47 ], [ %38, %34 ]
%44 = phi i8* [ %48, %47 ], [ %3, %34 ]
%45 = icmp eq i8 %43, 32
br i1 %45, label %46, label %47
46: ; preds = %42
store i8 0, i8* %44
br label %53
47: ; preds = %42
%48 = getelementptr inbounds i8, i8* %44, i64 1
%49 = load i8, i8* %48
%50 = icmp ne i8 %49, 0
%51 = icmp ult i8* %48, %37
%52 = select i1 %50, i1 %51, i1 false
br i1 %52, label %42, label %53
53: ; preds = %47, %34, %46, %40
%54 = call i32 @basename(i8* %3)
%55 = call i8* @strdup(i32 %54)
ret i8* %55
}