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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fprintf(int /*<<< orphan*/ *, char *, long);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int const, long);
scalar_t__ time(int /*<<< orphan*/ *);
int write_pid_to_file(const pid_t child_pid) {
  FILE *file;
  char filename[255];
  time_t current_time = time(NULL);
  snprintf(filename, 255, "%s/%d.%ld", ".", child_pid, (long)current_time);
  file = fopen(filename, "w+");
  if (file == NULL) {
    perror("Error opening status file");
    return 1;
  }
  if ((fprintf(file, "%ld", (long)child_pid)) < 1) {
    perror("Error printing pid to status file");
    return 1;
  }
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<write_pid_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %rax,-0x118(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%r8d
mov    -0x118(%rbp),%r9
mov    $0xff,%esi
lea    0xe64(%rip),%rdx
lea    0xe67(%rip),%rcx
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e2 <write_pid_to_file+0x82>
lea    0xe3e(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1227 <write_pid_to_file+0xc7>
mov    -0x10(%rbp),%rdi
movslq -0x8(%rbp),%rdx
lea    0xe15(%rip),%rsi
call   1050 <fprintf@plt>
cmp    $0x1,%eax
jge    1217 <write_pid_to_file+0xb7>
lea    0xe23(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1227 <write_pid_to_file+0xc7>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pid_to_file>:
push   %rbp
push   %rbx
sub    $0x108,%rsp
mov    %edi,%ebp
xor    %edi,%edi
call   1060 <time@plt>
lea    0xe87(%rip),%rdx
lea    0xe8a(%rip),%rcx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %ebp,%r8d
mov    %rax,%r9
call   1040 <snprintf@plt>
lea    0xe6f(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d7 <write_pid_to_file+0x77>
mov    %rax,%rbx
movslq %ebp,%rdx
lea    0xe4f(%rip),%rsi
mov    %rax,%rdi
call   1050 <fprintf@plt>
test   %eax,%eax
jle    11e0 <write_pid_to_file+0x80>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe31(%rip),%rdi
jmp    11e7 <write_pid_to_file+0x87>
lea    0xe42(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    11cd <write_pid_to_file+0x6d>

LLVM IR (-O0):
@.str = [10 x i8] c"%s/%d.%ld\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [26 x i8] c"Error opening status file\00"
@.str.4 = [4 x i8] c"%ld\00"
@.str.5 = [34 x i8] c"Error printing pid to status file\00"
define i32 @write_pid_to_file(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [255 x i8]
%6 = alloca i64
store i32 %0, i32* %3
%7 = call i64 @time(i32* null)
store i64 %7, i64* %6
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* %3
%10 = load i64, i64* %6
%11 = call i32 @snprintf(i8* %8, i32 255, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %9, i64 %10)
%12 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %13, i32** %4
%14 = load i32*, i32** %4
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %2
br label %29
18: ; preds = %1
%19 = load i32*, i32** %4
%20 = load i32, i32* %3
%21 = sext i32 %20 to i64
%22 = call i32 @fprintf(i32* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i64 %21)
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %2
br label %29
26: ; preds = %18
%27 = load i32*, i32** %4
%28 = call i32 @fclose(i32* %27)
store i32 0, i32* %2
br label %29
29: ; preds = %26, %24, %16
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@.str = [10 x i8] c"%s/%d.%ld\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [26 x i8] c"Error opening status file\00"
@.str.4 = [4 x i8] c"%ld\00"
@.str.5 = [34 x i8] c"Error printing pid to status file\00"
define i32 @write_pid_to_file(i32 %0) {
%2 = alloca [255 x i8]
%3 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%4 = tail call i64 @time(i32* null)
%5 = call i32 @snprintf(i8* %3, i32 255, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %0, i64 %4)
%6 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %18
10: ; preds = %1
%11 = sext i32 %0 to i64
%12 = call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i64 %11)
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
br label %18
16: ; preds = %10
%17 = call i32 @fclose(i32* %6)
br label %18
18: ; preds = %16, %14, %8
%19 = phi i32 [ 1, %8 ], [ 1, %14 ], [ 0, %16 ]
ret i32 %19
}