#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int getpid();
scalar_t__ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ *stderr;
char *strerror(int /*<<< orphan*/);
int check_pid(void) {
  FILE *fd;
  int pid = getpid();
  int oldpid;
  fd = fopen("/tmp/snesbot.pid", "r");
  if (fd != NULL) {
    fscanf(fd, "%d", &oldpid);
    if (pid != oldpid && kill(oldpid, 0) == 0) {
      fprintf(stderr, "Already running as PID %d\n", oldpid);
      fclose(fd);
      return 1;
    } else {
      fclose(fd);
      remove("/tmp/snesbot.pid");
    }
  }
  fd = fopen("/tmp/snesbot.pid", "w");
  if (fd == NULL) {
    fprintf(stderr, "Error: Can't open or create %s: %s\n", "/tmp/snesbot.pid",
            strerror(errno));
    return 1;
  }
  fprintf(fd, "%d\n", pid);
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<check_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x14(%rbp)
lea    0xe67(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1232 <check_pid+0xb2>
mov    -0x10(%rbp),%rdi
lea    0xe54(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1060 <fscanf@plt>
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     1218 <check_pid+0x98>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1080 <kill@plt>
cmp    $0x0,%rax
jne    1218 <check_pid+0x98>
mov    0x2df1(%rip),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%edx
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bd <check_pid+0x13d>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xdd8(%rip),%rdi
call   1030 <remove@plt>
jmp    1232 <check_pid+0xb2>
lea    0xdc7(%rip),%rdi
lea    0xdf1(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1298 <check_pid+0x118>
mov    0x2d85(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x20(%rbp),%rdi
mov    %rax,%rcx
lea    0xdb5(%rip),%rsi
lea    0xd7b(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bd <check_pid+0x13d>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xd87(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_pid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r14d
lea    0xe67(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11ee <check_pid+0x6e>
mov    %rax,%rbx
lea    0xe5f(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
mov    0xc(%rsp),%edi
cmp    %edi,%r14d
je     11da <check_pid+0x5a>
xor    %esi,%esi
call   1080 <kill@plt>
test   %rax,%rax
je     125e <check_pid+0xde>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe17(%rip),%rdi
call   1030 <remove@plt>
lea    0xe0b(%rip),%rdi
lea    0xe35(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1229 <check_pid+0xa9>
mov    %rax,%rbp
lea    0xe1d(%rip),%rsi
xor    %ebx,%ebx
mov    %rax,%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
jmp    1287 <check_pid+0x107>
mov    0x2db0(%rip),%rax
mov    (%rax),%rbx
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdeb(%rip),%rsi
lea    0xdb1(%rip),%rdx
mov    %rbx,%rdi
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1282 <check_pid+0x102>
mov    0x2d7b(%rip),%rax
mov    (%rax),%rdi
mov    0xc(%rsp),%edx
lea    0xda3(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/tmp/snesbot.pid\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@stderr = global i32* null
@.str.3 = [27 x i8] c"Already running as PID %d\0A\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [36 x i8] c"Error: Can't open or create %s: %s\0A\00"
@errno = global i32 0
@.str.6 = [4 x i8] c"%d\0A\00"
define i32 @check_pid() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = call i32 (...) @getpid()
store i32 %5, i32* %3
%6 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %30
9: ; preds = %0
%10 = load i32*, i32** %2
%11 = call i32 @fscanf(i32* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%12 = load i32, i32* %3
%13 = load i32, i32* %4
%14 = icmp ne i32 %12, %13
br i1 %14, label %15, label %25
15: ; preds = %9
%16 = load i32, i32* %4
%17 = call i64 @kill(i32 %16, i32 0)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = load i32*, i32** @stderr
%21 = load i32, i32* %4
%22 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = load i32*, i32** %2
%24 = call i32 @fclose(i32* %23)
store i32 1, i32* %1
br label %45
25: ; preds = %15, %9
%26 = load i32*, i32** %2
%27 = call i32 @fclose(i32* %26)
%28 = call i32 @remove(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %25
br label %30
30: ; preds = %29, %0
%31 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %31, i32** %2
%32 = load i32*, i32** %2
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i32*, i32** @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i32*, i8*, ...) @fprintf(i32* %35, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %37)
store i32 1, i32* %1
br label %45
39: ; preds = %30
%40 = load i32*, i32** %2
%41 = load i32, i32* %3
%42 = call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %41)
%43 = load i32*, i32** %2
%44 = call i32 @fclose(i32* %43)
store i32 0, i32* %1
br label %45
45: ; preds = %39, %34, %19
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
@.str = [17 x i8] c"/tmp/snesbot.pid\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@stderr = global i32* null
@.str.3 = [27 x i8] c"Already running as PID %d\0A\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [36 x i8] c"Error: Can't open or create %s: %s\0A\00"
@errno = global i32 0
@.str.6 = [4 x i8] c"%d\0A\00"
define i32 @check_pid() {
%1 = alloca i32
%2 = tail call i32 (...) @getpid()
%3 = bitcast i32* %1 to i8*
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %21, label %6
6: ; preds = %0
%7 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%8 = load i32, i32* %1
%9 = icmp eq i32 %2, %8
br i1 %9, label %18, label %10
10: ; preds = %6
%11 = call i64 @kill(i32 %8, i32 0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = load i32*, i32** @stderr
%15 = load i32, i32* %1
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = call i32 @fclose(i32* %4)
br label %32
18: ; preds = %10, %6
%19 = call i32 @fclose(i32* %4)
%20 = call i32 @remove(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %18, %0
%22 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i32*, i32** @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i32*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %27)
br label %32
29: ; preds = %21
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %2)
%31 = call i32 @fclose(i32* %22)
br label %32
32: ; preds = %29, %24, %13
%33 = phi i32 [ 1, %13 ], [ 1, %24 ], [ 0, %29 ]
ret i32 %33
}