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

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int S_IRGRP;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ assert(char const *const);
int /*<<< orphan*/ close(int);
int errno;
int getpid();
int open(char const *const, int, int);
int snprintf(char *, int, char *, int);
int write(int, char *, int);
int create_pidfile(const char *const pidfile) {
  int pidfd, len, rc = 0;
  char pidstr[8];
  assert(pidfile);
  pidfd = open(pidfile, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP);
  if (pidfd < 0)
    return errno;
  len = snprintf(pidstr, sizeof(pidstr), "%u", getpid());
  if (write(pidfd, pidstr, len) != len)
    rc = errno;
  close(pidfd);
  return rc;
}


x86 assembly (clang, -O0):
<create_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    0x2e68(%rip),%rax
or     (%rax),%esi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
mov    0x2e1e(%rip),%rax
or     (%rax),%edx
mov    0x2e1d(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d0 <create_pidfile+0x70>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    122b <create_pidfile+0xcb>
lea    -0x24(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%ecx
mov    $0x8,%esi
lea    0xe0f(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
cmp    -0x18(%rbp),%eax
je     121d <create_pidfile+0xbd>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_pidfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1070 <assert@plt>
mov    0x2e3a(%rip),%rax
mov    0x2e7b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e58(%rip),%rax
mov    0x2e31(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e2e(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11f2 <create_pidfile+0x92>
mov    %eax,%ebx
xor    %r15d,%r15d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe45(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x8,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     11e9 <create_pidfile+0x89>
mov    0x2dea(%rip),%rax
mov    (%rax),%r15d
mov    %ebx,%edi
call   1060 <close@plt>
jmp    11fc <create_pidfile+0x9c>
mov    0x2dd7(%rip),%rax
mov    (%rax),%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@errno = global i32 0
@.str = [3 x i8] c"%u\00"
define i32 @create_pidfile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [8 x i8]
store i8* %0, i8** %3
store i32 0, i32* %6
%8 = load i8*, i8** %3
%9 = call i32 @assert(i8* %8)
%10 = load i8*, i8** %3
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IWUSR
%15 = load i32, i32* @S_IRUSR
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRGRP
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %10, i32 %13, i32 %18)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %1
%23 = load i32, i32* @errno
store i32 %23, i32* %2
br label %40
24: ; preds = %1
%25 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%26 = call i32 (...) @getpid()
%27 = call i32 @snprintf(i8* %25, i32 8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %26)
store i32 %27, i32* %5
%28 = load i32, i32* %4
%29 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%30 = load i32, i32* %5
%31 = call i32 @write(i32 %28, i8* %29, i32 %30)
%32 = load i32, i32* %5
%33 = icmp ne i32 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %24
%35 = load i32, i32* @errno
store i32 %35, i32* %6
br label %36
36: ; preds = %34, %24
%37 = load i32, i32* %4
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %6
store i32 %39, i32* %2
br label %40
40: ; preds = %36, %22
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@errno = global i32 0
@.str = [3 x i8] c"%u\00"
define i32 @create_pidfile(i8* %0) {
%2 = alloca [8 x i8]
%3 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @assert(i8* %0)
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IWUSR
%9 = load i32, i32* @S_IRUSR
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* %0, i32 %7, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = load i32, i32* @errno
br label %25
17: ; preds = %1
%18 = tail call i32 (...) @getpid()
%19 = call i32 @snprintf(i8* %3, i32 8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = call i32 @write(i32 %13, i8* %3, i32 %19)
%21 = icmp eq i32 %20, %19
%22 = load i32, i32* @errno
%23 = select i1 %21, i32 0, i32 %22
%24 = call i32 @close(i32 %13)
br label %25
25: ; preds = %17, %15
%26 = phi i32 [ %16, %15 ], [ %23, %17 ]
ret i32 %26
}