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

/* Variables and functions */
size_t readlink(char *, char *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
ssize_t proc_get_cmdline(unsigned int pid, char *cmdline, size_t len) {
  ssize_t ret;
  char path[1024];
  snprintf(path, sizeof(path), "/proc/%u/exe", pid);
  ret = readlink(path, cmdline, len - 1);
  if (ret < 0)
    cmdline[0] = '\0';
  else
    cmdline[ret] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<proc_get_cmdline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x400,%esi
lea    0xeb4(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
call   1030 <readlink@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    1184 <proc_get_cmdline+0x64>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    1190 <proc_get_cmdline+0x70>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_get_cmdline>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ecx
lea    0xec5(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x400,%esi
call   1040 <snprintf@plt>
add    $0xffffffffffffffff,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <readlink@plt>
movb   $0x0,(%r14,%rax,1)
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/proc/%u/exe\00"
define i64 @proc_get_cmdline(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca [1024 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @snprintf(i8* %9, i32 1024, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%13 = load i8*, i8** %5
%14 = load i64, i64* %6
%15 = sub i64 %14, 1
%16 = call i64 @readlink(i8* %12, i8* %13, i64 %15)
store i64 %16, i64* %7
%17 = load i64, i64* %7
%18 = icmp ult i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = load i8*, i8** %5
%21 = getelementptr inbounds i8, i8* %20, i64 0
store i8 0, i8* %21
br label %26
22: ; preds = %3
%23 = load i8*, i8** %5
%24 = load i64, i64* %7
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %22, %19
%27 = load i64, i64* %7
ret i64 %27
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/%u/exe\00"
define i64 @proc_get_cmdline(i32 %0, i8* nocapture %1, i64 %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 1024, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = add i64 %2, -1
%8 = call i64 @readlink(i8* %5, i8* %1, i64 %7)
%9 = getelementptr inbounds i8, i8* %1, i64 %8
store i8 0, i8* %9
ret i64 %8
}