#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, long);
int pid_is_cmd(pid_t pid, const char *name) {
  char buf[PATH_MAX];
  FILE *f;
  int c;
  snprintf(buf, sizeof(buf), "/proc/%ld/stat", (long)pid);
  f = fopen(buf, "r");
  if (!f)
    return 0;
  while ((c = getc(f)) != EOF && c != '(')
    ;
  if (c != '(') {
    fclose(f);
    return 0;
  }
  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    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e99(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%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,-0x28(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe77(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x40(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c0 <pid_is_cmd+0x80>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12c0 <pid_is_cmd+0x180>
jmp    11c5 <pid_is_cmd+0x85>
mov    -0x30(%rbp),%rdi
call   1060 <getc@plt>
mov    %eax,%ecx
mov    %ecx,-0x34(%rbp)
xor    %eax,%eax
mov    0x2e04(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x41(%rbp)
je     11f1 <pid_is_cmd+0xb1>
cmpl   $0x28,-0x34(%rbp)
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1201 <pid_is_cmd+0xc1>
jmp    1206 <pid_is_cmd+0xc6>
jmp    11c5 <pid_is_cmd+0x85>
cmpl   $0x28,-0x34(%rbp)
je     122c <pid_is_cmd+0xec>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12c0 <pid_is_cmd+0x180>
jmp    1231 <pid_is_cmd+0xf1>
mov    -0x30(%rbp),%rdi
call   1060 <getc@plt>
mov    %eax,%ecx
mov    %ecx,-0x34(%rbp)
xor    %eax,%eax
mov    0x2d98(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x42(%rbp)
je     1265 <pid_is_cmd+0x125>
mov    -0x34(%rbp),%eax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
sete   %al
mov    %al,-0x42(%rbp)
mov    -0x42(%rbp),%al
test   $0x1,%al
jne    1275 <pid_is_cmd+0x135>
jmp    1286 <pid_is_cmd+0x146>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1231 <pid_is_cmd+0xf1>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
cmpl   $0x29,-0x34(%rbp)
mov    %al,-0x43(%rbp)
jne    12ae <pid_is_cmd+0x16e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
sete   %al
mov    %al,-0x43(%rbp)
mov    -0x43(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_is_cmd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rcx
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe88(%rip),%rdx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
lea    0xe88(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     1219 <pid_is_cmd+0xd9>
mov    %rax,%rbx
mov    0x2e3b(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1060 <getc@plt>
cmp    $0x28,%eax
je     11c3 <pid_is_cmd+0x83>
cmp    0x0(%r13),%eax
jne    11b0 <pid_is_cmd+0x70>
cmp    $0x28,%eax
je     11e0 <pid_is_cmd+0xa0>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1219 <pid_is_cmd+0xd9>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1060 <getc@plt>
mov    %eax,%r12d
cmp    0x0(%r13),%eax
je     1200 <pid_is_cmd+0xc0>
movsbl (%r15),%eax
cmp    %eax,%r12d
jne    1200 <pid_is_cmd+0xc0>
add    $0x1,%r15
jmp    11e0 <pid_is_cmd+0xa0>
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    $0x29,%r12d
jne    1219 <pid_is_cmd+0xd9>
xor    %r14d,%r14d
cmpb   $0x0,(%r15)
sete   %r14b
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 = [15 x i8] c"/proc/%ld/stat\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
define i32 @pid_is_cmd(i64 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i64 %0, i64* %4
store i8* %1, i8** %5
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
%15 = trunc i64 %12 to i32
%16 = load i64, i64* %4
%17 = call i32 @snprintf(i8* %14, i32 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i64 %16)
%18 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %8
%19 = load i32*, i32** %8
%20 = icmp ne i32* %19, null
br i1 %20, label %22, label %21
21: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %10
br label %70
22: ; preds = %2
br label %23
23: ; preds = %33, %22
%24 = load i32*, i32** %8
%25 = call i32 @getc(i32* %24)
store i32 %25, i32* %9
%26 = load i32, i32* @EOF
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* %9
%30 = icmp ne i32 %29, 40
br label %31
31: ; preds = %28, %23
%32 = phi i1 [ false, %23 ], [ %30, %28 ]
br i1 %32, label %33, label %34
33: ; preds = %31
br label %23
34: ; preds = %31
%35 = load i32, i32* %9
%36 = icmp ne i32 %35, 40
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32*, i32** %8
%39 = call i32 @fclose(i32* %38)
store i32 0, i32* %3
store i32 1, i32* %10
br label %70
40: ; preds = %34
br label %41
41: ; preds = %54, %40
%42 = load i32*, i32** %8
%43 = call i32 @getc(i32* %42)
store i32 %43, i32* %9
%44 = load i32, i32* @EOF
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %52
46: ; preds = %41
%47 = load i32, i32* %9
%48 = load i8*, i8** %5
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %47, %50
br label %52
52: ; preds = %46, %41
%53 = phi i1 [ false, %41 ], [ %51, %46 ]
br i1 %53, label %54, label %57
54: ; preds = %52
%55 = load i8*, i8** %5
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %5
br label %41
57: ; preds = %52
%58 = load i32*, i32** %8
%59 = call i32 @fclose(i32* %58)
%60 = load i32, i32* %9
%61 = icmp eq i32 %60, 41
br i1 %61, label %62, label %67
62: ; preds = %57
%63 = load i8*, i8** %5
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = icmp eq i32 %65, 0
br label %67
67: ; preds = %62, %57
%68 = phi i1 [ false, %57 ], [ %66, %62 ]
%69 = zext i1 %68 to i32
store i32 %69, i32* %3
store i32 1, i32* %10
br label %70
70: ; preds = %67, %37, %21
%71 = load i8*, i8** %6
%72 = load i32, i32* %3
ret i32 %72
}

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