#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 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();
  int ret = fprintf(f, "%d\n", pid);
  if (ret == 0 && ret == -1) {
    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    133d <write_pid+0x1ad>
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    133d <write_pid+0x1ad>
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    133d <write_pid+0x1ad>
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>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    132e <write_pid+0x19e>
cmpl   $0xffffffff,-0x28(%rbp)
jne    132e <write_pid+0x19e>
mov    0x2cbe(%rip),%rax
mov    (%rax),%edi
lea    0xd64(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    133d <write_pid+0x1ad>
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   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movl   $0x0,0xc(%rsp)
mov    0x2e46(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1210 <write_pid+0x90>
mov    %eax,%ebp
lea    0xe55(%rip),%rsi
mov    %eax,%edi
call   1080 <fdopen@plt>
test   %rax,%rax
je     1222 <write_pid+0xa2>
mov    %rax,%r14
mov    0x2e05(%rip),%rax
mov    0x2dde(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebp,%edi
call   10a0 <flock@plt>
cmp    $0xffffffff,%eax
je     1249 <write_pid+0xc9>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
lea    0xe79(%rip),%rsi
mov    %r14d,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14,%rdi
call   1070 <fflush@plt>
jmp    123e <write_pid+0xbe>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
jmp    1232 <write_pid+0xb2>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdd5(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %r14,%rdi
call   1050 <fscanf@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
cmp    $0x1,%ebx
jne    128b <write_pid+0x10b>
mov    0xc(%rsp),%edx
lea    0xdc6(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    123e <write_pid+0xbe>
lea    0xd96(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    123e <write_pid+0xbe>

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
%8 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %5
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %9, i32 %12, i32 420)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %3
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 0, i32* %2
br label %71
20: ; preds = %1
%21 = load i32, i32* %6
%22 = call i32* @fdopen(i32 %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %4
%23 = load i32*, i32** %4
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %3
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %27)
store i32 0, i32* %2
br label %71
29: ; preds = %20
%30 = load i32, i32* %6
%31 = load i32, i32* @LOCK_EX
%32 = load i32, i32* @LOCK_NB
%33 = or i32 %31, %32
%34 = call i32 @flock(i32 %30, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %51
36: ; preds = %29
%37 = load i32*, i32** %4
%38 = call i32 @fscanf(i32* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
store i32 %38, i32* %7
%39 = load i32*, i32** %4
%40 = call i32 @fclose(i32* %39)
%41 = load i32, i32* %7
%42 = icmp ne i32 %41, 1
br i1 %42, label %43, label %46
43: ; preds = %36
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %50
46: ; preds = %36
%47 = load i32, i32* @stderr
%48 = load i32, i32* %5
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %46, %43
store i32 0, i32* %2
br label %71
51: ; preds = %29
%52 = call i32 (...) @getpid()
store i32 %52, i32* %5
%53 = load i32*, i32** %4
%54 = ptrtoint i32* %53 to i32
%55 = load i32, i32* %5
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %55)
store i32 %56, i32* %8
%57 = load i32, i32* %8
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %67
59: ; preds = %51
%60 = load i32, i32* %8
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %67
62: ; preds = %59
%63 = load i32, i32* @stderr
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
%65 = load i32, i32* %6
%66 = call i32 @close(i32 %65)
store i32 0, i32* %2
br label %71
67: ; preds = %59, %51
%68 = load i32*, i32** %4
%69 = call i32 @fflush(i32* %68)
%70 = load i32, i32* %5
store i32 %70, i32* %2
br label %71
71: ; preds = %67, %62, %50, %25, %16
%72 = load i32, i32* %2
ret i32 %72
}

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"
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 %40
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 %40
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 %40
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 %40
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 = tail call i32 @fflush(i32* %13)
br label %40
40: ; preds = %29, %31, %34, %15, %9
%41 = phi i32 [ 0, %9 ], [ 0, %15 ], [ %35, %34 ], [ 0, %31 ], [ 0, %29 ]
ret i32 %41
}