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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int sscanf(char *, char *, int *);
int stat(char *, struct stat *);
int check_program_is_running(char *pidfile) {
  int pidfd;
  int err;
  int pid;
  int out;
  char buf[32];
  int rc = 0;
  struct stat st;
  if (!pidfile) {
    return -1;
  };
  err = stat(pidfile, &st);
  if (err) {
    return -1;
  };
  pidfd = open(pidfile, O_RDONLY);
  if (pidfd > 0) {
    err = read(pidfd, buf, sizeof(buf));
    if (err > 2) {
      close(pidfd);
      out = sscanf(buf, "%i\n", &pid);
      if (out == 1) {
        snprintf(buf, sizeof(buf), "/proc/%i/status", pid);
        pidfd = open(buf, O_RDONLY);
        if (pidfd > 0) {
          close(pidfd);
          rc = 1;
        }
      }
    } else {
      close(pidfd);
      rc = 0;
    }
  }
  return rc;
}


x86 assembly (clang, -O0):
<check_program_is_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x44(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118a <check_program_is_running+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <check_program_is_running+0x120>
mov    -0x10(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11b0 <check_program_is_running+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <check_program_is_running+0x120>
mov    -0x10(%rbp),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    127a <check_program_is_running+0x11a>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x2,-0x18(%rbp)
jle    1266 <check_program_is_running+0x106>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
lea    -0x40(%rbp),%rdi
lea    0xe00(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1070 <sscanf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
jne    1261 <check_program_is_running+0x101>
lea    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
mov    $0x20,%esi
lea    0xddb(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    125c <check_program_is_running+0xfc>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x44(%rbp)
jmp    1261 <check_program_is_running+0x101>
jmp    1275 <check_program_is_running+0x115>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x44(%rbp)
jmp    127a <check_program_is_running+0x11a>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_program_is_running>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     1185 <check_program_is_running+0x25>
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %eax,%eax
je     1192 <check_program_is_running+0x32>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e4f(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
xor    %ebp,%ebp
test   %eax,%eax
jle    1185 <check_program_is_running+0x25>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x20,%edx
call   1050 <read@plt>
mov    %eax,%r14d
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0x3,%r14d
jl     1185 <check_program_is_running+0x25>
lea    0xe2c(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x4(%rsp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    1185 <check_program_is_running+0x25>
mov    0x4(%rsp),%ecx
lea    0xe11(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1030 <snprintf@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
jle    1185 <check_program_is_running+0x25>
mov    %eax,%edi
call   1040 <close@plt>
mov    $0x1,%ebp
jmp    1185 <check_program_is_running+0x25>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [4 x i8] c"%i\0A\00"
@.str.1 = [16 x i8] c"/proc/%i/status\00"
define i32 @check_program_is_running(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [32 x i8]
%9 = alloca i32
%10 = alloca %struct.stat
store i8* %0, i8** %3
store i32 0, i32* %9
%11 = load i8*, i8** %3
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %1
store i32 -1, i32* %2
br label %59
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i32 @stat(i8* %15, %struct.stat* %10)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 -1, i32* %2
br label %59
20: ; preds = %14
%21 = load i8*, i8** %3
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %4
%24 = load i32, i32* %4
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %57
26: ; preds = %20
%27 = load i32, i32* %4
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%29 = call i32 @read(i32 %27, i8* %28, i32 32)
store i32 %29, i32* %5
%30 = load i32, i32* %5
%31 = icmp sgt i32 %30, 2
br i1 %31, label %32, label %53
32: ; preds = %26
%33 = load i32, i32* %4
%34 = call i32 @close(i32 %33)
%35 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%36 = call i32 @sscanf(i8* %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %6)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = icmp eq i32 %37, 1
br i1 %38, label %39, label %52
39: ; preds = %32
%40 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%41 = load i32, i32* %6
%42 = call i32 @snprintf(i8* %40, i32 32, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %41)
%43 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%44 = load i32, i32* @O_RDONLY
%45 = call i32 @open(i8* %43, i32 %44)
store i32 %45, i32* %4
%46 = load i32, i32* %4
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %39
%49 = load i32, i32* %4
%50 = call i32 @close(i32 %49)
store i32 1, i32* %9
br label %51
51: ; preds = %48, %39
br label %52
52: ; preds = %51, %32
br label %56
53: ; preds = %26
%54 = load i32, i32* %4
%55 = call i32 @close(i32 %54)
store i32 0, i32* %9
br label %56
56: ; preds = %53, %52
br label %57
57: ; preds = %56, %20
%58 = load i32, i32* %9
store i32 %58, i32* %2
br label %59
59: ; preds = %57, %19, %13
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [4 x i8] c"%i\0A\00"
@.str.1 = [16 x i8] c"/proc/%i/status\00"
define i32 @check_program_is_running(i8* %0) {
%2 = alloca i32
%3 = alloca [32 x i8]
%4 = alloca %struct.stat
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.stat* %4 to i8*
%8 = icmp eq i8* %0, null
br i1 %8, label %31, label %9
9: ; preds = %1
%10 = call i32 @stat(i8* %0, %struct.stat* %4)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %31
12: ; preds = %9
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %0, i32 %13)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %31
16: ; preds = %12
%17 = call i32 @read(i32 %14, i8* %6, i32 32)
%18 = icmp sgt i32 %17, 2
%19 = tail call i32 @close(i32 %14)
br i1 %18, label %20, label %31
20: ; preds = %16
%21 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = load i32, i32* %2
%25 = call i32 @snprintf(i8* %6, i32 32, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* %6, i32 %26)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @close(i32 %27)
br label %31
31: ; preds = %16, %12, %20, %29, %23, %9, %1
%32 = phi i32 [ -1, %1 ], [ -1, %9 ], [ 1, %29 ], [ 0, %23 ], [ 0, %20 ], [ 0, %12 ], [ 0, %16 ]
ret i32 %32
}