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

/* Variables and functions */
int /*<<< orphan*/ F_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
char *basename(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strcmp(char *, char const *);
int /*<<< orphan*/ xreadlink(char *, char *, int);
int check_proc_name(int pid, const char *name) {
  char buf[128];
  FILE *f;
  sprintf(buf, "/proc/%d/comm", pid);
  if ((f = fopen(buf, "r"))) {
    fgets(buf, sizeof(buf), f);
    if (strcmp(buf, name) == 0)
      return 1;
  } else {
    return 0;
  }
  fclose(f);
  sprintf(buf, "/proc/%d/cmdline", pid);
  f = fopen(buf, "r");
  fgets(buf, sizeof(buf), f);
  if (strcmp(basename(buf), name) == 0)
    return 1;
  fclose(f);
  sprintf(buf, "/proc/%d/exe", pid);
  if (access(buf, F_OK) != 0)
    return 0;
  xreadlink(buf, buf, sizeof(buf));
  if (strcmp(basename(buf), name) == 0)
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<check_proc_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe5d(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xe58(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x98(%rbp)
cmp    $0x0,%rax
je     120f <check_proc_name+0x8f>
lea    -0x90(%rbp),%rdi
mov    -0x98(%rbp),%rdx
mov    $0x80,%esi
call   1050 <fgets@plt>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    120a <check_proc_name+0x8a>
movl   $0x1,-0x4(%rbp)
jmp    1337 <check_proc_name+0x1b7>
jmp    121b <check_proc_name+0x9b>
movl   $0x0,-0x4(%rbp)
jmp    1337 <check_proc_name+0x1b7>
mov    -0x98(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xdd8(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xdc3(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x98(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x98(%rbp),%rdx
mov    $0x80,%esi
call   1050 <fgets@plt>
lea    -0x90(%rbp),%rdi
call   1070 <basename@plt>
mov    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    129d <check_proc_name+0x11d>
movl   $0x1,-0x4(%rbp)
jmp    1337 <check_proc_name+0x1b7>
mov    -0x98(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xd67(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2d0b(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
je     12ea <check_proc_name+0x16a>
movl   $0x0,-0x4(%rbp)
jmp    1337 <check_proc_name+0x1b7>
lea    -0x90(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1040 <xreadlink@plt>
lea    -0x90(%rbp),%rdi
call   1070 <basename@plt>
mov    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1330 <check_proc_name+0x1b0>
movl   $0x1,-0x4(%rbp)
jmp    1337 <check_proc_name+0x1b7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_proc_name>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
mov    %rsi,%r14
mov    %edi,%r12d
lea    0xe64(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r12d,%edx
call   10a0 <sprintf@plt>
lea    0xe5d(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     12b0 <check_proc_name+0x130>
mov    %rax,%rbp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
mov    $0x1,%ebx
test   %rax,%rax
je     12b0 <check_proc_name+0x130>
mov    %rbp,%rdi
call   1030 <fclose@plt>
lea    0xe0e(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %r12d,%edx
call   10a0 <sprintf@plt>
lea    0xdf7(%rip),%rsi
mov    %rbp,%rdi
call   1090 <fopen@plt>
mov    %rax,%r15
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %rbp,%rdi
call   1070 <basename@plt>
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     12b0 <check_proc_name+0x130>
mov    %r15,%rdi
call   1030 <fclose@plt>
lea    0xdc8(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r12d,%edx
call   10a0 <sprintf@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
mov    $0x0,%ebx
test   %rax,%rax
jne    12b0 <check_proc_name+0x130>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1040 <xreadlink@plt>
mov    %rbx,%rdi
call   1070 <basename@plt>
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
xor    %ebx,%ebx
test   %rax,%rax
sete   %bl
mov    %ebx,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/comm\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [17 x i8] c"/proc/%d/cmdline\00"
@.str.3 = [13 x i8] c"/proc/%d/exe\00"
@F_OK = global i32 0
define i32 @check_proc_name(i32 %0, i8* %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 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 %32, label %8
8: ; preds = %2
%9 = call i32 @fgets(i8* %4, i32 128, i32* %6)
%10 = call i64 @strcmp(i8* %4, i8* %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %32, label %12
12: ; preds = %8
%13 = call i32 @fclose(i32* %6)
%14 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%15 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @fgets(i8* %4, i32 128, i32* %15)
%17 = call i8* @basename(i8* %4)
%18 = call i64 @strcmp(i8* %17, i8* %1)
%19 = icmp eq i64 %18, 0
br i1 %19, label %32, label %20
20: ; preds = %12
%21 = call i32 @fclose(i32* %15)
%22 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%23 = load i32, i32* @F_OK
%24 = call i64 @access(i8* %4, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %20
%27 = call i32 @xreadlink(i8* %4, i8* %4, i32 128)
%28 = call i8* @basename(i8* %4)
%29 = call i64 @strcmp(i8* %28, i8* %1)
%30 = icmp eq i64 %29, 0
%31 = zext i1 %30 to i32
br label %32
32: ; preds = %26, %20, %12, %2, %8
%33 = phi i32 [ 1, %8 ], [ 0, %2 ], [ 1, %12 ], [ 0, %20 ], [ %31, %26 ]
ret i32 %33
}