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

/* Variables and functions */
int LOCK_EX;
int LOCK_NB;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int flock(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int getpid();
int open(char const *, int, int);
int /*<<< orphan*/ stderr;
int write_pid(const char *pidfile) {
  FILE *f;
  int pid = 0;
  int fd = open(pidfile, O_RDWR | O_CREAT, 0644);
  if (fd == -1) {
    fprintf(stderr, "Can't create %s.\n", pidfile);
    return 0;
  }
  f = fdopen(fd, "r+");
  if (f == NULL) {
    fprintf(stderr, "Can't open %s.\n", pidfile);
    return 0;
  }
  if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
    int n = fscanf(f, "%d", &pid);
    fclose(f);
    if (n != 1) {
      fprintf(stderr, "Can't lock and read pidfile.\n");
    } else {
      fprintf(stderr, "Can't lock pidfile, lock is held by pid %d.\n", pid);
    }
    return 0;
  }
  pid = getpid();
  if (!fprintf(f, "%d\n", pid)) {
    fprintf(stderr, "Can't write pid.\n");
    close(fd);
    return 0;
  }
  fflush(f);
  return pid;
}


x86 assembly (clang, -O0):
<write_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e41(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11f7 <write_pid+0x67>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132f <write_pid+0x19f>
mov    -0x20(%rbp),%edi
lea    0xe11(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    123c <write_pid+0xac>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132f <write_pid+0x19f>
mov    -0x20(%rbp),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    0x2d69(%rip),%rax
or     (%rax),%esi
call   10b0 <flock@plt>
cmp    $0xffffffff,%eax
jne    12cb <write_pid+0x13b>
mov    -0x18(%rbp),%rdi
lea    0xdbb(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1050 <fscanf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x1,-0x24(%rbp)
je     12a5 <write_pid+0x115>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xd8f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    12bf <write_pid+0x12f>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xd8e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132f <write_pid+0x19f>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%edi
mov    -0x1c(%rbp),%edx
lea    0xd8e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
cmp    $0x0,%eax
jne    1320 <write_pid+0x190>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
lea    0xd72(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    132f <write_pid+0x19f>
mov    -0x18(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
movl   $0x0,0x4(%rsp)
mov    0x2e37(%rip),%rax
mov    0x2e48(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     122d <write_pid+0x9d>
mov    %eax,%ebp
lea    0xe46(%rip),%rsi
mov    %eax,%edi
call   1090 <fdopen@plt>
test   %rax,%rax
je     123f <write_pid+0xaf>
mov    %rax,%r15
mov    0x2df6(%rip),%rax
mov    0x2dcf(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebp,%edi
call   10b0 <flock@plt>
cmp    $0xffffffff,%eax
je     126a <write_pid+0xda>
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
lea    0xe67(%rip),%rsi
mov    %r15d,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
test   %eax,%eax
je     12ad <write_pid+0x11d>
mov    %r15,%rdi
call   1080 <fflush@plt>
mov    %ebx,%r14d
jmp    125c <write_pid+0xcc>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
jmp    124f <write_pid+0xbf>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
xor    %r14d,%r14d
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb4(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %r15,%rdi
call   1050 <fscanf@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
cmp    $0x1,%ebx
jne    12cd <write_pid+0x13d>
mov    0x4(%rsp),%edx
lea    0xda5(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    125c <write_pid+0xcc>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    125c <write_pid+0xcc>
lea    0xd54(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    125c <write_pid+0xcc>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"Can't create %s.\0A\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [16 x i8] c"Can't open %s.\0A\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [30 x i8] c"Can't lock and read pidfile.\0A\00"
@.str.5 = [45 x i8] c"Can't lock pidfile, lock is held by pid %d.\0A\00"
@.str.6 = [4 x i8] c"%d\0A\00"
@.str.7 = [18 x i8] c"Can't write pid.\0A\00"
define i32 @write_pid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %5
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %8, i32 %11, i32 420)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %3
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 0, i32* %2
br label %66
19: ; preds = %1
%20 = load i32, i32* %6
%21 = call i32* @fdopen(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %4
%22 = load i32*, i32** %4
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %3
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %26)
store i32 0, i32* %2
br label %66
28: ; preds = %19
%29 = load i32, i32* %6
%30 = load i32, i32* @LOCK_EX
%31 = load i32, i32* @LOCK_NB
%32 = or i32 %30, %31
%33 = call i32 @flock(i32 %29, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %50
35: ; preds = %28
%36 = load i32*, i32** %4
%37 = call i32 @fscanf(i32* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
store i32 %37, i32* %7
%38 = load i32*, i32** %4
%39 = call i32 @fclose(i32* %38)
%40 = load i32, i32* %7
%41 = icmp ne i32 %40, 1
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %49
45: ; preds = %35
%46 = load i32, i32* @stderr
%47 = load i32, i32* %5
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %45, %42
store i32 0, i32* %2
br label %66
50: ; preds = %28
%51 = call i32 (...) @getpid()
store i32 %51, i32* %5
%52 = load i32*, i32** %4
%53 = ptrtoint i32* %52 to i32
%54 = load i32, i32* %5
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %54)
%56 = icmp ne i32 %55, 0
br i1 %56, label %62, label %57
57: ; preds = %50
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
%60 = load i32, i32* %6
%61 = call i32 @close(i32 %60)
store i32 0, i32* %2
br label %66
62: ; preds = %50
%63 = load i32*, i32** %4
%64 = call i32 @fflush(i32* %63)
%65 = load i32, i32* %5
store i32 %65, i32* %2
br label %66
66: ; preds = %62, %57, %49, %24, %15
%67 = load i32, i32* %2
ret i32 %67
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"Can't create %s.\0A\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [16 x i8] c"Can't open %s.\0A\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [30 x i8] c"Can't lock and read pidfile.\0A\00"
@.str.5 = [45 x i8] c"Can't lock pidfile, lock is held by pid %d.\0A\00"
@.str.6 = [4 x i8] c"%d\0A\00"
@.str.7 = [18 x i8] c"Can't write pid.\0A\00"
define i32 @write_pid(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%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* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %46
12: ; preds = %1
%13 = tail call i32* @fdopen(i32 %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %46
18: ; preds = %12
%19 = load i32, i32* @LOCK_EX
%20 = load i32, i32* @LOCK_NB
%21 = or i32 %20, %19
%22 = tail call i32 @flock(i32 %7, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %34
24: ; preds = %18
%25 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%26 = call i32 @fclose(i32* %13)
%27 = icmp eq i32 %25, 1
%28 = load i32, i32* @stderr
br i1 %27, label %31, label %29
29: ; preds = %24
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %46
31: ; preds = %24
%32 = load i32, i32* %2
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i32 %32)
br label %46
34: ; preds = %18
%35 = tail call i32 (...) @getpid()
%36 = ptrtoint i32* %13 to i64
%37 = trunc i64 %36 to i32
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %35)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %34
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
%43 = tail call i32 @close(i32 %7)
br label %46
44: ; preds = %34
%45 = tail call i32 @fflush(i32* %13)
br label %46
46: ; preds = %29, %31, %44, %40, %15, %9
%47 = phi i32 [ 0, %9 ], [ 0, %15 ], [ %35, %44 ], [ 0, %40 ], [ 0, %31 ], [ 0, %29 ]
ret i32 %47
}