#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 /*<<< orphan*/ eerror(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, int);
int getpid();
int createpid(const char *filename) {
  FILE *file;
  if ((file = fopen(filename, "w")) == NULL) {
    eerror("Faile to open file");
    return -1;
  }
  if (fprintf(file, "%u", getpid()) < 0) {
    fclose(file);
    eerror("Faile to write in file");
    return -1;
  }
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<createpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1192 <createpid+0x42>
lea    0xe81(%rip),%rdi
call   1060 <eerror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <createpid+0x9e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%edx
lea    0xe67(%rip),%rsi
call   1050 <fprintf@plt>
cmp    $0x0,%rax
jge    11de <createpid+0x8e>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe4b(%rip),%rdi
call   1060 <eerror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <createpid+0x9e>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createpid>:
push   %rbp
push   %r14
push   %rbx
lea    0xea5(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     119b <createpid+0x4b>
mov    %rax,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe9d(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1050 <fprintf@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
jns    11ac <createpid+0x5c>
lea    0xe7f(%rip),%rdi
jmp    11a2 <createpid+0x52>
lea    0xe60(%rip),%rdi
call   1060 <eerror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [19 x i8] c"Faile to open file\00"
@.str.2 = [3 x i8] c"%u\00"
@.str.3 = [23 x i8] c"Faile to write in file\00"
define i32 @createpid(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 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @eerror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %22
10: ; preds = %1
%11 = load i32*, i32** %4
%12 = call i32 (...) @getpid()
%13 = call i64 @fprintf(i32* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i32*, i32** %4
%17 = call i32 @fclose(i32* %16)
%18 = call i32 @eerror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %22
19: ; preds = %10
%20 = load i32*, i32** %4
%21 = call i32 @fclose(i32* %20)
store i32 0, i32* %2
br label %22
22: ; preds = %19, %15, %8
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [19 x i8] c"Faile to open file\00"
@.str.2 = [3 x i8] c"%u\00"
@.str.3 = [23 x i8] c"Faile to write in file\00"
define i32 @createpid(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 %9, label %4
4: ; preds = %1
%5 = tail call i32 (...) @getpid()
%6 = tail call i64 @fprintf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %5)
%7 = icmp slt i64 %6, 0
%8 = tail call i32 @fclose(i32* %2)
br i1 %7, label %9, label %12
9: ; preds = %4, %1
%10 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), %1 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), %4 ]
%11 = tail call i32 @eerror(i8* %10)
br label %12
12: ; preds = %9, %4
%13 = phi i32 [ 0, %4 ], [ -1, %9 ]
ret i32 %13
}