#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 /*<<< orphan*/ LOG_ERR;
int O_CREAT;
int O_RDWR;
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 getpid();
int open(char *, int, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
int writepid(char *pidfile) {
  FILE *file;
  int fd;
  int pid;
  if (((fd = open(pidfile, O_RDWR | O_CREAT, 0644)) == -1) ||
      ((file = fdopen(fd, "r+")) == NULL)) {
    syslog(LOG_ERR, "Error, open %s: %m", pidfile);
    return 0;
  }
  if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
    syslog(LOG_ERR, "Error, flock %s: %m", pidfile);
    fclose(file);
    return 0;
  }
  pid = getpid();
  if (!fprintf(file, "%d\n", pid)) {
    syslog(LOG_ERR, "Error, fprintf %s: %m", pidfile);
    fclose(file);
    return 0;
  }
  fflush(file);
  if (flock(fd, LOCK_UN) == -1) {
    syslog(LOG_ERR, "Error, flock %s: %m", pidfile);
    fclose(file);
    return 0;
  }
  fclose(file);
  return pid;
}


x86 assembly (clang, -O0):
<writepid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     11d5 <writepid+0x55>
mov    -0x1c(%rbp),%edi
lea    0xe3e(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11fa <writepid+0x7a>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1a(%rip),%rsi
call   1070 <syslog@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ff <writepid+0x17f>
mov    -0x1c(%rbp),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    0x2da3(%rip),%rax
or     (%rax),%esi
call   10a0 <flock@plt>
cmp    $0xffffffff,%eax
jne    124b <writepid+0xcb>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xde5(%rip),%rsi
call   1070 <syslog@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ff <writepid+0x17f>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xdc7(%rip),%rsi
call   1050 <fprintf@plt>
cmp    $0x0,%eax
jne    129f <writepid+0x11f>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xda9(%rip),%rsi
call   1070 <syslog@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ff <writepid+0x17f>
mov    -0x18(%rbp),%rdi
call   1060 <fflush@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d16(%rip),%rax
mov    (%rax),%esi
call   10a0 <flock@plt>
cmp    $0xffffffff,%eax
jne    12f0 <writepid+0x170>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd40(%rip),%rsi
call   1070 <syslog@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ff <writepid+0x17f>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writepid>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
mov    0x2e4e(%rip),%rax
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1231 <writepid+0xb1>
mov    %eax,%ebp
lea    0xe47(%rip),%rsi
mov    %eax,%edi
call   1080 <fdopen@plt>
test   %rax,%rax
je     1231 <writepid+0xb1>
mov    %rax,%r14
mov    0x2e09(%rip),%rax
mov    0x2dda(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %ebp,%edi
call   10a0 <flock@plt>
cmp    $0xffffffff,%eax
je     124d <writepid+0xcd>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r12d
lea    0xe31(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1050 <fprintf@plt>
test   %eax,%eax
je     1271 <writepid+0xf1>
mov    %r14,%rdi
call   1060 <fflush@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   10a0 <flock@plt>
cmp    $0xffffffff,%eax
je     124d <writepid+0xcd>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r12d,%ebx
jmp    1291 <writepid+0x111>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
mov    %r15,%rdx
call   1070 <syslog@plt>
xor    %ebx,%ebx
jmp    1291 <writepid+0x111>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xdb9(%rip),%rsi
mov    %r15,%rdx
call   1070 <syslog@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %ebx,%ebx
jmp    1291 <writepid+0x111>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
mov    %r15,%rdx
call   1070 <syslog@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [3 x i8] c"r+\00"
@LOG_ERR = global i32 0
@.str.1 = [19 x i8] c"Error, open %s: %m\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@.str.2 = [20 x i8] c"Error, flock %s: %m\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [22 x i8] c"Error, fprintf %s: %m\00"
@LOCK_UN = global i32 0
define i32 @writepid(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 = call i32 @open(i8* %7, i32 %10, i32 420)
store i32 %11, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %17, label %13
13: ; preds = %1
%14 = load i32, i32* %5
%15 = call i32* @fdopen(i32 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %4
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %13, %1
%18 = load i32, i32* @LOG_ERR
%19 = load i8*, i8** %3
%20 = call i32 @syslog(i32 %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %19)
store i32 0, i32* %2
br label %63
21: ; preds = %13
%22 = load i32, i32* %5
%23 = load i32, i32* @LOCK_EX
%24 = load i32, i32* @LOCK_NB
%25 = or i32 %23, %24
%26 = call i32 @flock(i32 %22, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %21
%29 = load i32, i32* @LOG_ERR
%30 = load i8*, i8** %3
%31 = call i32 @syslog(i32 %29, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = load i32*, i32** %4
%33 = call i32 @fclose(i32* %32)
store i32 0, i32* %2
br label %63
34: ; preds = %21
%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.3, i64 0, i64 0), i32 %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %46, label %40
40: ; preds = %34
%41 = load i32, i32* @LOG_ERR
%42 = load i8*, i8** %3
%43 = call i32 @syslog(i32 %41, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %42)
%44 = load i32*, i32** %4
%45 = call i32 @fclose(i32* %44)
store i32 0, i32* %2
br label %63
46: ; preds = %34
%47 = load i32*, i32** %4
%48 = call i32 @fflush(i32* %47)
%49 = load i32, i32* %5
%50 = load i32, i32* @LOCK_UN
%51 = call i32 @flock(i32 %49, i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %59
53: ; preds = %46
%54 = load i32, i32* @LOG_ERR
%55 = load i8*, i8** %3
%56 = call i32 @syslog(i32 %54, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %55)
%57 = load i32*, i32** %4
%58 = call i32 @fclose(i32* %57)
store i32 0, i32* %2
br label %63
59: ; preds = %46
%60 = load i32*, i32** %4
%61 = call i32 @fclose(i32* %60)
%62 = load i32, i32* %6
store i32 %62, i32* %2
br label %63
63: ; preds = %59, %53, %40, %28, %17
%64 = load i32, i32* %2
ret i32 %64
}

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