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

/* Variables and functions */
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int pid_write(char *filename, pid_t pid) {
  FILE *file;
  file = fopen(filename, "w");
  if (!file) {
    return -1;
  }
  (void)fprintf(file, "%d\n", pid);
  if (ferror(file) != 0) {
    (void)fclose(file);
    return -1;
  }
  if (fclose(file) != 0) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<pid_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117a <pid_write+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <pid_write+0x9b>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe7a(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11b5 <pid_write+0x75>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <pid_write+0x9b>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     11d4 <pid_write+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <pid_write+0x9b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
lea    0xeaf(%rip),%rsi
call   1060 <fopen@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1194 <pid_write+0x54>
mov    %rax,%rbx
lea    0xe97(%rip),%rsi
mov    %rax,%rdi
mov    %r14d,%edx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %rbp,%rbp
jne    1194 <pid_write+0x54>
neg    %rax
sbb    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @pid_write(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %12, label %11
11: ; preds = %2
store i32 -1, i32* %3
br label %28
12: ; preds = %2
%13 = load i32*, i32** %6
%14 = load i32, i32* %5
%15 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = load i32*, i32** %6
%17 = call i64 @ferror(i32* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i32*, i32** %6
%21 = call i64 @fclose(i32* %20)
store i32 -1, i32* %3
br label %28
22: ; preds = %12
%23 = load i32*, i32** %6
%24 = call i64 @fclose(i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 -1, i32* %3
br label %28
27: ; preds = %22
store i32 0, i32* %3
br label %28
28: ; preds = %27, %26, %19, %11
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @pid_write(i8* nocapture %0, i32 %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %13, label %5
5: ; preds = %2
%6 = tail call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%7 = tail call i64 @ferror(i32* %3)
%8 = icmp eq i64 %7, 0
%9 = tail call i64 @fclose(i32* %3)
br i1 %8, label %10, label %13
10: ; preds = %5
%11 = icmp ne i64 %9, 0
%12 = sext i1 %11 to i32
br label %13
13: ; preds = %5, %10, %2
%14 = phi i32 [ -1, %2 ], [ %12, %10 ], [ -1, %5 ]
ret i32 %14
}