#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 FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, int);
scalar_t__ ftruncate(int, int /*<<< orphan*/);
int getpid();
scalar_t__ lock_reg(int, int /*<<< orphan*/, int /*<<< orphan*/,
                    int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strlen(char *);
int write(int, char *, scalar_t__);
int daemon_acquire_lock(const char *pidfile, int block_seconds) {
  int fd;
  int val;
  char buf[10];
  int tries = 0;
  if ((fd = open(pidfile, O_WRONLY | O_CREAT,
                 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
    return (-2);
  }
  while (lock_reg(fd, F_SETLK, F_WRLCK, 0, SEEK_SET, 0) < 0) {
    if (errno != EACCES && errno != EAGAIN) {
      close(fd);
      return (-2);
    }
    if (tries++ >= block_seconds) {
      close(fd);
      return (-1);
    }
    sleep(1);
  }
  if (ftruncate(fd, 0) < 0) {
    close(fd);
    return (-2);
  }
  sprintf(buf, "%d\n", getpid());
  if (write(fd, buf, strlen(buf)) != (int)strlen(buf)) {
    close(fd);
    return (-2);
  }
  if ((val = fcntl(fd, F_GETFD, 0)) < 0) {
  } else {
    val |= FD_CLOEXEC;
    if (fcntl(fd, F_SETFD, val) < 0) {
    }
  }
  return (fd);
}


x86 assembly (clang, -O0):
<daemon_acquire_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
mov    0x2e2e(%rip),%rax
or     (%rax),%esi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
mov    0x2dfc(%rip),%rax
or     (%rax),%edx
mov    0x2db3(%rip),%rax
or     (%rax),%edx
mov    0x2da2(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    120d <daemon_acquire_lock+0x6d>
movl   $0xfffffffe,-0x4(%rbp)
jmp    13c3 <daemon_acquire_lock+0x223>
jmp    1212 <daemon_acquire_lock+0x72>
mov    -0x18(%rbp),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
mov    0x2d7a(%rip),%rax
mov    (%rax),%r8d
xor    %r9d,%r9d
mov    %r9d,%ecx
call   1060 <lock_reg@plt>
cmp    $0x0,%rax
jge    12c5 <daemon_acquire_lock+0x125>
mov    0x2d63(%rip),%rax
mov    (%rax),%rax
mov    0x2d79(%rip),%rcx
cmp    (%rcx),%rax
je     128e <daemon_acquire_lock+0xee>
mov    0x2d49(%rip),%rax
mov    (%rax),%rax
mov    0x2cf7(%rip),%rcx
cmp    (%rcx),%rax
je     128e <daemon_acquire_lock+0xee>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    13c3 <daemon_acquire_lock+0x223>
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x2c(%rbp)
cmp    -0x14(%rbp),%eax
jl     12b6 <daemon_acquire_lock+0x116>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c3 <daemon_acquire_lock+0x223>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    1212 <daemon_acquire_lock+0x72>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1080 <ftruncate@plt>
cmp    $0x0,%rax
jge    12ed <daemon_acquire_lock+0x14d>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    13c3 <daemon_acquire_lock+0x223>
lea    -0x26(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
lea    0xcf7(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x26(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x26(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
call   1040 <write@plt>
mov    %eax,-0x30(%rbp)
lea    -0x26(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
je     1362 <daemon_acquire_lock+0x1c2>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    13c3 <daemon_acquire_lock+0x223>
mov    -0x18(%rbp),%edi
mov    0x2c34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1386 <daemon_acquire_lock+0x1e6>
jmp    13bd <daemon_acquire_lock+0x21d>
mov    0x2c2b(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2c21(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    13b8 <daemon_acquire_lock+0x218>
jmp    13b8 <daemon_acquire_lock+0x218>
jmp    13bd <daemon_acquire_lock+0x21d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_acquire_lock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2dbb(%rip),%rcx
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2dc1(%rip),%rcx
mov    0x2e0a(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2dbf(%rip),%rcx
or     (%rcx),%edx
mov    %esi,%ebp
mov    0x2dac(%rip),%rcx
or     (%rcx),%edx
mov    %eax,%esi
call   10a0 <open@plt>
mov    %eax,%ebx
mov    $0xfffffffe,%eax
test   %ebx,%ebx
js     1336 <daemon_acquire_lock+0x196>
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    0x2d74(%rip),%r12
mov    (%r12),%edx
mov    0x2d91(%rip),%r13
mov    0x0(%r13),%r8d
xor    %r14d,%r14d
mov    %ebx,%edi
xor    %ecx,%ecx
xor    %r9d,%r9d
call   1060 <lock_reg@plt>
test   %rax,%rax
js     12be <daemon_acquire_lock+0x11e>
mov    %ebx,%edi
xor    %esi,%esi
call   1080 <ftruncate@plt>
test   %rax,%rax
js     132a <daemon_acquire_lock+0x18a>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xdad(%rip),%rsi
lea    0xe(%rsp),%r15
mov    %r15,%rdi
mov    %eax,%edx
call   10b0 <sprintf@plt>
mov    %r15,%rdi
call   1070 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1070 <strlen@plt>
cmp    %eax,%ebp
jne    132a <daemon_acquire_lock+0x18a>
mov    0x2d10(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     12ba <daemon_acquire_lock+0x11a>
mov    0x2d12(%rip),%rcx
or     (%rcx),%eax
mov    0x2d11(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    %ebx,%eax
jmp    1336 <daemon_acquire_lock+0x196>
test   %ebp,%ebp
cmovle %r14d,%ebp
add    $0x1,%ebp
mov    0x2ce2(%rip),%r14
mov    0x2cfb(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r14),%rax
cmp    (%r15),%rax
je     12f4 <daemon_acquire_lock+0x154>
mov    0x2c79(%rip),%rcx
cmp    (%rcx),%rax
jne    132a <daemon_acquire_lock+0x18a>
add    $0xffffffff,%ebp
je     1345 <daemon_acquire_lock+0x1a5>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    0x2cd6(%rip),%rax
mov    (%rax),%esi
mov    (%r12),%edx
mov    0x0(%r13),%r8d
mov    %ebx,%edi
xor    %ecx,%ecx
xor    %r9d,%r9d
call   1060 <lock_reg@plt>
test   %rax,%rax
js     12e0 <daemon_acquire_lock+0x140>
jmp    1233 <daemon_acquire_lock+0x93>
mov    %ebx,%edi
call   1090 <close@plt>
mov    $0xfffffffe,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
call   1090 <close@plt>
mov    $0xffffffff,%eax
jmp    1336 <daemon_acquire_lock+0x196>

LLVM IR (-O0):
@O_WRONLY = 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
@F_SETLK = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str = [4 x i8] c"%d\0A\00"
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define i32 @daemon_acquire_lock(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [10 x i8]
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %9
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRGRP
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IROTH
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %10, i32 %13, i32 %20)
store i32 %21, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %2
store i32 -2, i32* %3
br label %96
24: ; preds = %2
br label %25
25: ; preds = %51, %24
%26 = load i32, i32* %6
%27 = load i32, i32* @F_SETLK
%28 = load i32, i32* @F_WRLCK
%29 = load i32, i32* @SEEK_SET
%30 = call i64 @lock_reg(i32 %26, i32 %27, i32 %28, i32 0, i32 %29, i32 0)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %53
32: ; preds = %25
%33 = load i64, i64* @errno
%34 = load i64, i64* @EACCES
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %43
36: ; preds = %32
%37 = load i64, i64* @errno
%38 = load i64, i64* @EAGAIN
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
store i32 -2, i32* %3
br label %96
43: ; preds = %36, %32
%44 = load i32, i32* %9
%45 = add nsw i32 %44, 1
store i32 %45, i32* %9
%46 = load i32, i32* %5
%47 = icmp sge i32 %44, %46
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %3
br label %96
51: ; preds = %43
%52 = call i32 @sleep(i32 1)
br label %25
53: ; preds = %25
%54 = load i32, i32* %6
%55 = call i64 @ftruncate(i32 %54, i32 0)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
store i32 -2, i32* %3
br label %96
60: ; preds = %53
%61 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%62 = call i32 (...) @getpid()
%63 = call i32 @sprintf(i8* %61, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %62)
%64 = load i32, i32* %6
%65 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%66 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%67 = call i64 @strlen(i8* %66)
%68 = call i32 @write(i32 %64, i8* %65, i64 %67)
%69 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%70 = call i64 @strlen(i8* %69)
%71 = trunc i64 %70 to i32
%72 = icmp ne i32 %68, %71
br i1 %72, label %73, label %76
73: ; preds = %60
%74 = load i32, i32* %6
%75 = call i32 @close(i32 %74)
store i32 -2, i32* %3
br label %96
76: ; preds = %60
%77 = load i32, i32* %6
%78 = load i32, i32* @F_GETFD
%79 = call i64 @fcntl(i32 %77, i32 %78, i32 0)
%80 = trunc i64 %79 to i32
store i32 %80, i32* %7
%81 = icmp slt i32 %80, 0
br i1 %81, label %82, label %83
82: ; preds = %76
br label %94
83: ; preds = %76
%84 = load i32, i32* @FD_CLOEXEC
%85 = load i32, i32* %7
%86 = or i32 %85, %84
store i32 %86, i32* %7
%87 = load i32, i32* %6
%88 = load i32, i32* @F_SETFD
%89 = load i32, i32* %7
%90 = call i64 @fcntl(i32 %87, i32 %88, i32 %89)
%91 = icmp slt i64 %90, 0
br i1 %91, label %92, label %93
92: ; preds = %83
br label %93
93: ; preds = %92, %83
br label %94
94: ; preds = %93, %82
%95 = load i32, i32* %6
store i32 %95, i32* %3
br label %96
96: ; preds = %94, %73, %57, %48, %40, %23
%97 = load i32, i32* %3
ret i32 %97
}

LLVM IR (-O2):
@O_WRONLY = 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
@F_SETLK = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str = [4 x i8] c"%d\0A\00"
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define i32 @daemon_acquire_lock(i8* nocapture %0, i32 %1) {
%3 = alloca [10 x i8]
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IROTH
%14 = or i32 %12, %13
%15 = tail call i32 @open(i8* %0, i32 %7, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %72, label %17
17: ; preds = %2
%18 = load i32, i32* @F_SETLK
%19 = load i32, i32* @F_WRLCK
%20 = load i32, i32* @SEEK_SET
%21 = tail call i64 @lock_reg(i32 %15, i32 %18, i32 %19, i32 0, i32 %20, i32 0)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %47
23: ; preds = %17
%24 = call i32 @llvm.smax.i32(i32 %1, i32 0)
br label %25
25: ; preds = %23, %39
%26 = phi i32 [ %40, %39 ], [ 0, %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 %35, label %33
33: ; preds = %25
%34 = tail call i32 @close(i32 %15)
br label %72
35: ; preds = %25
%36 = icmp eq i32 %26, %24
br i1 %36, label %37, label %39
37: ; preds = %35
%38 = tail call i32 @close(i32 %15)
br label %72
39: ; preds = %35
%40 = add nuw i32 %26, 1
%41 = tail call i32 @sleep(i32 1)
%42 = load i32, i32* @F_SETLK
%43 = load i32, i32* @F_WRLCK
%44 = load i32, i32* @SEEK_SET
%45 = tail call i64 @lock_reg(i32 %15, i32 %42, i32 %43, i32 0, i32 %44, i32 0)
%46 = icmp slt i64 %45, 0
br i1 %46, label %25, label %47
47: ; preds = %39, %17
%48 = tail call i64 @ftruncate(i32 %15, i32 0)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = tail call i32 @close(i32 %15)
br label %72
52: ; preds = %47
%53 = tail call i32 (...) @getpid()
%54 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %53)
%55 = call i64 @strlen(i8* %4)
%56 = call i32 @write(i32 %15, i8* %4, i64 %55)
%57 = call i64 @strlen(i8* %4)
%58 = trunc i64 %57 to i32
%59 = icmp eq i32 %56, %58
br i1 %59, label %62, label %60
60: ; preds = %52
%61 = tail call i32 @close(i32 %15)
br label %72
62: ; preds = %52
%63 = load i32, i32* @F_GETFD
%64 = tail call i64 @fcntl(i32 %15, i32 %63, i32 0)
%65 = trunc i64 %64 to i32
%66 = icmp slt i32 %65, 0
br i1 %66, label %72, label %67
67: ; preds = %62
%68 = load i32, i32* @FD_CLOEXEC
%69 = or i32 %68, %65
%70 = load i32, i32* @F_SETFD
%71 = tail call i64 @fcntl(i32 %15, i32 %70, i32 %69)
br label %72
72: ; preds = %67, %62, %2, %60, %50, %37, %33
%73 = phi i32 [ -2, %33 ], [ -1, %37 ], [ -2, %50 ], [ -2, %60 ], [ -2, %2 ], [ %15, %62 ], [ %15, %67 ]
ret i32 %73
}