#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 S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
scalar_t__ chmod(char const *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
scalar_t__ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int write_pid(const char *file) {
  FILE *fp;
  fp = fopen(file, "w");
  if (fp == NULL) {
    return -1;
  } else if (chmod(file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) != 0) {
    fclose(fp);
    return -1;
  }
  fprintf(fp, "%d\n", getpid());
  if (fflush(fp) || ferror(fp)) {
    fclose(fp);
    return -1;
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<write_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a7 <write_pid+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <write_pid+0xf4>
mov    -0x10(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2e2d(%rip),%rax
or     (%rax),%esi
mov    0x2e14(%rip),%rax
or     (%rax),%esi
mov    0x2e03(%rip),%rax
or     (%rax),%esi
call   1080 <chmod@plt>
cmp    $0x0,%rax
je     11f3 <write_pid+0x83>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <write_pid+0xf4>
jmp    11f8 <write_pid+0x88>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%edx
lea    0xdee(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <fflush@plt>
cmp    $0x0,%rax
jne    123f <write_pid+0xcf>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1254 <write_pid+0xe4>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <write_pid+0xf4>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pid>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbp
lea    0xe83(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11fc <write_pid+0x8c>
mov    %rax,%rbx
mov    0x2e37(%rip),%rax
mov    0x2e50(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    0x2e24(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
call   1080 <chmod@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11f2 <write_pid+0x82>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe34(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1070 <fflush@plt>
test   %rax,%rax
jne    11f2 <write_pid+0x82>
mov    %rbx,%rdi
call   1030 <ferror@plt>
neg    %rax
sbb    %ebp,%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1201 <write_pid+0x91>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @write_pid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %42
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRGRP
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IROTH
%18 = or i32 %16, %17
%19 = call i64 @chmod(i8* %11, i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %10
%22 = load i32*, i32** %4
%23 = call i32 @fclose(i32* %22)
store i32 -1, i32* %2
br label %42
24: ; preds = %10
br label %25
25: ; preds = %24
%26 = load i32*, i32** %4
%27 = call i32 (...) @getpid()
%28 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = load i32*, i32** %4
%30 = call i64 @fflush(i32* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %25
%33 = load i32*, i32** %4
%34 = call i64 @ferror(i32* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %32, %25
%37 = load i32*, i32** %4
%38 = call i32 @fclose(i32* %37)
store i32 -1, i32* %2
br label %42
39: ; preds = %32
%40 = load i32*, i32** %4
%41 = call i32 @fclose(i32* %40)
store i32 0, i32* %2
br label %42
42: ; preds = %39, %36, %21, %9
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @write_pid(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %26, label %4
4: ; preds = %1
%5 = load i32, i32* @S_IRUSR
%6 = load i32, i32* @S_IWUSR
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IRGRP
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IROTH
%11 = or i32 %9, %10
%12 = tail call i64 @chmod(i8* %0, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %4
%15 = tail call i32 (...) @getpid()
%16 = tail call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = tail call i64 @fflush(i32* %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = tail call i64 @ferror(i32* %2)
%21 = icmp ne i64 %20, 0
%22 = sext i1 %21 to i32
br label %23
23: ; preds = %19, %14, %4
%24 = phi i32 [ -1, %4 ], [ -1, %14 ], [ %22, %19 ]
%25 = tail call i32 @fclose(i32* %2)
br label %26
26: ; preds = %23, %1
%27 = phi i32 [ -1, %1 ], [ %24, %23 ]
ret i32 %27
}