#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 scalar_t__ pid_t;

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int O_WRONLY;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
scalar_t__ getpid();
int open(char *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, long);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int write(int, char *, int);
void pid_file_create(char *pid_file) {
  char pidbuf[16];
  pid_t pid = getpid();
  int fd, len;
  if ((fd = open(pid_file, O_WRONLY | O_CREAT | O_EXCL, 0666)) == -1) {
    fprintf(stderr, "Failed to create pid file %s: %s\n", pid_file,
            strerror(errno));
    _exit(0);
  }
  snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
  len = strlen(pidbuf);
  if (write(fd, pidbuf, len) != len) {
    fprintf(stderr, "Failed to write in pid file %s: %s\n", pid_file,
            strerror(errno));
  }
  close(fd);
}


x86 assembly (clang, -O0):
<pid_file_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    0x2e3d(%rip),%rax
or     (%rax),%esi
mov    0x2e0c(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    121b <pid_file_create+0x8b>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdf1(%rip),%rsi
call   1090 <fprintf@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
lea    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x10,%esi
lea    0xdf3(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1040 <write@plt>
cmp    -0x30(%rbp),%eax
je     1290 <pid_file_create+0x100>
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd9c(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_file_create>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   1050 <getpid@plt>
mov    0x2e0b(%rip),%rcx
mov    0x2e44(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %rax,%r15
mov    0x2e0e(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1b6,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     124b <pid_file_create+0xbb>
mov    %eax,%ebp
lea    0xe43(%rip),%rdx
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x10,%esi
mov    %r15,%rcx
call   1070 <snprintf@plt>
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %ebx,%eax
je     1237 <pid_file_create+0xa7>
mov    0x2dc5(%rip),%rax
mov    (%rax),%ebx
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdfd(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1090 <fprintf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d86(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd97(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1090 <fprintf@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"Failed to create pid file %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"%ld\0A\00"
@.str.2 = [36 x i8] c"Failed to write in pid file %s: %s\0A\00"
define void @pid_file_create(i8* %0) {
%2 = alloca i8*
%3 = alloca [16 x i8]
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = call i64 (...) @getpid()
store i64 %7, i64* %4
%8 = load i8*, i8** %2
%9 = load i32, i32* @O_WRONLY
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = load i32, i32* @O_EXCL
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %8, i32 %13, i32 438)
store i32 %14, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %23
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %2
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %20)
%22 = call i32 @_exit(i32 0)
unreachable
23: ; preds = %1
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%25 = load i64, i64* %4
%26 = call i32 @snprintf(i8* %24, i32 16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %25)
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%28 = call i32 @strlen(i8* %27)
store i32 %28, i32* %6
%29 = load i32, i32* %5
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%31 = load i32, i32* %6
%32 = call i32 @write(i32 %29, i8* %30, i32 %31)
%33 = load i32, i32* %6
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %41
35: ; preds = %23
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %2
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %37, i8* %39)
br label %41
41: ; preds = %35, %23
%42 = load i32, i32* %5
%43 = call i32 @close(i32 %42)
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"Failed to create pid file %s: %s\0A\00"
@errno = global i32 0
@.str.1 = [5 x i8] c"%ld\0A\00"
@.str.2 = [36 x i8] c"Failed to write in pid file %s: %s\0A\00"
define void @pid_file_create(i8* %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = tail call i64 (...) @getpid()
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_EXCL
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9, i32 438)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %18
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %15)
%17 = tail call i32 @_exit(i32 0)
unreachable
18: ; preds = %1
%19 = call i32 @snprintf(i8* %3, i32 16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %4)
%20 = call i32 @strlen(i8* %3)
%21 = call i32 @write(i32 %10, i8* %3, i32 %20)
%22 = icmp eq i32 %21, %20
br i1 %22, label %28, label %23
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %26)
br label %28
28: ; preds = %23, %18
%29 = call i32 @close(i32 %10)
ret void
}