#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*/ errno;
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 /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int getpid();
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write_pid(char *pidfile) {
  FILE *f;
  int fd;
  int pid;
  if (((fd = open(pidfile, O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) ||
      ((f = fdopen(fd, "r+")) == NULL)) {
    fprintf(stderr, "Can't open or create %s.\n", pidfile);
    return 0;
  }
  if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
    fscanf(f, "%d", &pid);
    fclose(f);
    printf("Can't lock, lock is held by pid %d.\n", pid);
    return 0;
  }
  pid = getpid();
  if (!fprintf(f, "%d\n", pid)) {
    printf("Can't write pid , %s.\n", strerror(errno));
    close(fd);
    return 0;
  }
  fflush(f);
  if (flock(fd, LOCK_UN) == -1) {
    printf("Can't unlock pidfile %s, %s.\n", pidfile, strerror(errno));
    close(fd);
    return 0;
  }
  close(fd);
  return pid;
}


x86 assembly (clang, -O0):
<write_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
mov    0x2e17(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10b0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     120e <write_pid+0x5e>
mov    -0x1c(%rbp),%edi
lea    0xe05(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1236 <write_pid+0x86>
mov    0x2dab(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rdx
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    135e <write_pid+0x1ae>
mov    -0x1c(%rbp),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    0x2d57(%rip),%rax
or     (%rax),%esi
call   10c0 <flock@plt>
cmp    $0xffffffff,%eax
jne    1293 <write_pid+0xe3>
mov    -0x18(%rbp),%rdi
lea    0xdb9(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <fscanf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%esi
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    135e <write_pid+0x1ae>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xd9a(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
cmp    $0x0,%eax
jne    12ee <write_pid+0x13e>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    135e <write_pid+0x1ae>
mov    -0x18(%rbp),%rdi
call   1090 <fflush@plt>
mov    -0x1c(%rbp),%edi
mov    0x2caf(%rip),%rax
mov    (%rax),%esi
call   10c0 <flock@plt>
cmp    $0xffffffff,%eax
jne    1350 <write_pid+0x1a0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2c98(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xd2b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    135e <write_pid+0x1ae>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%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   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e15(%rip),%rax
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rdi,%r14
mov    0x2e18(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     127c <write_pid+0xcc>
mov    %eax,%ebx
lea    0xe0a(%rip),%rsi
mov    %eax,%edi
call   10a0 <fdopen@plt>
test   %rax,%rax
je     127c <write_pid+0xcc>
mov    %rax,%rbp
mov    0x2dc4(%rip),%rax
mov    0x2d8d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebx,%edi
call   10c0 <flock@plt>
cmp    $0xffffffff,%eax
je     12aa <write_pid+0xfa>
xor    %r15d,%r15d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r12d
lea    0xe0a(%rip),%rsi
mov    %rbp,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
test   %eax,%eax
je     12dd <write_pid+0x12d>
mov    %rbp,%rdi
call   1090 <fflush@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10c0 <flock@plt>
cmp    $0xffffffff,%eax
je     12fe <write_pid+0x14e>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r12d,%r15d
jmp    129a <write_pid+0xea>
mov    0x2d3d(%rip),%rax
movslq (%rax),%rdi
lea    0xd76(%rip),%rsi
xor    %r15d,%r15d
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6c(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbp,%rdi
call   1060 <fscanf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0xc(%rsp),%esi
lea    0xd4f(%rip),%rdi
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <printf@plt>
jmp    129a <write_pid+0xea>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd57(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1323 <write_pid+0x173>
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd4d(%rip),%rdi
xor    %r15d,%r15d
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    129a <write_pid+0xea>

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"
@stderr = global i32 0
@.str.1 = [26 x i8] c"Can't open or create %s.\0A\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [37 x i8] c"Can't lock, lock is held by pid %d.\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
@.str.5 = [23 x i8] c"Can't write pid , %s.\0A\00"
@errno = global i32 0
@LOCK_UN = global i32 0
@.str.6 = [30 x i8] c"Can't unlock pidfile %s, %s.\0A\00"
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 %25
19: ; preds = %15, %1
%20 = load i32, i32* @stderr
%21 = sext i32 %20 to i64
%22 = inttoptr i64 %21 to i32*
%23 = load i8*, i8** %3
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %23)
store i32 0, i32* %2
br label %69
25: ; preds = %15
%26 = load i32, i32* %5
%27 = load i32, i32* @LOCK_EX
%28 = load i32, i32* @LOCK_NB
%29 = or i32 %27, %28
%30 = call i32 @flock(i32 %26, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %39
32: ; preds = %25
%33 = load i32*, i32** %4
%34 = call i32 @fscanf(i32* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %6)
%35 = load i32*, i32** %4
%36 = call i32 @fclose(i32* %35)
%37 = load i32, i32* %6
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %37)
store i32 0, i32* %2
br label %69
39: ; preds = %25
%40 = call i32 (...) @getpid()
store i32 %40, i32* %6
%41 = load i32*, i32** %4
%42 = load i32, i32* %6
%43 = call i32 (i32*, i8*, ...) @fprintf(i32* %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %51, label %45
45: ; preds = %39
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %47)
%49 = load i32, i32* %5
%50 = call i32 @close(i32 %49)
store i32 0, i32* %2
br label %69
51: ; preds = %39
%52 = load i32*, i32** %4
%53 = call i32 @fflush(i32* %52)
%54 = load i32, i32* %5
%55 = load i32, i32* @LOCK_UN
%56 = call i32 @flock(i32 %54, i32 %55)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %65
58: ; preds = %51
%59 = load i8*, i8** %3
%60 = load i32, i32* @errno
%61 = call i8* @strerror(i32 %60)
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i8* %59, i8* %61)
%63 = load i32, i32* %5
%64 = call i32 @close(i32 %63)
store i32 0, i32* %2
br label %69
65: ; preds = %51
%66 = load i32, i32* %5
%67 = call i32 @close(i32 %66)
%68 = load i32, i32* %6
store i32 %68, i32* %2
br label %69
69: ; preds = %65, %58, %45, %32, %19
%70 = load i32, i32* %2
ret i32 %70
}

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"
@stderr = global i32 0
@.str.1 = [26 x i8] c"Can't open or create %s.\0A\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [37 x i8] c"Can't lock, lock is held by pid %d.\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
@.str.5 = [23 x i8] c"Can't write pid , %s.\0A\00"
@errno = global i32 0
@LOCK_UN = global i32 0
@.str.6 = [30 x i8] c"Can't unlock pidfile %s, %s.\0A\00"
define i32 @write_pid(i8* %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 %19
14: ; preds = %11, %1
%15 = load i32, i32* @stderr
%16 = sext i32 %15 to i64
%17 = inttoptr i64 %16 to i32*
%18 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %51
19: ; preds = %11
%20 = load i32, i32* @LOCK_EX
%21 = load i32, i32* @LOCK_NB
%22 = or i32 %21, %20
%23 = tail call i32 @flock(i32 %9, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %19
%26 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%27 = call i32 @fclose(i32* %12)
%28 = load i32, i32* %2
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %28)
br label %51
30: ; preds = %19
%31 = tail call i32 (...) @getpid()
%32 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i32, i32* @errno
%36 = tail call i8* @strerror(i32 %35)
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %36)
%38 = tail call i32 @close(i32 %9)
br label %51
39: ; preds = %30
%40 = tail call i32 @fflush(i32* %12)
%41 = load i32, i32* @LOCK_UN
%42 = tail call i32 @flock(i32 %9, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = load i32, i32* @errno
%46 = tail call i8* @strerror(i32 %45)
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i8* %0, i8* %46)
%48 = tail call i32 @close(i32 %9)
br label %51
49: ; preds = %39
%50 = tail call i32 @close(i32 %9)
br label %51
51: ; preds = %49, %44, %34, %25, %14
%52 = phi i32 [ 0, %14 ], [ 0, %25 ], [ 0, %44 ], [ %31, %49 ], [ 0, %34 ]
ret i32 %52
}