#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*/ errStr;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int getpid();
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ rs_strerror_r(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int write_pid(char *pidfile) {
  FILE *f;
  int fd;
  int pid;
  if (((fd = open(pidfile, O_RDWR | O_CREAT, 0644)) == -1) ||
      ((f = fdopen(fd, "r+")) == NULL)) {
    fprintf(stderr, "Can't open or create %s.\n", pidfile);
    return 0;
  }
  pid = getpid();
  if (!fprintf(f, "%d\n", pid)) {
    char errStr[1024];
    rs_strerror_r(errno, errStr, sizeof(errStr));
    printf("Can't write pid , %s.\n", errStr);
    close(fd);
    return 0;
  }
  fflush(f);
  close(fd);
  return pid;
}


x86 assembly (clang, -O0):
<write_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    0x2e55(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     11d8 <write_pid+0x58>
mov    -0x1c(%rbp),%edi
lea    0xe3b(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1200 <write_pid+0x80>
mov    0x2df1(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1280 <write_pid+0x100>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
cmp    $0x0,%eax
jne    1269 <write_pid+0xe9>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <rs_strerror_r@plt>
lea    -0x420(%rbp),%rsi
lea    0xdd1(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1280 <write_pid+0x100>
mov    -0x18(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbp
mov    0x2e49(%rip),%rax
mov    0x2e5a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1202 <write_pid+0x82>
mov    %eax,%r15d
lea    0xe45(%rip),%rsi
mov    %eax,%edi
call   1090 <fdopen@plt>
test   %rax,%rax
je     1202 <write_pid+0x82>
mov    %rax,%rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
lea    0xe40(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
test   %eax,%eax
je     1222 <write_pid+0xa2>
mov    %rbx,%rdi
call   1080 <fflush@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %ebp,%r14d
jmp    1252 <write_pid+0xd2>
mov    0x2dc7(%rip),%rax
movslq (%rax),%rdi
lea    0xdf0(%rip),%rsi
xor    %r14d,%r14d
mov    %rbp,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1252 <write_pid+0xd2>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rbx
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <rs_strerror_r@plt>
lea    0xddf(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [3 x i8] c"r+\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"Can't open or create %s.\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@errno = global i32 0
@.str.3 = [23 x i8] c"Can't write pid , %s.\0A\00"
define i32 @write_pid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1024 x i8]
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %8, i32 %11, i32 420)
store i32 %12, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %18, label %14
14: ; preds = %1
%15 = load i32, i32* %5
%16 = call i32* @fdopen(i32 %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %4
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %24
18: ; preds = %14, %1
%19 = load i32, i32* @stderr
%20 = sext i32 %19 to i64
%21 = inttoptr i64 %20 to i32*
%22 = load i8*, i8** %3
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 0, i32* %2
br label %44
24: ; preds = %14
%25 = call i32 (...) @getpid()
store i32 %25, i32* %6
%26 = load i32*, i32** %4
%27 = load i32, i32* %6
%28 = call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %38, label %30
30: ; preds = %24
%31 = load i32, i32* @errno
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%33 = call i32 @rs_strerror_r(i32 %31, i8* %32, i32 1024)
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%35 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
store i32 0, i32* %2
br label %44
38: ; preds = %24
%39 = load i32*, i32** %4
%40 = call i32 @fflush(i32* %39)
%41 = load i32, i32* %5
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %6
store i32 %43, i32* %2
br label %44
44: ; preds = %38, %30, %18
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [3 x i8] c"r+\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"Can't open or create %s.\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@errno = global i32 0
@.str.3 = [23 x i8] c"Can't write pid , %s.\0A\00"
define i32 @write_pid(i8* %0) {
%2 = alloca [1024 x i8]
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* %0, i32 %5, i32 420)
%7 = icmp eq i32 %6, -1
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = tail call i32* @fdopen(i32 %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %8, %1
%12 = load i32, i32* @stderr
%13 = sext i32 %12 to i64
%14 = inttoptr i64 %13 to i32*
%15 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %29
16: ; preds = %8
%17 = tail call i32 (...) @getpid()
%18 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%22 = load i32, i32* @errno
%23 = call i32 @rs_strerror_r(i32 %22, i8* %21, i32 1024)
%24 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %21)
%25 = call i32 @close(i32 %6)
br label %29
26: ; preds = %16
%27 = tail call i32 @fflush(i32* %9)
%28 = tail call i32 @close(i32 %6)
br label %29
29: ; preds = %26, %20, %11
%30 = phi i32 [ 0, %11 ], [ %17, %26 ], [ 0, %20 ]
ret i32 %30
}