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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
bool pid_is_cmd(pid_t pid, const char *name) {
  char buf[32];
  FILE *f;
  int c;
  sprintf(buf, "/proc/%d/stat", pid);
  f = fopen(buf, "r");
  if (!f)
    return false;
  while ((c = getc(f)) != EOF && c != '(')
    ;
  if (c != '(') {
    fclose(f);
    return false;
  }
  while ((c = getc(f)) != EOF && c == *name)
    name++;
  fclose(f);
  return (c == ')' && *name == '\0');
}


x86 assembly (clang, -O0):
<pid_is_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xea3(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x30(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    118d <pid_is_cmd+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    127f <pid_is_cmd+0x13f>
jmp    1192 <pid_is_cmd+0x52>
mov    -0x38(%rbp),%rdi
call   1060 <getc@plt>
mov    %eax,%ecx
mov    %ecx,-0x3c(%rbp)
xor    %eax,%eax
mov    0x2e3f(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x3d(%rbp)
je     11be <pid_is_cmd+0x7e>
cmpl   $0x28,-0x3c(%rbp)
setne  %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    11ce <pid_is_cmd+0x8e>
jmp    11d3 <pid_is_cmd+0x93>
jmp    1192 <pid_is_cmd+0x52>
cmpl   $0x28,-0x3c(%rbp)
je     11f2 <pid_is_cmd+0xb2>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    127f <pid_is_cmd+0x13f>
jmp    11f7 <pid_is_cmd+0xb7>
mov    -0x38(%rbp),%rdi
call   1060 <getc@plt>
mov    %eax,%ecx
mov    %ecx,-0x3c(%rbp)
xor    %eax,%eax
mov    0x2dda(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x3e(%rbp)
je     122b <pid_is_cmd+0xeb>
mov    -0x3c(%rbp),%eax
mov    -0x10(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
sete   %al
mov    %al,-0x3e(%rbp)
mov    -0x3e(%rbp),%al
test   $0x1,%al
jne    123b <pid_is_cmd+0xfb>
jmp    124c <pid_is_cmd+0x10c>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11f7 <pid_is_cmd+0xb7>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
cmpl   $0x29,-0x3c(%rbp)
mov    %al,-0x3f(%rbp)
jne    1274 <pid_is_cmd+0x134>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
sete   %al
mov    %al,-0x3f(%rbp)
mov    -0x3f(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_is_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r15
mov    %edi,%edx
lea    0xea8(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <sprintf@plt>
lea    0xea4(%rip),%rsi
mov    %rbx,%rdi
call   1040 <fopen@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     11f6 <pid_is_cmd+0xb6>
mov    %rax,%rbp
mov    0x2e64(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
call   1060 <getc@plt>
cmp    $0x28,%eax
je     11a3 <pid_is_cmd+0x63>
cmp    (%r12),%eax
jne    1190 <pid_is_cmd+0x50>
cmp    $0x28,%eax
je     11c0 <pid_is_cmd+0x80>
mov    %rbp,%rdi
call   1030 <fclose@plt>
jmp    11f6 <pid_is_cmd+0xb6>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
call   1060 <getc@plt>
mov    %eax,%ebx
cmp    (%r12),%eax
je     11de <pid_is_cmd+0x9e>
movsbl (%r15),%eax
cmp    %eax,%ebx
jne    11de <pid_is_cmd+0x9e>
add    $0x1,%r15
jmp    11c0 <pid_is_cmd+0x80>
mov    %rbp,%rdi
call   1030 <fclose@plt>
cmp    $0x29,%ebx
jne    11f6 <pid_is_cmd+0xb6>
xor    %r14d,%r14d
cmpb   $0x0,(%r15)
sete   %r14b
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
define i32 @pid_is_cmd(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [32 x i8]
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%10 = load i32, i32* %4
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %2
store i32 0, i32* %3
br label %65
17: ; preds = %2
br label %18
18: ; preds = %28, %17
%19 = load i32*, i32** %7
%20 = call i32 @getc(i32* %19)
store i32 %20, i32* %8
%21 = load i32, i32* @EOF
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* %8
%25 = icmp ne i32 %24, 40
br label %26
26: ; preds = %23, %18
%27 = phi i1 [ false, %18 ], [ %25, %23 ]
br i1 %27, label %28, label %29
28: ; preds = %26
br label %18
29: ; preds = %26
%30 = load i32, i32* %8
%31 = icmp ne i32 %30, 40
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32*, i32** %7
%34 = call i32 @fclose(i32* %33)
store i32 0, i32* %3
br label %65
35: ; preds = %29
br label %36
36: ; preds = %49, %35
%37 = load i32*, i32** %7
%38 = call i32 @getc(i32* %37)
store i32 %38, i32* %8
%39 = load i32, i32* @EOF
%40 = icmp ne i32 %38, %39
br i1 %40, label %41, label %47
41: ; preds = %36
%42 = load i32, i32* %8
%43 = load i8*, i8** %5
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %42, %45
br label %47
47: ; preds = %41, %36
%48 = phi i1 [ false, %36 ], [ %46, %41 ]
br i1 %48, label %49, label %52
49: ; preds = %47
%50 = load i8*, i8** %5
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %5
br label %36
52: ; preds = %47
%53 = load i32*, i32** %7
%54 = call i32 @fclose(i32* %53)
%55 = load i32, i32* %8
%56 = icmp eq i32 %55, 41
br i1 %56, label %57, label %62
57: ; preds = %52
%58 = load i8*, i8** %5
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 0
br label %62
62: ; preds = %57, %52
%63 = phi i1 [ false, %52 ], [ %61, %57 ]
%64 = zext i1 %63 to i32
store i32 %64, i32* %3
br label %65
65: ; preds = %62, %32, %16
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
define i32 @pid_is_cmd(i32 %0, i8* nocapture %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) 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 %41, label %8
8: ; preds = %2, %8
%9 = tail call i32 @getc(i32* %6)
%10 = load i32, i32* @EOF
%11 = icmp ne i32 %9, %10
%12 = icmp ne i32 %9, 40
%13 = and i1 %12, %11
br i1 %13, label %8, label %14
14: ; preds = %8
br i1 %12, label %19, label %15
15: ; preds = %14
%16 = tail call i32 @getc(i32* %6)
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %32, label %21
19: ; preds = %14
%20 = tail call i32 @fclose(i32* %6)
br label %41
21: ; preds = %15, %27
%22 = phi i32 [ %29, %27 ], [ %16, %15 ]
%23 = phi i8* [ %28, %27 ], [ %1, %15 ]
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %22, %25
br i1 %26, label %27, label %32
27: ; preds = %21
%28 = getelementptr inbounds i8, i8* %23, i64 1
%29 = tail call i32 @getc(i32* %6)
%30 = load i32, i32* @EOF
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %21
32: ; preds = %21, %27, %15
%33 = phi i8* [ %1, %15 ], [ %28, %27 ], [ %23, %21 ]
%34 = phi i32 [ %16, %15 ], [ %29, %27 ], [ %22, %21 ]
%35 = tail call i32 @fclose(i32* %6)
%36 = icmp eq i32 %34, 41
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = load i8, i8* %33
%39 = icmp eq i8 %38, 0
%40 = zext i1 %39 to i32
br label %41
41: ; preds = %32, %37, %2, %19
%42 = phi i32 [ 0, %19 ], [ 0, %2 ], [ 0, %32 ], [ %40, %37 ]
ret i32 %42
}