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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
void print_process_command_line(int pid) {
  int fd, i;
  char filename[64];
  char read_buf[512];
  size_t num_bytes_read;
  sprintf(filename, "/proc/%d/cmdline", pid);
  fd = open(filename, O_RDONLY);
  if (fd != -1) {
    while (((num_bytes_read = read(fd, read_buf, 512)) != 0) &&
           (num_bytes_read != -1)) {
      for (i = 0; i < num_bytes_read; i++) {
        if (read_buf[i] == 0) {
          printf(" ");
        } else {
          printf("%c", read_buf[i]);
        }
      }
    }
    printf("\n");
  }
}


x86 assembly (clang, -O0):
<print_process_command_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xea4(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     1259 <print_process_command_line+0x119>
jmp    1185 <print_process_command_line+0x45>
mov    -0x8(%rbp),%edi
lea    -0x250(%rbp),%rsi
mov    $0x200,%edx
call   1040 <read@plt>
mov    %rax,%rcx
mov    %rcx,-0x258(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x259(%rbp)
je     11c6 <print_process_command_line+0x86>
cmpq   $0xffffffffffffffff,-0x258(%rbp)
ff
setne  %al
mov    %al,-0x259(%rbp)
mov    -0x259(%rbp),%al
test   $0x1,%al
jne    11d9 <print_process_command_line+0x99>
jmp    124b <print_process_command_line+0x10b>
movl   $0x0,-0xc(%rbp)
movslq -0xc(%rbp),%rax
cmp    -0x258(%rbp),%rax
jae    1246 <print_process_command_line+0x106>
movslq -0xc(%rbp),%rax
movsbl -0x250(%rbp,%rax,1),%eax
ff
cmp    $0x0,%eax
jne    1219 <print_process_command_line+0xd9>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1233 <print_process_command_line+0xf3>
movslq -0xc(%rbp),%rax
movsbl -0x250(%rbp,%rax,1),%esi
ff
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1238 <print_process_command_line+0xf8>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11e0 <print_process_command_line+0xa0>
jmp    1185 <print_process_command_line+0x45>
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_process_command_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x240,%rsp
mov    %edi,%edx
lea    0xea8(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1212 <print_process_command_line+0xd2>
mov    %eax,%r15d
lea    0x40(%rsp),%rsi
mov    %eax,%edi
mov    $0x200,%edx
call   1040 <read@plt>
mov    %rax,%rbx
add    $0xffffffffffffffff,%rax
cmp    $0xfffffffffffffffd,%rax
ja     11f9 <print_process_command_line+0xb9>
mov    $0x20,%r12d
lea    0x40(%rsp),%r14
nopl   0x0(%rax)
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x40(%rsp,%rbp,1),%eax
test   %al,%al
cmove  %r12d,%eax
movsbl %al,%edi
call   1030 <putchar@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11c0 <print_process_command_line+0x80>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x200,%edx
call   1040 <read@plt>
mov    %rax,%rbx
add    $0xffffffffffffffff,%rax
cmp    $0xfffffffffffffffd,%rax
jbe    11b0 <print_process_command_line+0x70>
mov    $0xa,%edi
add    $0x240,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>
add    $0x240,%rsp
pop    %rbx
pop    %r12
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 = [2 x i8] c" \00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [2 x i8] c"\0A\00"
define void @print_process_command_line(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [64 x i8]
%6 = alloca [512 x i8]
%7 = alloca i64
store i32 %0, i32* %2
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* %2
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %56
16: ; preds = %1
br label %17
17: ; preds = %53, %16
%18 = load i32, i32* %3
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%20 = call i64 @read(i32 %18, i8* %19, i32 512)
store i64 %20, i64* %7
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i64, i64* %7
%24 = icmp ne i64 %23, -1
br label %25
25: ; preds = %22, %17
%26 = phi i1 [ false, %17 ], [ %24, %22 ]
br i1 %26, label %27, label %54
27: ; preds = %25
store i32 0, i32* %4
br label %28
28: ; preds = %50, %27
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = load i64, i64* %7
%32 = icmp ult i64 %30, %31
br i1 %32, label %33, label %53
33: ; preds = %28
%34 = load i32, i32* %4
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %33
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %49
42: ; preds = %33
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %42, %40
br label %50
50: ; preds = %49
%51 = load i32, i32* %4
%52 = add nsw i32 %51, 1
store i32 %52, i32* %4
br label %28
53: ; preds = %28
br label %17
54: ; preds = %25
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %54, %1
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/%d/cmdline\00"
@O_RDONLY = global i32 0
define void @print_process_command_line(i32 %0) {
%2 = alloca [64 x i8]
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %4, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %32, label %10
10: ; preds = %1
%11 = call i64 @read(i32 %8, i8* %5, i32 512)
%12 = add i64 %11, -1
%13 = icmp ult i64 %12, -2
br i1 %13, label %18, label %30
14: ; preds = %20
%15 = call i64 @read(i32 %8, i8* %5, i32 512)
%16 = add i64 %15, -1
%17 = icmp ult i64 %16, -2
br i1 %17, label %18, label %30
18: ; preds = %10, %14
%19 = phi i64 [ %15, %14 ], [ %11, %10 ]
br label %20
20: ; preds = %18, %20
%21 = phi i64 [ 0, %18 ], [ %28, %20 ]
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
%25 = select i1 %24, i8 32, i8 %23
%26 = sext i8 %25 to i32
%27 = tail call i32 @putchar(i32 %26)
%28 = add nuw i64 %21, 1
%29 = icmp eq i64 %28, %19
br i1 %29, label %14, label %20
30: ; preds = %14, %10
%31 = tail call i32 @putchar(i32 10)
br label %32
32: ; preds = %30, %1
ret void
}