#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 LOCK_UN;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
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);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int getpid();
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int write_pid(char *pid_file) {
  FILE *f;
  int fd;
  int pid;
  if (((fd = open(pid_file, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) ||
      ((f = fdopen(fd, "r+")) == NULL)) {
    perror(pid_file);
    return 0;
  }
  if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
    fscanf(f, "%d", &pid);
    fclose(f);
    return 0;
  }
  pid = getpid();
  if (!fprintf(f, "%d\n", pid)) {
    close(fd);
    return 0;
  }
  fflush(f);
  if (flock(fd, LOCK_UN) == -1) {
    close(fd);
    return 0;
  }
  close(fd);
  return pid;
}


x86 assembly (clang, -O0):
<write_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
mov    0x2e27(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     11fe <write_pid+0x5e>
mov    -0x1c(%rbp),%edi
lea    0xe15(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1213 <write_pid+0x73>
mov    -0x10(%rbp),%rdi
call   10b0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12de <write_pid+0x13e>
mov    -0x1c(%rbp),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    0x2d8a(%rip),%rax
or     (%rax),%esi
call   10c0 <flock@plt>
cmp    $0xffffffff,%eax
jne    125f <write_pid+0xbf>
mov    -0x18(%rbp),%rdi
lea    0xdc2(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12de <write_pid+0x13e>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xd8f(%rip),%rsi
call   1070 <fprintf@plt>
cmp    $0x0,%eax
jne    1299 <write_pid+0xf9>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12de <write_pid+0x13e>
mov    -0x18(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d14(%rip),%rax
mov    (%rax),%esi
call   10c0 <flock@plt>
cmp    $0xffffffff,%eax
jne    12d0 <write_pid+0x130>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12de <write_pid+0x13e>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e2a(%rip),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdi,%r14
mov    0x2e2d(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1285 <write_pid+0xe5>
mov    %eax,%ebx
lea    0xe1f(%rip),%rsi
mov    %eax,%edi
call   1090 <fdopen@plt>
test   %rax,%rax
je     1285 <write_pid+0xe5>
mov    %rax,%rbp
mov    0x2dd5(%rip),%rax
mov    0x2dae(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebx,%edi
call   10c0 <flock@plt>
cmp    $0xffffffff,%eax
je     125e <write_pid+0xbe>
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r15d
lea    0xde0(%rip),%rsi
mov    %rbp,%rdi
mov    %eax,%edx
call   1070 <fprintf@plt>
test   %eax,%eax
je     127c <write_pid+0xdc>
mov    %rbp,%rdi
call   1080 <fflush@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10c0 <flock@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%ebp
cmovne %r15d,%r14d
jmp    1290 <write_pid+0xf0>
lea    0xd9e(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbp,%rdi
call   1050 <fscanf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
jmp    128d <write_pid+0xed>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1290 <write_pid+0xf0>
mov    %r14,%rdi
call   10b0 <perror@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [3 x i8] c"r+\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@LOCK_UN = global i32 0
define i32 @write_pid(i8* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8, i32 420)
%10 = icmp eq i32 %9, -1
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = tail call i32* @fdopen(i32 %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11, %1
%15 = tail call i32 @perror(i8* %0)
br label %38
16: ; preds = %11
%17 = load i32, i32* @LOCK_EX
%18 = load i32, i32* @LOCK_NB
%19 = or i32 %18, %17
%20 = tail call i32 @flock(i32 %9, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%24 = call i32 @fclose(i32* %12)
br label %38
25: ; preds = %16
%26 = tail call i32 (...) @getpid()
%27 = tail call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = tail call i32 @close(i32 %9)
br label %38
31: ; preds = %25
%32 = tail call i32 @fflush(i32* %12)
%33 = load i32, i32* @LOCK_UN
%34 = tail call i32 @flock(i32 %9, i32 %33)
%35 = icmp eq i32 %34, -1
%36 = tail call i32 @close(i32 %9)
%37 = select i1 %35, i32 0, i32 %26
br label %38
38: ; preds = %31, %29, %22, %14
%39 = phi i32 [ 0, %14 ], [ 0, %22 ], [ 0, %29 ], [ %37, %31 ]
ret i32 %39
}