#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 /*<<< 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*/ stderr;
char *strerror(int /*<<< orphan*/);
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 ? pidfile : "(null)");
    return 0;
  }
  pid = getpid();
  if (!fprintf(f, "%d\n", pid)) {
    printf("Can't write pid , %s.\n", strerror(errno));
    close(fd);
    return 0;
  }
  fflush(f);
  close(fd);
  return pid;
}


x86 assembly (clang, -O0):
<write_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     11d5 <write_pid+0x55>
mov    -0x1c(%rbp),%edi
lea    0xe3e(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    122d <write_pid+0xad>
mov    0x2df4(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11fb <write_pid+0x7b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    120b <write_pid+0x8b>
lea    0xe1b(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    120b <write_pid+0x8b>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xde9(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    129d <write_pid+0x11d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
cmp    $0x0,%eax
jne    1286 <write_pid+0x106>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xdbb(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    129d <write_pid+0x11d>
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    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbp
mov    0x2e4f(%rip),%rax
mov    0x2e60(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     11fa <write_pid+0x7a>
mov    %eax,%ebx
lea    0xe4c(%rip),%rsi
mov    %eax,%edi
call   1080 <fdopen@plt>
test   %rax,%rax
je     11fa <write_pid+0x7a>
mov    %rax,%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%r15d
lea    0xe4e(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %eax,%eax
je     1224 <write_pid+0xa4>
mov    %r14,%rdi
call   1070 <fflush@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r15d,%ebp
jmp    1248 <write_pid+0xc8>
mov    0x2dcf(%rip),%rax
movslq (%rax),%rdi
test   %rbp,%rbp
lea    0xe0f(%rip),%rdx
cmovne %rbp,%rdx
lea    0xdea(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1248 <write_pid+0xc8>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdef(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%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 = [7 x i8] c"(null)\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [23 x i8] c"Can't write pid , %s.\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 %29
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 = icmp ne i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i8*, i8** %3
br label %26
25: ; preds = %17
br label %26
26: ; preds = %25, %23
%27 = phi i8* [ %24, %23 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %25 ]
%28 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %27)
store i32 0, i32* %2
br label %47
29: ; preds = %13
%30 = call i32 (...) @getpid()
store i32 %30, i32* %6
%31 = load i32*, i32** %4
%32 = load i32, i32* %6
%33 = call i32 (i32*, i8*, ...) @fprintf(i32* %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %41, label %35
35: ; preds = %29
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %37)
%39 = load i32, i32* %5
%40 = call i32 @close(i32 %39)
store i32 0, i32* %2
br label %47
41: ; preds = %29
%42 = load i32*, i32** %4
%43 = call i32 @fflush(i32* %42)
%44 = load i32, i32* %5
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* %6
store i32 %46, i32* %2
br label %47
47: ; preds = %41, %35, %26
%48 = load i32, i32* %2
ret i32 %48
}

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