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

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int /*<<< orphan*/ S_IRWXU;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fchmod(int, int);
int getpid();
int /*<<< orphan*/ msg(char *, int /*<<< orphan*/);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char **, int /*<<< orphan*/);
void writePidFile(char *filename) {
  pid_t pid = getpid();
  int fd = open(filename, O_TRUNC | O_CREAT | O_WRONLY, S_IRWXU);
  char buf[6];
  snprintf(buf, 6, "%i", pid);
  if (fd >= 0) {
    write(fd, &buf, strlen(buf));
    fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    close(fd);
  } else
    msg("Warning! Writing pid_file failed (%s)!\n", strerror(errno));
}


x86 assembly (clang, -O0):
<writePidFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    0x2e3e(%rip),%rax
or     (%rax),%esi
mov    0x2dd5(%rip),%rax
or     (%rax),%esi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x10(%rbp)
lea    -0x16(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x6,%esi
lea    0xe17(%rip),%rdx
call   1060 <snprintf@plt>
cmpl   $0x0,-0x10(%rbp)
jl     1256 <writePidFile+0xc6>
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    -0x16(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x16(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%edi
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
mov    0x2da0(%rip),%rax
or     (%rax),%esi
mov    0x2d7f(%rip),%rax
or     (%rax),%esi
mov    0x2d6e(%rip),%rax
or     (%rax),%esi
call   1080 <fchmod@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
jmp    1272 <writePidFile+0xe2>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xd96(%rip),%rdi
call   10b0 <msg@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writePidFile>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
mov    0x2e4a(%rip),%rax
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2de0(%rip),%rax
or     (%rax),%esi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
lea    0xe2c(%rip),%rdx
lea    0x2(%rsp),%rdi
mov    $0x6,%esi
mov    %ebp,%ecx
call   1060 <snprintf@plt>
test   %ebx,%ebx
js     1236 <writePidFile+0xa6>
lea    0x2(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x2d9f(%rip),%rax
mov    0x2dc0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d9d(%rip),%rax
or     (%rax),%esi
mov    0x2d8c(%rip),%rax
or     (%rax),%esi
mov    %ebx,%edi
call   1080 <fchmod@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1252 <writePidFile+0xc2>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdb8(%rip),%rdi
mov    %eax,%esi
call   10b0 <msg@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRWXU = global i32 0
@.str = [3 x i8] c"%i\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [40 x i8] c"Warning! Writing pid_file failed (%s)!\0A\00"
@errno = global i32 0
define void @writePidFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [6 x i8]
store i8* %0, i8** %2
%6 = call i32 (...) @getpid()
store i32 %6, i32* %3
%7 = load i8*, i8** %2
%8 = load i32, i32* @O_TRUNC
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = load i32, i32* @O_WRONLY
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRWXU
%14 = call i32 @open(i8* %7, i32 %12, i32 %13)
store i32 %14, i32* %4
%15 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%16 = load i32, i32* %3
%17 = call i32 @snprintf(i8* %15, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %4
%19 = icmp sge i32 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %1
%21 = load i32, i32* %4
%22 = bitcast [6 x i8]* %5 to i8**
%23 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
%25 = call i32 @write(i32 %21, i8** %22, i32 %24)
%26 = load i32, i32* %4
%27 = load i32, i32* @S_IRUSR
%28 = load i32, i32* @S_IWUSR
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IRGRP
%31 = or i32 %29, %30
%32 = load i32, i32* @S_IROTH
%33 = or i32 %31, %32
%34 = call i32 @fchmod(i32 %26, i32 %33)
%35 = load i32, i32* %4
%36 = call i32 @close(i32 %35)
br label %41
37: ; preds = %1
%38 = load i32, i32* @errno
%39 = call i32 @strerror(i32 %38)
%40 = call i32 @msg(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %37, %20
ret void
}

LLVM IR (-O2):
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRWXU = global i32 0
@.str = [3 x i8] c"%i\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [40 x i8] c"Warning! Writing pid_file failed (%s)!\0A\00"
@errno = global i32 0
define void @writePidFile(i8* nocapture %0) {
%2 = alloca [6 x i8]
%3 = tail call i32 (...) @getpid()
%4 = load i32, i32* @O_TRUNC
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_WRONLY
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRWXU
%10 = tail call i32 @open(i8* %0, i32 %8, i32 %9)
%11 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 0
%12 = call i32 @snprintf(i8* %11, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %3)
%13 = icmp sgt i32 %10, -1
br i1 %13, label %14, label %27
14: ; preds = %1
%15 = bitcast [6 x i8]* %2 to i8**
%16 = call i32 @strlen(i8* %11)
%17 = call i32 @write(i32 %10, i8** %15, i32 %16)
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %19, %18
%21 = load i32, i32* @S_IRGRP
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IROTH
%24 = or i32 %22, %23
%25 = call i32 @fchmod(i32 %10, i32 %24)
%26 = call i32 @close(i32 %10)
br label %31
27: ; preds = %1
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 @msg(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %27, %14
ret void
}