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

/* Variables and functions */
scalar_t__ EWOULDBLOCK;
int LOCK_EX;
int LOCK_NB;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_INFO;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
scalar_t__ dprintf(int, char *, int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ flock(int, int);
scalar_t__ getpid();
int open(char const *, int, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int create_pidfile(const char *pidfile) {
  int fd = open(pidfile, O_CREAT | O_TRUNC | O_WRONLY, 0644);
  if (fd == -1) {
    syslog(LOG_ERR, "Failed to create new pidfile at %s", pidfile);
    return -1;
  }
  if (flock(fd, LOCK_EX | LOCK_NB)) {
    if (errno == EWOULDBLOCK)
      syslog(LOG_ERR, "An instance of usbwalld is already running!");
    else
      syslog(LOG_ERR, "Failed to lock pidfile");
    close(fd);
    return -1;
  }
  if (dprintf(fd, "%zd", (ssize_t)getpid()) <= 0) {
    syslog(LOG_ERR, "Failed to write to pidfile at %s", pidfile);
    close(fd);
    return -1;
  }
  syslog(LOG_INFO, "Pidfile created at %s", pidfile);
  return fd;
}


x86 assembly (clang, -O0):
<create_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
mov    0x2e1f(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c9 <create_pidfile+0x69>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4a(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <create_pidfile+0x166>
mov    -0x14(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    0x2dbc(%rip),%rax
or     (%rax),%esi
call   1080 <flock@plt>
cmp    $0x0,%rax
je     124e <create_pidfile+0xee>
mov    0x2dd4(%rip),%rax
mov    (%rax),%rax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%rax
jne    1223 <create_pidfile+0xc3>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
jmp    123a <create_pidfile+0xda>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <create_pidfile+0x166>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xdff(%rip),%rsi
call   1050 <dprintf@plt>
cmp    $0x0,%rax
jg     12a5 <create_pidfile+0x145>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <create_pidfile+0x166>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdd2(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_pidfile>:
push   %r14
push   %rbx
push   %rax
mov    0x2e8d(%rip),%rax
mov    0x2e7e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdi,%r14
mov    0x2e28(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11f5 <create_pidfile+0x95>
mov    %eax,%ebx
mov    0x2e3e(%rip),%rax
mov    0x2df7(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebx,%edi
call   1080 <flock@plt>
test   %rax,%rax
je     1216 <create_pidfile+0xb6>
mov    0x2e10(%rip),%rax
mov    (%rax),%rax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rax
lea    0xe73(%rip),%rsi
cmove  %rax,%rsi
xor    %eax,%eax
call   1060 <syslog@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    124c <create_pidfile+0xec>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <syslog@plt>
mov    $0xffffffff,%ebx
jmp    124c <create_pidfile+0xec>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe42(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1050 <dprintf@plt>
test   %rax,%rax
jle    1256 <create_pidfile+0xf6>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <syslog@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <syslog@plt>
jmp    11e7 <create_pidfile+0x87>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@LOG_ERR = global i32 0
@.str = [35 x i8] c"Failed to create new pidfile at %s\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@.str.1 = [44 x i8] c"An instance of usbwalld is already running!\00"
@.str.2 = [23 x i8] c"Failed to lock pidfile\00"
@.str.3 = [4 x i8] c"%zd\00"
@.str.4 = [33 x i8] c"Failed to write to pidfile at %s\00"
@LOG_INFO = global i32 0
@.str.5 = [22 x i8] c"Pidfile created at %s\00"
define i32 @create_pidfile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @O_WRONLY
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %5, i32 %10, i32 420)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i32, i32* @LOG_ERR
%16 = load i8*, i8** %3
%17 = call i32 (i32, i8*, ...) @syslog(i32 %15, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %2
br label %55
18: ; preds = %1
%19 = load i32, i32* %4
%20 = load i32, i32* @LOCK_EX
%21 = load i32, i32* @LOCK_NB
%22 = or i32 %20, %21
%23 = call i64 @flock(i32 %19, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %18
%26 = load i64, i64* @errno
%27 = load i64, i64* @EWOULDBLOCK
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* @LOG_ERR
%31 = call i32 (i32, i8*, ...) @syslog(i32 %30, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
br label %35
32: ; preds = %25
%33 = load i32, i32* @LOG_ERR
%34 = call i32 (i32, i8*, ...) @syslog(i32 %33, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %32, %29
%36 = load i32, i32* %4
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %2
br label %55
38: ; preds = %18
%39 = load i32, i32* %4
%40 = call i64 (...) @getpid()
%41 = trunc i64 %40 to i32
%42 = call i64 @dprintf(i32 %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %41)
%43 = icmp sle i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %38
%45 = load i32, i32* @LOG_ERR
%46 = load i8*, i8** %3
%47 = call i32 (i32, i8*, ...) @syslog(i32 %45, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %46)
%48 = load i32, i32* %4
%49 = call i32 @close(i32 %48)
store i32 -1, i32* %2
br label %55
50: ; preds = %38
%51 = load i32, i32* @LOG_INFO
%52 = load i8*, i8** %3
%53 = call i32 (i32, i8*, ...) @syslog(i32 %51, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %52)
%54 = load i32, i32* %4
store i32 %54, i32* %2
br label %55
55: ; preds = %50, %44, %35, %14
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@LOG_ERR = global i32 0
@.str = [35 x i8] c"Failed to create new pidfile at %s\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@.str.1 = [44 x i8] c"An instance of usbwalld is already running!\00"
@.str.2 = [23 x i8] c"Failed to lock pidfile\00"
@.str.3 = [4 x i8] c"%zd\00"
@.str.4 = [33 x i8] c"Failed to write to pidfile at %s\00"
@LOG_INFO = global i32 0
@.str.5 = [22 x i8] c"Pidfile created at %s\00"
define i32 @create_pidfile(i8* %0) {
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_TRUNC
%4 = or i32 %3, %2
%5 = load i32, i32* @O_WRONLY
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* %0, i32 %6, i32 420)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @LOG_ERR
%11 = tail call i32 (i32, i8*, ...) @syslog(i32 %10, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %38
12: ; preds = %1
%13 = load i32, i32* @LOCK_EX
%14 = load i32, i32* @LOCK_NB
%15 = or i32 %14, %13
%16 = tail call i64 @flock(i32 %7, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %26, label %18
18: ; preds = %12
%19 = load i64, i64* @errno
%20 = load i64, i64* @EWOULDBLOCK
%21 = icmp eq i64 %19, %20
%22 = load i32, i32* @LOG_ERR
%23 = select i1 %21, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0)
%24 = tail call i32 (i32, i8*, ...) @syslog(i32 %22, i8* %23)
%25 = tail call i32 @close(i32 %7)
br label %38
26: ; preds = %12
%27 = tail call i64 (...) @getpid()
%28 = trunc i64 %27 to i32
%29 = tail call i64 @dprintf(i32 %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = icmp slt i64 %29, 1
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = load i32, i32* @LOG_ERR
%33 = tail call i32 (i32, i8*, ...) @syslog(i32 %32, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%34 = tail call i32 @close(i32 %7)
br label %38
35: ; preds = %26
%36 = load i32, i32* @LOG_INFO
%37 = tail call i32 (i32, i8*, ...) @syslog(i32 %36, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %38
38: ; preds = %35, %31, %18, %9
%39 = phi i32 [ -1, %9 ], [ -1, %18 ], [ -1, %31 ], [ %7, %35 ]
ret i32 %39
}