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

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ LOG_ERR;
int O_CREAT;
int O_NONBLOCK;
int O_TRUNC;
int O_WRONLY;
scalar_t__ errno;
scalar_t__ getpid();
int open(char *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned long);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int writepid(char *file) {
  int fd;
  char buf[20];
  fd = open(file, O_CREAT | O_WRONLY | O_TRUNC | O_NONBLOCK, 0644);
  if (fd < 0) {
    if (errno == EAGAIN)
      syslog(LOG_ERR, "%s: already locked", file);
    else
      syslog(LOG_ERR, "%s: %m", file);
    return -1;
  }
  snprintf(buf, sizeof(buf), "%lu\n", (unsigned long)getpid());
  if (write(fd, buf, strlen(buf)) < 0) {
    syslog(LOG_ERR, "%s: write: %m", file);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<writepid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
mov    0x2e67(%rip),%rax
or     (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1208 <writepid+0xa8>
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    0x2dec(%rip),%rcx
cmp    (%rcx),%rax
jne    11e3 <writepid+0x83>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe27(%rip),%rsi
call   1070 <syslog@plt>
jmp    11fc <writepid+0x9c>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1c(%rip),%rsi
call   1070 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <writepid+0x12a>
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rcx
mov    $0x14,%esi
lea    0xdf0(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1283 <writepid+0x123>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdad(%rip),%rsi
call   1070 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <writepid+0x12a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writepid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e87(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e6d(%rip),%rax
or     (%rax),%esi
mov    %rdi,%r14
mov    0x2e49(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
test   %eax,%eax
js     11ef <writepid+0x8f>
mov    %eax,%ebx
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe69(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x14,%esi
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    122b <writepid+0xcb>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
jmp    121e <writepid+0xbe>
mov    0x2dd2(%rip),%rax
mov    (%rax),%rax
mov    0x2da8(%rip),%rcx
mov    0x2da9(%rip),%rdx
mov    (%rdx),%edi
cmp    (%rcx),%rax
jne    1217 <writepid+0xb7>
lea    0xdeb(%rip),%rsi
jmp    121e <writepid+0xbe>
lea    0xdf5(%rip),%rsi
mov    %r14,%rdx
call   1070 <syslog@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@LOG_ERR = global i32 0
@.str = [19 x i8] c"%s: already locked\00"
@.str.1 = [7 x i8] c"%s: %m\00"
@.str.2 = [5 x i8] c"%lu\0A\00"
@.str.3 = [14 x i8] c"%s: write: %m\00"
define i32 @writepid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [20 x i8]
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_WRONLY
%9 = or i32 %7, %8
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %6, i32 %13, i32 420)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %30
17: ; preds = %1
%18 = load i64, i64* @errno
%19 = load i64, i64* @EAGAIN
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* @LOG_ERR
%23 = load i8*, i8** %3
%24 = call i32 @syslog(i32 %22, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %29
25: ; preds = %17
%26 = load i32, i32* @LOG_ERR
%27 = load i8*, i8** %3
%28 = call i32 @syslog(i32 %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %25, %21
store i32 -1, i32* %2
br label %45
30: ; preds = %1
%31 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%32 = call i64 (...) @getpid()
%33 = call i32 @snprintf(i8* %31, i32 20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %32)
%34 = load i32, i32* %4
%35 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%36 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = call i64 @write(i32 %34, i8* %35, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %30
%41 = load i32, i32* @LOG_ERR
%42 = load i8*, i8** %3
%43 = call i32 @syslog(i32 %41, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %42)
store i32 -1, i32* %2
br label %45
44: ; preds = %30
store i32 0, i32* %2
br label %45
45: ; preds = %44, %40, %29
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@LOG_ERR = global i32 0
@.str = [19 x i8] c"%s: already locked\00"
@.str.1 = [7 x i8] c"%s: %m\00"
@.str.2 = [5 x i8] c"%lu\0A\00"
@.str.3 = [14 x i8] c"%s: write: %m\00"
define i32 @writepid(i8* %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_WRONLY
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* %0, i32 %10, i32 420)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %1
%14 = load i64, i64* @errno
%15 = load i64, i64* @EAGAIN
%16 = icmp eq i64 %14, %15
%17 = load i32, i32* @LOG_ERR
br i1 %16, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @syslog(i32 %17, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %31
20: ; preds = %13
%21 = tail call i32 @syslog(i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %31
22: ; preds = %1
%23 = tail call i64 (...) @getpid()
%24 = call i32 @snprintf(i8* %3, i32 20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %23)
%25 = call i32 @strlen(i8* %3)
%26 = call i64 @write(i32 %11, i8* %3, i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i32, i32* @LOG_ERR
%30 = call i32 @syslog(i32 %29, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %31
31: ; preds = %22, %18, %20, %28
%32 = phi i32 [ -1, %28 ], [ -1, %20 ], [ -1, %18 ], [ 0, %22 ]
ret i32 %32
}