#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*/ procPath;
typedef scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ getMutex();
int /*<<< orphan*/ snprintf(char *, int, char *, scalar_t__);
scalar_t__ stat(char *, struct stat *);
pid_t serverRunning() {
  char procPath[256];
  struct stat st;
  pid_t serverPid = getMutex();
  if (serverPid == 0) {
    return 0;
  }
  snprintf(procPath, sizeof(procPath), "/proc/%d", serverPid);
  if (stat(procPath, &st)) {
    return 0;
  }
  return serverPid;
}


x86 assembly (clang, -O0):
<serverRunning>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    $0x0,%al
call   1030 <getMutex@plt>
mov    %rax,-0x120(%rbp)
cmpq   $0x0,-0x120(%rbp)
jne    1164 <serverRunning+0x34>
movq   $0x0,-0x8(%rbp)
jmp    11b8 <serverRunning+0x88>
lea    -0x110(%rbp),%rdi
mov    -0x120(%rbp),%rcx
mov    $0x100,%esi
lea    0xe82(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    -0x118(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     11ad <serverRunning+0x7d>
movq   $0x0,-0x8(%rbp)
jmp    11b8 <serverRunning+0x88>
mov    -0x120(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serverRunning>:
push   %r14
push   %rbx
sub    $0x118,%rsp
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getMutex@plt>
test   %rax,%rax
je     117d <serverRunning+0x4d>
mov    %rax,%r14
lea    0xeae(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %rax,%rcx
call   1040 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
xor    %ebx,%ebx
test   %rax,%rax
cmove  %r14,%rbx
mov    %rbx,%rax
add    $0x118,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"/proc/%d\00"
define i64 @serverRunning() {
%1 = alloca i64
%2 = alloca [256 x i8]
%3 = alloca %struct.stat
%4 = alloca i64
%5 = call i64 (...) @getMutex()
store i64 %5, i64* %4
%6 = load i64, i64* %4
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %0
store i64 0, i64* %1
br label %19
9: ; preds = %0
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%11 = load i64, i64* %4
%12 = call i32 @snprintf(i8* %10, i32 256, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i64 %11)
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%14 = call i64 @stat(i8* %13, %struct.stat* %3)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %9
store i64 0, i64* %1
br label %19
17: ; preds = %9
%18 = load i64, i64* %4
store i64 %18, i64* %1
br label %19
19: ; preds = %17, %16, %8
%20 = load i64, i64* %1
ret i64 %20
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"/proc/%d\00"
define i64 @serverRunning() {
%1 = alloca [256 x i8]
%2 = alloca %struct.stat
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.stat* %2 to i8*
%5 = tail call i64 (...) @getMutex()
%6 = icmp eq i64 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %0
%8 = call i32 @snprintf(i8* %3, i32 256, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i64 %5)
%9 = call i64 @stat(i8* %3, %struct.stat* %2)
%10 = icmp eq i64 %9, 0
%11 = select i1 %10, i64 %5, i64 0
br label %12
12: ; preds = %7, %0
%13 = phi i64 [ 0, %0 ], [ %11, %7 ]
ret i64 %13
}