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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int daemon_acquire_lock(char const *, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int daemon_lock_pid(const char *pidfile, int *savefd) {
  int fd = daemon_acquire_lock(pidfile, 0);
  if (fd >= 0) {
    if (savefd) {
      *savefd = fd;
    }
    return (0);
  }
  if (fd == -1) {
    int pid = 0;
    fd = open(pidfile, O_RDONLY);
    if (fd >= 0) {
      char buf[10];
      int len = read(fd, buf, sizeof(buf) - 1);
      if (len > 0) {
        buf[len] = 0;
        pid = atoi(buf);
      }
      close(fd);
    }
    return pid ?: 1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<daemon_lock_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <daemon_acquire_lock@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1198 <daemon_lock_pid+0x48>
cmpq   $0x0,-0x18(%rbp)
je     118c <daemon_lock_pid+0x3c>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    123c <daemon_lock_pid+0xec>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1235 <daemon_lock_pid+0xe5>
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1203 <daemon_lock_pid+0xb3>
mov    -0x1c(%rbp),%edi
lea    -0x2a(%rbp),%rsi
mov    $0x9,%edx
call   1050 <read@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jle    11fb <daemon_lock_pid+0xab>
movslq -0x30(%rbp),%rax
movb   $0x0,-0x2a(%rbp,%rax,1)
lea    -0x2a(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
je     121d <daemon_lock_pid+0xcd>
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    122a <daemon_lock_pid+0xda>
mov    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    122a <daemon_lock_pid+0xda>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123c <daemon_lock_pid+0xec>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<daemon_lock_pid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
xor    %r14d,%r14d
xor    %esi,%esi
call   1030 <daemon_acquire_lock@plt>
test   %eax,%eax
js     1175 <daemon_lock_pid+0x25>
test   %rbx,%rbx
je     11db <daemon_lock_pid+0x8b>
mov    %eax,(%rbx)
jmp    11db <daemon_lock_pid+0x8b>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
jne    11db <daemon_lock_pid+0x8b>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11d5 <daemon_lock_pid+0x85>
mov    %eax,%ebp
lea    0x6(%rsp),%rsi
mov    %eax,%edi
mov    $0x9,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11ce <daemon_lock_pid+0x7e>
mov    %eax,%eax
movb   $0x0,0x6(%rsp,%rax,1)
lea    0x6(%rsp),%rdi
call   1070 <atoi@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1040 <close@plt>
test   %r14d,%r14d
jne    11db <daemon_lock_pid+0x8b>
jmp    11d5 <daemon_lock_pid+0x85>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @daemon_lock_pid(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
%10 = load i8*, i8** %4
%11 = call i32 @daemon_acquire_lock(i8* %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i32*, i32** %5
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* %6
%19 = load i32*, i32** %5
store i32 %18, i32* %19
br label %20
20: ; preds = %17, %14
store i32 0, i32* %3
br label %53
21: ; preds = %2
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %52
24: ; preds = %21
store i32 0, i32* %7
%25 = load i8*, i8** %4
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp sge i32 %28, 0
br i1 %29, label %30, label %45
30: ; preds = %24
%31 = load i32, i32* %6
%32 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 9)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %30
%37 = load i32, i32* %9
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 %38
store i8 0, i8* %39
%40 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%41 = call i32 @atoi(i8* %40)
store i32 %41, i32* %7
br label %42
42: ; preds = %36, %30
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %42, %24
%46 = load i32, i32* %7
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %45
br label %50
49: ; preds = %45
br label %50
50: ; preds = %49, %48
%51 = phi i32 [ %46, %48 ], [ 1, %49 ]
store i32 %51, i32* %3
br label %53
52: ; preds = %21
store i32 -1, i32* %3
br label %53
53: ; preds = %52, %50, %20
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @daemon_lock_pid(i8* %0, i32* writeonly %1) {
%3 = alloca [10 x i8]
%4 = tail call i32 @daemon_acquire_lock(i8* %0, i32 0)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = icmp eq i32* %1, null
br i1 %7, label %28, label %8
8: ; preds = %6
store i32 %4, i32* %1
br label %28
9: ; preds = %2
%10 = icmp eq i32 %4, -1
br i1 %10, label %11, label %28
11: ; preds = %9
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %0, i32 %12)
%14 = icmp sgt i32 %13, -1
br i1 %14, label %15, label %27
15: ; preds = %11
%16 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%17 = call i32 @read(i32 %13, i8* %16, i32 9)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = tail call i32 @close(i32 %13)
br label %27
21: ; preds = %15
%22 = zext i32 %17 to i64
%23 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 %22
store i8 0, i8* %23
%24 = call i32 @atoi(i8* %16)
%25 = tail call i32 @close(i32 %13)
%26 = icmp eq i32 %24, 0
br i1 %26, label %27, label %28
27: ; preds = %11, %19, %21
br label %28
28: ; preds = %27, %21, %9, %6, %8
%29 = phi i32 [ 0, %8 ], [ 0, %6 ], [ -1, %9 ], [ 1, %27 ], [ %24, %21 ]
ret i32 %29
}