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

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EAGAIN;
int LOG_DAEMON;
int LOG_ERR;
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftruncate(int, int /*<<< orphan*/);
int getpid();
scalar_t__ lockfile(int);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strerror(scalar_t__);
scalar_t__ strlen(char *);
int /*<<< orphan*/ syslog(int, char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int already_running() {
  int fd;
  char buf[16];
  fd = open("/var/run/judged.pid", O_RDWR | O_CREAT,
            (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
  if (fd < 0) {
    syslog(LOG_ERR | LOG_DAEMON, "can't open %s: %s", "/var/run/judged.pid",
           strerror(errno));
    exit(1);
  }
  if (lockfile(fd) < 0) {
    if (errno == EACCES || errno == EAGAIN) {
      close(fd);
      return 1;
    }
    syslog(LOG_ERR | LOG_DAEMON, "can't lock %s: %s", "/var/run/judged.pid",
           strerror(errno));
    exit(1);
  }
  ftruncate(fd, 0);
  sprintf(buf, "%d", getpid());
  write(fd, buf, strlen(buf) + 1);
  return (0);
}


x86 assembly (clang, -O0):
<already_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
mov    0x2df6(%rip),%rax
or     (%rax),%edx
mov    0x2dcd(%rip),%rax
or     (%rax),%edx
mov    0x2dbc(%rip),%rax
or     (%rax),%edx
lea    0xe0b(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    124d <already_running+0x9d>
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
mov    0x2dc1(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d95(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%ecx
lea    0xddd(%rip),%rsi
lea    0xdc2(%rip),%rdx
call   1080 <syslog@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1090 <lockfile@plt>
cmp    $0x0,%rax
jge    12ed <already_running+0x13d>
mov    0x2d52(%rip),%rax
mov    (%rax),%rax
mov    0x2d58(%rip),%rcx
cmp    (%rcx),%rax
je     1293 <already_running+0xe3>
mov    0x2d38(%rip),%rax
mov    (%rax),%rax
mov    0x2cfe(%rip),%rcx
cmp    (%rcx),%rax
jne    12a7 <already_running+0xf7>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1349 <already_running+0x199>
mov    0x2ce2(%rip),%rax
mov    (%rax),%eax
mov    0x2d21(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cf5(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%ecx
lea    0xd4f(%rip),%rsi
lea    0xd22(%rip),%rdx
call   1080 <syslog@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
lea    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
lea    0xd25(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<already_running>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e21(%rip),%rax
mov    0x2e32(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dcf(%rip),%rax
mov    0x2df8(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dcd(%rip),%rax
or     (%rax),%edx
mov    0x2dbc(%rip),%rax
or     (%rax),%edx
lea    0xe0b(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1287 <already_running+0xd7>
mov    %eax,%ebx
mov    %eax,%edi
call   1090 <lockfile@plt>
test   %rax,%rax
js     124e <already_running+0x9e>
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe0f(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %eax,%edx
call   10b0 <sprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <write@plt>
jmp    127c <already_running+0xcc>
mov    0x2d63(%rip),%rax
mov    (%rax),%rdi
mov    0x2d69(%rip),%rax
cmp    (%rax),%rdi
je     1270 <already_running+0xc0>
mov    0x2d1d(%rip),%rax
cmp    (%rax),%rdi
jne    12b1 <already_running+0x101>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d02(%rip),%rax
mov    0x2d43(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2d18(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
lea    0xd65(%rip),%rsi
jmp    12cf <already_running+0x11f>
mov    0x2cd8(%rip),%rax
mov    0x2d19(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
call   10d0 <strerror@plt>
lea    0xd57(%rip),%rsi
lea    0xd2a(%rip),%rdx
mov    %ebx,%edi
mov    %eax,%ecx
call   1080 <syslog@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"/var/run/judged.pid\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@LOG_ERR = global i32 0
@LOG_DAEMON = global i32 0
@.str.1 = [18 x i8] c"can't open %s: %s\00"
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.2 = [18 x i8] c"can't lock %s: %s\00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @already_running() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [16 x i8]
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRGRP
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IROTH
%13 = or i32 %11, %12
%14 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %13)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %0
%18 = load i32, i32* @LOG_ERR
%19 = load i32, i32* @LOG_DAEMON
%20 = or i32 %18, %19
%21 = load i64, i64* @errno
%22 = call i32 @strerror(i64 %21)
%23 = call i32 @syslog(i32 %20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %0
%26 = load i32, i32* %2
%27 = call i64 @lockfile(i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %48
29: ; preds = %25
%30 = load i64, i64* @errno
%31 = load i64, i64* @EACCES
%32 = icmp eq i64 %30, %31
br i1 %32, label %37, label %33
33: ; preds = %29
%34 = load i64, i64* @errno
%35 = load i64, i64* @EAGAIN
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %40
37: ; preds = %33, %29
%38 = load i32, i32* %2
%39 = call i32 @close(i32 %38)
store i32 1, i32* %1
br label %60
40: ; preds = %33
%41 = load i32, i32* @LOG_ERR
%42 = load i32, i32* @LOG_DAEMON
%43 = or i32 %41, %42
%44 = load i64, i64* @errno
%45 = call i32 @strerror(i64 %44)
%46 = call i32 @syslog(i32 %43, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %25
%49 = load i32, i32* %2
%50 = call i32 @ftruncate(i32 %49, i32 0)
%51 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%52 = call i32 (...) @getpid()
%53 = call i32 @sprintf(i8* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %2
%55 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%56 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%57 = call i64 @strlen(i8* %56)
%58 = add nsw i64 %57, 1
%59 = call i32 @write(i32 %54, i8* %55, i64 %58)
store i32 0, i32* %1
br label %60
60: ; preds = %48, %37
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
@.str = [20 x i8] c"/var/run/judged.pid\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@LOG_ERR = global i32 0
@LOG_DAEMON = global i32 0
@.str.1 = [18 x i8] c"can't open %s: %s\00"
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.2 = [18 x i8] c"can't lock %s: %s\00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @already_running() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRGRP
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IROTH
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %0
%16 = load i32, i32* @LOG_ERR
%17 = load i32, i32* @LOG_DAEMON
%18 = or i32 %17, %16
%19 = load i64, i64* @errno
%20 = tail call i32 @strerror(i64 %19)
%21 = tail call i32 @syslog(i32 %18, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %0
%24 = tail call i64 @lockfile(i32 %13)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %42
26: ; preds = %23
%27 = load i64, i64* @errno
%28 = load i64, i64* @EACCES
%29 = icmp eq i64 %27, %28
%30 = load i64, i64* @EAGAIN
%31 = icmp eq i64 %27, %30
%32 = select i1 %29, i1 true, i1 %31
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = tail call i32 @close(i32 %13)
br label %49
35: ; preds = %26
%36 = load i32, i32* @LOG_ERR
%37 = load i32, i32* @LOG_DAEMON
%38 = or i32 %37, %36
%39 = tail call i32 @strerror(i64 %27)
%40 = tail call i32 @syslog(i32 %38, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %39)
%41 = tail call i32 @exit(i32 1)
unreachable
42: ; preds = %23
%43 = tail call i32 @ftruncate(i32 %13, i32 0)
%44 = tail call i32 (...) @getpid()
%45 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %44)
%46 = call i64 @strlen(i8* %2)
%47 = add nsw i64 %46, 1
%48 = call i32 @write(i32 %13, i8* %2, i64 %47)
br label %49
49: ; preds = %42, %33
%50 = phi i32 [ 1, %33 ], [ 0, %42 ]
ret i32 %50
}