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

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int getpid();
int open(char const *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void write_pidfile(const char *pidfile_path) {
  int pidfile_fd = open(pidfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (pidfile_fd < 0) {
    die("Unable to create pid file", errno);
  }
  char pid[30];
  snprintf(pid, 30, "%d\n", getpid());
  write(pidfile_fd, pid, strlen(pid));
  close(pidfile_fd);
}


x86 assembly (clang, -O0):
<write_pidfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    0x2e68(%rip),%rax
or     (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11c7 <write_pidfile+0x57>
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
lea    0xe3e(%rip),%rdi
call   1090 <die@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%ecx
mov    $0x1e,%esi
lea    0xe32(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0xc(%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>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pidfile>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e42(%rip),%rax
mov    0x2e73(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11b7 <write_pidfile+0x47>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
lea    0xe4e(%rip),%rdi
call   1090 <die@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe55(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x1e,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [26 x i8] c"Unable to create pid file\00"
@errno = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
define void @write_pidfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [30 x i8]
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %5, i32 %10, i32 420)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32, i32* @errno
%16 = call i32 @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %14, %1
%18 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%19 = call i32 (...) @getpid()
%20 = call i32 @snprintf(i8* %18, i32 30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %3
%22 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%23 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
%25 = call i32 @write(i32 %21, i8* %22, i32 %24)
%26 = load i32, i32* %3
%27 = call i32 @close(i32 %26)
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [26 x i8] c"Unable to create pid file\00"
@errno = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
define void @write_pidfile(i8* nocapture %0) {
%2 = alloca [30 x i8]
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* %0, i32 %7, i32 420)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = tail call i32 @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %10, %1
%14 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%15 = tail call i32 (...) @getpid()
%16 = call i32 @snprintf(i8* %14, i32 30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = call i32 @strlen(i8* %14)
%18 = call i32 @write(i32 %8, i8* %14, i32 %17)
%19 = call i32 @close(i32 %8)
ret void
}