#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*/ path;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *const, size_t const);
int /*<<< orphan*/ snprintf(char *, int, char *, int const);
int /*<<< orphan*/ strlcpy(char *const, char *, size_t const);
int proc_cmdline(const pid_t pid, char *const cmdline, const size_t size) {
  int fd;
  char path[PATH_MAX];
  int n = 0;
  *cmdline = '\0';
  (void)snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
  if ((fd = open(path, O_RDONLY)) > -1) {
    n = read(fd, cmdline, size - 1);
    (void)close(fd);
    if (n > -1)
      cmdline[n] = '\0';
  }
  if (!*cmdline) {
    (void)snprintf(path, sizeof(path), "/proc/%d/comm", pid);
    if ((fd = open(path, O_RDONLY)) > -1) {
      n = read(fd, cmdline, size - 1);
      (void)close(fd);
      if (n > 0)
        cmdline[n - 1] = '\0';
    }
  }
  if (n < 1) {
    (void)strlcpy(cmdline, "<unknown>", size);
    n = 9;
  }
  return n;
}


x86 assembly (clang, -O0):
<proc_cmdline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rcx)
mov    %eax,%esi
mov    -0x4(%rbp),%ecx
lea    0xe53(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x40(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jle    120a <proc_cmdline+0xba>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1050 <read@plt>
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xffffffff,-0x34(%rbp)
jle    1205 <proc_cmdline+0xb5>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    120a <proc_cmdline+0xba>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1292 <proc_cmdline+0x142>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%esi
mov    -0x4(%rbp),%ecx
lea    0xde6(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x40(%rbp),%rdi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jle    128d <proc_cmdline+0x13d>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1050 <read@plt>
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x34(%rbp)
jle    1288 <proc_cmdline+0x138>
mov    -0x10(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    128d <proc_cmdline+0x13d>
jmp    1292 <proc_cmdline+0x142>
cmpl   $0x1,-0x34(%rbp)
jge    12b7 <proc_cmdline+0x167>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd74(%rip),%rsi
call   1060 <strlcpy@plt>
movl   $0x9,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_cmdline>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r12
mov    %edi,%ecx
mov    0x2e80(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
mov    %r13,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movb   $0x0,(%rsi)
lea    0xe6f(%rip),%rdx
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %ecx,-0x2c(%rbp)
call   1030 <snprintf@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
mov    %r14,-0x38(%rbp)
js     11ea <proc_cmdline+0x9a>
mov    %eax,%r15d
lea    -0x1(%r14),%rdx
mov    %eax,%edi
mov    %r12,%rsi
call   1050 <read@plt>
mov    %eax,%r14d
mov    %r15d,%edi
call   1040 <close@plt>
test   %r14d,%r14d
js     11e1 <proc_cmdline+0x91>
mov    %r14d,%eax
movb   $0x0,(%r12,%rax,1)
cmpb   $0x0,(%r12)
jne    124e <proc_cmdline+0xfe>
jmp    11f4 <proc_cmdline+0xa4>
xor    %r14d,%r14d
cmpb   $0x0,(%r12)
jne    124e <proc_cmdline+0xfe>
lea    0xe16(%rip),%rdx
mov    %rbx,%rdi
mov    %r13d,%esi
mov    -0x2c(%rbp),%ecx
call   1030 <snprintf@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     124e <proc_cmdline+0xfe>
mov    %eax,%ebx
mov    -0x38(%rbp),%rax
lea    -0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r12,%rsi
call   1050 <read@plt>
mov    %eax,%r14d
mov    %ebx,%edi
call   1040 <close@plt>
test   %r14d,%r14d
jle    1253 <proc_cmdline+0x103>
mov    %r14d,%eax
add    $0xffffffff,%eax
movb   $0x0,(%r12,%rax,1)
jmp    126c <proc_cmdline+0x11c>
test   %r14d,%r14d
jg     126c <proc_cmdline+0x11c>
lea    0xdc5(%rip),%rsi
mov    %r12,%rdi
mov    -0x38(%rbp),%rdx
call   1060 <strlcpy@plt>
mov    $0x9,%r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.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 i32 @proc_cmdline(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %8
%14 = alloca i8, i64 %12
store i64 %12, i64* %9
store i32 0, i32* %10
%15 = load i8*, i8** %5
store i8 0, i8* %15
%16 = trunc i64 %12 to i32
%17 = load i32, i32* %4
%18 = call i32 @snprintf(i8* %14, i32 %16, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %14, i32 %19)
store i32 %20, i32* %7
%21 = icmp sgt i32 %20, -1
br i1 %21, label %22, label %38
22: ; preds = %3
%23 = load i32, i32* %7
%24 = load i8*, i8** %5
%25 = load i64, i64* %6
%26 = sub i64 %25, 1
%27 = call i32 @read(i32 %23, i8* %24, i64 %26)
store i32 %27, i32* %10
%28 = load i32, i32* %7
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %10
%31 = icmp sgt i32 %30, -1
br i1 %31, label %32, label %37
32: ; preds = %22
%33 = load i8*, i8** %5
%34 = load i32, i32* %10
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %32, %22
br label %38
38: ; preds = %37, %3
%39 = load i8*, i8** %5
%40 = load i8, i8* %39
%41 = icmp ne i8 %40, 0
br i1 %41, label %67, label %42
42: ; preds = %38
%43 = trunc i64 %12 to i32
%44 = load i32, i32* %4
%45 = call i32 @snprintf(i8* %14, i32 %43, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %44)
%46 = load i32, i32* @O_RDONLY
%47 = call i32 @open(i8* %14, i32 %46)
store i32 %47, i32* %7
%48 = icmp sgt i32 %47, -1
br i1 %48, label %49, label %66
49: ; preds = %42
%50 = load i32, i32* %7
%51 = load i8*, i8** %5
%52 = load i64, i64* %6
%53 = sub i64 %52, 1
%54 = call i32 @read(i32 %50, i8* %51, i64 %53)
store i32 %54, i32* %10
%55 = load i32, i32* %7
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* %10
%58 = icmp sgt i32 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %49
%60 = load i8*, i8** %5
%61 = load i32, i32* %10
%62 = sub nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %60, i64 %63
store i8 0, i8* %64
br label %65
65: ; preds = %59, %49
br label %66
66: ; preds = %65, %42
br label %67
67: ; preds = %66, %38
%68 = load i32, i32* %10
%69 = icmp slt i32 %68, 1
br i1 %69, label %70, label %74
70: ; preds = %67
%71 = load i8*, i8** %5
%72 = load i64, i64* %6
%73 = call i32 @strlcpy(i8* %71, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %72)
store i32 9, i32* %10
br label %74
74: ; preds = %70, %67
%75 = load i32, i32* %10
%76 = load i8*, i8** %8
ret i32 %75
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.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 i32 @proc_cmdline(i32 %0, i8* %1, i64 %2) {
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
store i8 0, i8* %1
%7 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %6, i32 %8)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %19
11: ; preds = %3
%12 = add i64 %2, -1
%13 = call i32 @read(i32 %9, i8* %1, i64 %12)
%14 = call i32 @close(i32 %9)
%15 = icmp sgt i32 %13, -1
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = zext i32 %13 to i64
%18 = getelementptr inbounds i8, i8* %1, i64 %17
store i8 0, i8* %18
br label %19
19: ; preds = %11, %16, %3
%20 = phi i32 [ %13, %16 ], [ %13, %11 ], [ 0, %3 ]
%21 = load i8, i8* %1
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %19
%24 = call i32 @snprintf(i8* %6, i32 %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %6, i32 %25)
%27 = icmp sgt i32 %26, -1
br i1 %27, label %28, label %37
28: ; preds = %23
%29 = add i64 %2, -1
%30 = call i32 @read(i32 %26, i8* %1, i64 %29)
%31 = call i32 @close(i32 %26)
%32 = icmp sgt i32 %30, 0
br i1 %32, label %33, label %39
33: ; preds = %28
%34 = add nsw i32 %30, -1
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %1, i64 %35
store i8 0, i8* %36
br label %41
37: ; preds = %23, %19
%38 = icmp slt i32 %20, 1
br i1 %38, label %39, label %41
39: ; preds = %28, %37
%40 = call i32 @strlcpy(i8* %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 %2)
br label %41
41: ; preds = %33, %39, %37
%42 = phi i32 [ 9, %39 ], [ %20, %37 ], [ %30, %33 ]
ret i32 %42
}