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

/* Variables and functions */
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
void commandFromPid(int pid, char *name) {
  char buf[256], c;
  FILE *f;
  int i;
  name[0] = 0;
  snprintf(buf, 256, "/proc/%d/stat", pid);
  f = fopen(buf, "r");
  if (f == NULL)
    return;
  while ((c = fgetc(f)) != '(')
    ;
  i = 0;
  while ((c = fgetc(f)) != ')')
    name[i++] = c;
  name[i] = 0;
}


x86 assembly (clang, -O0):
<commandFromPid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x110(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x100,%esi
lea    0xea1(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe9c(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x120(%rbp)
cmpq   $0x0,-0x120(%rbp)
jne    1191 <commandFromPid+0x61>
jmp    1216 <commandFromPid+0xe6>
jmp    1196 <commandFromPid+0x66>
mov    -0x120(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x111(%rbp)
movsbl %al,%eax
cmp    $0x28,%eax
je     11b9 <commandFromPid+0x89>
jmp    1196 <commandFromPid+0x66>
movl   $0x0,-0x124(%rbp)
mov    -0x120(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x111(%rbp)
movsbl %al,%eax
cmp    $0x29,%eax
je     1207 <commandFromPid+0xd7>
mov    -0x111(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x124(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x124(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11c3 <commandFromPid+0x93>
mov    -0x10(%rbp),%rax
movslq -0x124(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<commandFromPid>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rsi,%r14
mov    %edi,%ecx
movb   $0x0,(%rsi)
lea    0xeb5(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1030 <snprintf@plt>
lea    0xeac(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11bb <commandFromPid+0x8b>
mov    %rax,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
call   1040 <fgetc@plt>
cmp    $0x28,%al
jne    1180 <commandFromPid+0x50>
mov    %r15,%rdi
call   1040 <fgetc@plt>
cmp    $0x29,%al
jne    119c <commandFromPid+0x6c>
xor    %eax,%eax
jmp    11b6 <commandFromPid+0x86>
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1040 <fgetc@plt>
cmp    $0x29,%al
jne    11a0 <commandFromPid+0x70>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
define void @commandFromPid(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [256 x i8]
%6 = alloca i8
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = load i8*, i8** %4
%10 = getelementptr inbounds i8, i8* %9, i64 0
store i8 0, i8* %10
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%12 = load i32, i32* %3
%13 = call i32 @snprintf(i8* %11, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %7
%16 = load i32*, i32** %7
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %19
18: ; preds = %2
br label %44
19: ; preds = %2
br label %20
20: ; preds = %25, %19
%21 = load i32*, i32** %7
%22 = call signext i8 @fgetc(i32* %21)
store i8 %22, i8* %6
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 40
br i1 %24, label %25, label %26
25: ; preds = %20
br label %20
26: ; preds = %20
store i32 0, i32* %8
br label %27
27: ; preds = %32, %26
%28 = load i32*, i32** %7
%29 = call signext i8 @fgetc(i32* %28)
store i8 %29, i8* %6
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 41
br i1 %31, label %32, label %39
32: ; preds = %27
%33 = load i8, i8* %6
%34 = load i8*, i8** %4
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 %33, i8* %38
br label %27
39: ; preds = %27
%40 = load i8*, i8** %4
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %39, %18
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
define void @commandFromPid(i32 %0, i8* nocapture writeonly %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
store i8 0, i8* %1
%5 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %26, label %8
8: ; preds = %2, %8
%9 = call signext i8 @fgetc(i32* %6)
%10 = icmp eq i8 %9, 40
br i1 %10, label %11, label %8
11: ; preds = %8
%12 = call signext i8 @fgetc(i32* %6)
%13 = icmp eq i8 %12, 41
br i1 %13, label %23, label %14
14: ; preds = %11, %14
%15 = phi i64 [ %17, %14 ], [ 0, %11 ]
%16 = phi i8 [ %19, %14 ], [ %12, %11 ]
%17 = add nuw i64 %15, 1
%18 = getelementptr inbounds i8, i8* %1, i64 %15
store i8 %16, i8* %18
%19 = call signext i8 @fgetc(i32* %6)
%20 = icmp eq i8 %19, 41
br i1 %20, label %21, label %14
21: ; preds = %14
%22 = and i64 %17, 4294967295
br label %23
23: ; preds = %21, %11
%24 = phi i64 [ 0, %11 ], [ %22, %21 ]
%25 = getelementptr inbounds i8, i8* %1, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %2, %23
ret void
}