#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 */
struct stat {
  scalar_t__ st_dev;
  scalar_t__ st_ino;
};
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ exe;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int NAME_MAX;
int PATH_MAX;
int /*<<< orphan*/ asprintf(char **, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int fscanf(int /*<<< orphan*/ *, char *, char *);
int readlink(char *, char *, int);
scalar_t__ stat(char const *, struct stat *);
scalar_t__ strcmp(char *, char const *);
int check_proc(pid_t pid, const char *cmd, const char *path) {
  int rv = -1;
  char *file = NULL;
  char name[NAME_MAX];
  char exe[PATH_MAX];
  struct stat st_path, st_exe;
  FILE *fp = NULL;
  asprintf(&file, "/proc/%d/status", pid);
  fp = fopen(file, "r");
  if (!fp)
    goto out;
  if (fscanf(fp, "%*s %s", name) != 1)
    goto out;
  if (strcmp(name, cmd) != 0) {
    goto out;
  }
  free(file);
  asprintf(&file, "/proc/%d/exe", pid);
  bzero(exe, sizeof(exe));
  if (readlink(file, exe, PATH_MAX - 1) == -1)
    goto out;
  if (stat(path, &st_path) != 0)
    goto out;
  if (stat(exe, &st_exe) != 0)
    goto out;
  if (st_path.st_dev != st_exe.st_dev || st_path.st_ino != st_exe.st_ino)
    goto out;
  rv = 0;
out:
  if (fp)
    fclose(fp);
  fp = NULL;
  free(file);
  file = NULL;
  return rv;
}


x86 assembly (clang, -O0):
<check_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x80(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x78(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x68(%rbp)
mov    -0x4(%rbp),%edx
lea    -0x28(%rbp),%rdi
lea    0xde0(%rip),%rsi
call   1090 <asprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xde0(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    1249 <check_proc+0xb9>
jmp    1339 <check_proc+0x1a9>
mov    -0x80(%rbp),%rdx
mov    -0x68(%rbp),%rdi
lea    0xdba(%rip),%rsi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
je     126b <check_proc+0xdb>
jmp    1339 <check_proc+0x1a9>
mov    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1287 <check_proc+0xf7>
jmp    1339 <check_proc+0x1a9>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%edx
lea    -0x28(%rbp),%rdi
lea    0xd7b(%rip),%rsi
call   1090 <asprintf@plt>
mov    -0x70(%rbp),%rdi
mov    -0x78(%rbp),%rax
mov    %eax,%esi
call   10b0 <bzero@plt>
mov    -0x70(%rbp),%rsi
mov    -0x28(%rbp),%rdi
mov    0x2d2f(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
jne    12d9 <check_proc+0x149>
jmp    1339 <check_proc+0x1a9>
mov    -0x18(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     12f5 <check_proc+0x165>
jmp    1339 <check_proc+0x1a9>
mov    -0x70(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     1311 <check_proc+0x181>
jmp    1339 <check_proc+0x1a9>
mov    -0x50(%rbp),%rax
cmp    -0x60(%rbp),%rax
jne    132d <check_proc+0x19d>
mov    -0x48(%rbp),%rax
cmp    -0x58(%rbp),%rax
je     1332 <check_proc+0x1a2>
jmp    1339 <check_proc+0x1a9>
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x68(%rbp)
je     134d <check_proc+0x1bd>
mov    -0x68(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x68(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_proc>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdx,-0x40(%rbp)
mov    %rsi,-0x48(%rbp)
mov    %edi,%r13d
mov    %rsp,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    0x2e17(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe0c(%rip),%rsi
lea    -0x38(%rbp),%rdi
mov    %r13d,%edx
call   1090 <asprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1254 <check_proc+0xc4>
mov    %rax,%r12
lea    0xdf3(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    1240 <check_proc+0xb0>
mov    %r14,%rdi
mov    -0x48(%rbp),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1284 <check_proc+0xf4>
mov    -0x30(%rbp),%rbx
mov    $0xffffffff,%r14d
mov    %r12,%rdi
call   1050 <fclose@plt>
jmp    125e <check_proc+0xce>
mov    $0xffffffff,%r14d
mov    -0x30(%rbp),%rbx
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
mov    %rbx,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
lea    0xd85(%rip),%rsi
lea    -0x38(%rbp),%rdi
mov    %r13d,%edx
call   1090 <asprintf@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
call   10b0 <bzero@plt>
mov    -0x38(%rbp),%rdi
mov    0x2d3a(%rip),%rax
mov    (%rax),%edx
add    $0xffffffff,%edx
mov    %rbx,%rsi
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     1240 <check_proc+0xb0>
lea    -0x68(%rbp),%rsi
mov    -0x40(%rbp),%rdi
call   1080 <stat@plt>
test   %rax,%rax
jne    1240 <check_proc+0xb0>
lea    -0x58(%rbp),%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %rax,%rax
mov    $0xffffffff,%r14d
je     1302 <check_proc+0x172>
mov    -0x30(%rbp),%rbx
jmp    124a <check_proc+0xba>
mov    -0x68(%rbp),%rax
cmp    -0x58(%rbp),%rax
mov    -0x30(%rbp),%rbx
jne    124a <check_proc+0xba>
mov    -0x60(%rbp),%rax
xor    %r14d,%r14d
cmp    -0x50(%rbp),%rax
setne  %r14b
neg    %r14d
jmp    124a <check_proc+0xba>

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@NAME_MAX = global i32 0
@PATH_MAX = global i32 0
@.str = [16 x i8] c"/proc/%d/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"%*s %s\00"
@.str.3 = [13 x i8] c"/proc/%d/exe\00"
define i32 @check_proc(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca %struct.stat
%13 = alloca %struct.stat
%14 = alloca i32*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 -1, i32* %7
store i8* null, i8** %8
%15 = load i32, i32* @NAME_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %9
%18 = alloca i8, i64 %16
store i64 %16, i64* %10
%19 = load i32, i32* @PATH_MAX
%20 = zext i32 %19 to i64
%21 = alloca i8, i64 %20
store i64 %20, i64* %11
store i32* null, i32** %14
%22 = load i32, i32* %4
%23 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = load i8*, i8** %8
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %14
%26 = load i32*, i32** %14
%27 = icmp ne i32* %26, null
br i1 %27, label %29, label %28
28: ; preds = %3
br label %75
29: ; preds = %3
%30 = load i32*, i32** %14
%31 = call i32 @fscanf(i32* %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%32 = icmp ne i32 %31, 1
br i1 %32, label %33, label %34
33: ; preds = %29
br label %75
34: ; preds = %29
%35 = load i8*, i8** %5
%36 = call i64 @strcmp(i8* %18, i8* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %34
br label %75
39: ; preds = %34
%40 = load i8*, i8** %8
%41 = call i32 @free(i8* %40)
%42 = load i32, i32* %4
%43 = call i32 @asprintf(i8** %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = trunc i64 %20 to i32
%45 = call i32 @bzero(i8* %21, i32 %44)
%46 = load i8*, i8** %8
%47 = load i32, i32* @PATH_MAX
%48 = sub nsw i32 %47, 1
%49 = call i32 @readlink(i8* %46, i8* %21, i32 %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %52
51: ; preds = %39
br label %75
52: ; preds = %39
%53 = load i8*, i8** %6
%54 = call i64 @stat(i8* %53, %struct.stat* %12)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %52
br label %75
57: ; preds = %52
%58 = call i64 @stat(i8* %21, %struct.stat* %13)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %57
br label %75
61: ; preds = %57
%62 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%63 = load i64, i64* %62
%64 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%65 = load i64, i64* %64
%66 = icmp ne i64 %63, %65
br i1 %66, label %73, label %67
67: ; preds = %61
%68 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 1
%69 = load i64, i64* %68
%70 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 1
%71 = load i64, i64* %70
%72 = icmp ne i64 %69, %71
br i1 %72, label %73, label %74
73: ; preds = %67, %61
br label %75
74: ; preds = %67
store i32 0, i32* %7
br label %75
75: ; preds = %74, %73, %60, %56, %51, %38, %33, %28
%76 = load i32*, i32** %14
%77 = icmp ne i32* %76, null
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load i32*, i32** %14
%80 = call i32 @fclose(i32* %79)
br label %81
81: ; preds = %78, %75
store i32* null, i32** %14
%82 = load i8*, i8** %8
%83 = call i32 @free(i8* %82)
store i8* null, i8** %8
%84 = load i32, i32* %7
%85 = load i8*, i8** %9
ret i32 %84
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@NAME_MAX = global i32 0
@PATH_MAX = global i32 0
@.str = [16 x i8] c"/proc/%d/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"%*s %s\00"
@.str.3 = [13 x i8] c"/proc/%d/exe\00"
define i32 @check_proc(i32 %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca %struct.stat
%7 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%8 = load i32, i32* @NAME_MAX
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = alloca i8, i64 %13
%15 = bitcast %struct.stat* %5 to i8*
%16 = bitcast %struct.stat* %6 to i8*
%17 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%18 = load i8*, i8** %4
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %59, label %21
21: ; preds = %3
%22 = call i32 @fscanf(i32* %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %56
24: ; preds = %21
%25 = call i64 @strcmp(i8* %11, i8* %1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %56
27: ; preds = %24
%28 = load i8*, i8** %4
%29 = call i32 @free(i8* %28)
%30 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%31 = call i32 @bzero(i8* %14, i32 %12)
%32 = load i8*, i8** %4
%33 = load i32, i32* @PATH_MAX
%34 = add nsw i32 %33, -1
%35 = call i32 @readlink(i8* %32, i8* %14, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %56, label %37
37: ; preds = %27
%38 = call i64 @stat(i8* %2, %struct.stat* %5)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %56
40: ; preds = %37
%41 = call i64 @stat(i8* %14, %struct.stat* %6)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %56
43: ; preds = %40
%44 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%45 = load i64, i64* %44
%46 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%47 = load i64, i64* %46
%48 = icmp eq i64 %45, %47
br i1 %48, label %49, label %56
49: ; preds = %43
%50 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%51 = load i64, i64* %50
%52 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 1
%53 = load i64, i64* %52
%54 = icmp ne i64 %51, %53
%55 = sext i1 %54 to i32
br label %56
56: ; preds = %49, %43, %40, %37, %27, %24, %21
%57 = phi i32 [ %55, %49 ], [ -1, %43 ], [ -1, %40 ], [ -1, %37 ], [ -1, %27 ], [ -1, %24 ], [ -1, %21 ]
%58 = call i32 @fclose(i32* %19)
br label %59
59: ; preds = %3, %56
%60 = phi i32 [ %57, %56 ], [ -1, %3 ]
%61 = load i8*, i8** %4
%62 = call i32 @free(i8* %61)
store i8* null, i8** %4
ret i32 %60
}