#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ F_TLOCK;
int /*<<< orphan*/ 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 /*<<< orphan*/);
int /*<<< orphan*/ ftruncate(int, int /*<<< orphan*/);
scalar_t__ getpid();
int lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, long);
int /*<<< orphan*/ strerror(scalar_t__);
scalar_t__ strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *,
                          int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int already_running() {
  int fd, ret;
  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, "can't open %s : %s", "/var/run/judged.pid",
           strerror(errno));
  }
  ret = lockf(fd, F_TLOCK, 0);
  if (ret < 0) {
    if (errno == EACCES || errno == EAGAIN) {
      close(fd);
      return -1;
    }
    syslog(LOG_ERR, "can't lock %s : %s", "/var/run/judged.pid",
           strerror(errno));
    exit(EXIT_FAILURE);
  }
  ftruncate(fd, 0);
  sprintf(buf, "%ld", (long)getpid());
  write(fd, buf, strlen(buf) + 1);
  return 0;
}


x86 assembly (clang, -O0):
<already_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
mov    0x2dc7(%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    123a <already_running+0x8a>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%ecx
lea    0xde6(%rip),%rsi
lea    0xdcb(%rip),%rdx
call   1090 <syslog@plt>
mov    -0x8(%rbp),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <lockf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12e3 <already_running+0x133>
mov    0x2d57(%rip),%rax
mov    (%rax),%rax
mov    0x2d5d(%rip),%rcx
cmp    (%rcx),%rax
je     128e <already_running+0xde>
mov    0x2d3d(%rip),%rax
mov    (%rax),%rax
mov    0x2cfb(%rip),%rcx
cmp    (%rcx),%rax
jne    12a2 <already_running+0xf2>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <already_running+0x190>
mov    0x2cdf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d03(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%ecx
lea    0xd5e(%rip),%rsi
lea    0xd30(%rip),%rdx
call   1090 <syslog@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%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    %rax,%rdx
lea    0xd30(%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    0x2e19(%rip),%rax
mov    0x2e32(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dc7(%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>
mov    %eax,%ebx
test   %eax,%eax
jns    122f <already_running+0x7f>
mov    0x2d81(%rip),%rax
mov    (%rax),%ebp
mov    0x2da8(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
lea    0xdf5(%rip),%rsi
lea    0xdda(%rip),%rdx
mov    %ebp,%edi
mov    %eax,%ecx
call   1090 <syslog@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <lockf@plt>
test   %eax,%eax
js     1284 <already_running+0xd4>
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xddc(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rax,%rdx
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    12b2 <already_running+0x102>
mov    0x2d2d(%rip),%rax
mov    (%rax),%rdi
mov    0x2d33(%rip),%rax
cmp    (%rax),%rdi
je     12a6 <already_running+0xf6>
mov    0x2cdf(%rip),%rax
cmp    (%rax),%rdi
jne    12bd <already_running+0x10d>
mov    %ebx,%edi
call   1080 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2cc4(%rip),%rax
mov    (%rax),%ebx
call   10d0 <strerror@plt>
lea    0xd55(%rip),%rsi
lea    0xd27(%rip),%rdx
mov    %ebx,%edi
mov    %eax,%ecx
call   1090 <syslog@plt>
mov    0x2cb7(%rip),%rax
mov    (%rax),%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
@.str.1 = [19 x i8] c"can't open %s : %s\00"
@errno = global i64 0
@F_TLOCK = global i32 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.2 = [19 x i8] c"can't lock %s : %s\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [4 x i8] c"%ld\00"
define i32 @already_running() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [16 x i8]
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IROTH
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %14)
store i32 %15, i32* %2
%16 = load i32, i32* %2
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %0
%19 = load i32, i32* @LOG_ERR
%20 = load i64, i64* @errno
%21 = call i32 @strerror(i64 %20)
%22 = call i32 @syslog(i32 %19, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %18, %0
%24 = load i32, i32* %2
%25 = load i32, i32* @F_TLOCK
%26 = call i32 @lockf(i32 %24, i32 %25, i32 0)
store i32 %26, i32* %3
%27 = load i32, i32* %3
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %47
29: ; preds = %23
%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 %59
40: ; preds = %33
%41 = load i32, i32* @LOG_ERR
%42 = load i64, i64* @errno
%43 = call i32 @strerror(i64 %42)
%44 = call i32 @syslog(i32 %41, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %43)
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %23
%48 = load i32, i32* %2
%49 = call i32 @ftruncate(i32 %48, i32 0)
%50 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%51 = call i64 (...) @getpid()
%52 = call i32 @sprintf(i8* %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 %51)
%53 = load i32, i32* %2
%54 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%55 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%56 = call i64 @strlen(i8* %55)
%57 = add nsw i64 %56, 1
%58 = call i32 @write(i32 %53, i8* %54, i64 %57)
store i32 0, i32* %1
br label %59
59: ; preds = %47, %37
%60 = load i32, i32* %1
ret i32 %60
}

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
@.str.1 = [19 x i8] c"can't open %s : %s\00"
@errno = global i64 0
@F_TLOCK = global i32 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.2 = [19 x i8] c"can't lock %s : %s\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [4 x i8] c"%ld\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 %20
15: ; preds = %0
%16 = load i32, i32* @LOG_ERR
%17 = load i64, i64* @errno
%18 = tail call i32 @strerror(i64 %17)
%19 = tail call i32 @syslog(i32 %16, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %15, %0
%21 = load i32, i32* @F_TLOCK
%22 = tail call i32 @lockf(i32 %13, i32 %21, i32 0)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %39
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = load i64, i64* @EACCES
%27 = icmp eq i64 %25, %26
%28 = load i64, i64* @EAGAIN
%29 = icmp eq i64 %25, %28
%30 = select i1 %27, i1 true, i1 %29
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = tail call i32 @close(i32 %13)
br label %46
33: ; preds = %24
%34 = load i32, i32* @LOG_ERR
%35 = tail call i32 @strerror(i64 %25)
%36 = tail call i32 @syslog(i32 %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = tail call i32 @exit(i32 %37)
unreachable
39: ; preds = %20
%40 = tail call i32 @ftruncate(i32 %13, i32 0)
%41 = tail call i64 (...) @getpid()
%42 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 %41)
%43 = call i64 @strlen(i8* %2)
%44 = add nsw i64 %43, 1
%45 = call i32 @write(i32 %13, i8* %2, i64 %44)
br label %46
46: ; preds = %39, %31
%47 = phi i32 [ -1, %31 ], [ 0, %39 ]
ret i32 %47
}