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

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int 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 *, 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)) {
    printf("Can't write pid , %i.\n", errno);
    close(fd);
    return 0;
  }
  fflush(f);
  close(fd);
  return pid;
}


x86 assembly (clang, -O0):
<write_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    0x2e68(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     11c5 <write_pid+0x55>
mov    -0x1c(%rbp),%edi
lea    0xe4e(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11ed <write_pid+0x7d>
mov    0x2e04(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1255 <write_pid+0xe5>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
cmp    $0x0,%eax
jne    123e <write_pid+0xce>
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
lea    0xdfc(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1255 <write_pid+0xe5>
mov    -0x18(%rbp),%rdi
call   1070 <fflush@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbp
mov    0x2e5f(%rip),%rax
mov    0x2e70(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     11ea <write_pid+0x7a>
mov    %eax,%ebx
lea    0xe5c(%rip),%rsi
mov    %eax,%edi
call   1080 <fdopen@plt>
test   %rax,%rax
je     11ea <write_pid+0x7a>
mov    %rax,%r15
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
lea    0xe57(%rip),%rsi
mov    %r15,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %eax,%eax
je     120a <write_pid+0x9a>
mov    %r15,%rdi
call   1070 <fflush@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%r14d
jmp    1226 <write_pid+0xb6>
mov    0x2ddf(%rip),%rax
movslq (%rax),%rdi
lea    0xe08(%rip),%rsi
xor    %r14d,%r14d
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1226 <write_pid+0xb6>
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
lea    0xe07(%rip),%rdi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14d,%eax
add    $0x8,%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"
@.str.3 = [23 x i8] c"Can't write pid , %i.\0A\00"
@errno = global i32 0
define i32 @write_pid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %7, i32 %10, i32 420)
store i32 %11, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %17, label %13
13: ; preds = %1
%14 = load i32, i32* %5
%15 = call i32* @fdopen(i32 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %4
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %23
17: ; preds = %13, %1
%18 = load i32, i32* @stderr
%19 = sext i32 %18 to i64
%20 = inttoptr i64 %19 to i32*
%21 = load i8*, i8** %3
%22 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %21)
store i32 0, i32* %2
br label %40
23: ; preds = %13
%24 = call i32 (...) @getpid()
store i32 %24, i32* %6
%25 = load i32*, i32** %4
%26 = load i32, i32* %6
%27 = call i32 (i32*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %34, label %29
29: ; preds = %23
%30 = load i32, i32* @errno
%31 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %5
%33 = call i32 @close(i32 %32)
store i32 0, i32* %2
br label %40
34: ; preds = %23
%35 = load i32*, i32** %4
%36 = call i32 @fflush(i32* %35)
%37 = load i32, i32* %5
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %6
store i32 %39, i32* %2
br label %40
40: ; preds = %34, %29, %17
%41 = load i32, i32* %2
ret i32 %41
}

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