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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ makeLogfilename(char *, int, size_t, int);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ unlink(char *);
void dump_output(size_t job_id, int is_stderr) {
  char out_filename_buf[256];
  char buf[4096];
  FILE *dst, *out_stream = is_stderr ? stderr : stdout;
  size_t nread;
  makeLogfilename(out_filename_buf, sizeof(out_filename_buf), job_id,
                  is_stderr);
  dst = fopen(out_filename_buf, "r");
  if (dst) {
    while ((nread = fread(buf, 1, sizeof(buf), dst))) {
      fwrite(buf, 1, nread, out_stream);
      if (nread < sizeof(buf))
        break;
    }
    fclose(dst);
    fflush(out_stream);
    unlink(out_filename_buf);
  }
}


x86 assembly (clang, -O0):
<dump_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11a2 <dump_output+0x32>
mov    0x2e4d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x1130(%rbp)
jmp    11b3 <dump_output+0x43>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x1130(%rbp)
mov    -0x1130(%rbp),%rax
mov    %rax,-0x1120(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
mov    $0x100,%esi
call   1060 <makeLogfilename@plt>
lea    -0x110(%rbp),%rdi
lea    0xe19(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x1118(%rbp)
cmpq   $0x0,-0x1118(%rbp)
je     1292 <dump_output+0x122>
jmp    1206 <dump_output+0x96>
lea    -0x1110(%rbp),%rdi
mov    -0x1118(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %rax,-0x1128(%rbp)
cmp    $0x0,%rax
je     126e <dump_output+0xfe>
lea    -0x1110(%rbp),%rdi
mov    -0x1128(%rbp),%rdx
mov    -0x1120(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
cmpq   $0x1000,-0x1128(%rbp)
jae    1269 <dump_output+0xf9>
jmp    126e <dump_output+0xfe>
jmp    1206 <dump_output+0x96>
mov    -0x1118(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x1120(%rbp),%rdi
call   1070 <fflush@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
add    $0x1130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_output>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1108,%rsp
mov    %esi,%ecx
mov    %rdi,%rdx
test   %esi,%esi
jne    1190 <dump_output+0x20>
mov    0x2e4a(%rip),%rax
jmp    1197 <dump_output+0x27>
mov    0x2e49(%rip),%rax
mov    (%rax),%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1060 <makeLogfilename@plt>
lea    0xe4f(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1221 <dump_output+0xb1>
mov    %rax,%r12
lea    0x100(%rsp),%r15
nopl   0x0(%rax)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1209 <dump_output+0x99>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r14,%rcx
call   1090 <fwrite@plt>
cmp    $0xfff,%rbx
ja     11d0 <dump_output+0x60>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1070 <fflush@plt>
mov    %rsp,%rdi
call   1030 <unlink@plt>
add    $0x1108,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32* null
@stdout = global i32* null
@.str = [2 x i8] c"r\00"
define void @dump_output(i64 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca [4096 x i8]
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i64
store i64 %0, i64* %3
store i32 %1, i32* %4
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32*, i32** @stderr
br label %16
14: ; preds = %2
%15 = load i32*, i32** @stdout
br label %16
16: ; preds = %14, %12
%17 = phi i32* [ %13, %12 ], [ %15, %14 ]
store i32* %17, i32** %8
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%19 = load i64, i64* %3
%20 = load i32, i32* %4
%21 = call i32 @makeLogfilename(i8* %18, i32 256, i64 %19, i32 %20)
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %23, i32** %7
%24 = load i32*, i32** %7
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %48
26: ; preds = %16
br label %27
27: ; preds = %40, %26
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%29 = load i32*, i32** %7
%30 = call i64 @fread(i8* %28, i32 1, i32 4096, i32* %29)
store i64 %30, i64* %9
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %41
32: ; preds = %27
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%34 = load i64, i64* %9
%35 = load i32*, i32** %8
%36 = call i32 @fwrite(i8* %33, i32 1, i64 %34, i32* %35)
%37 = load i64, i64* %9
%38 = icmp ult i64 %37, 4096
br i1 %38, label %39, label %40
39: ; preds = %32
br label %41
40: ; preds = %32
br label %27
41: ; preds = %39, %27
%42 = load i32*, i32** %7
%43 = call i32 @fclose(i32* %42)
%44 = load i32*, i32** %8
%45 = call i32 @fflush(i32* %44)
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%47 = call i32 @unlink(i8* %46)
br label %48
48: ; preds = %41, %16
ret void
}

LLVM IR (-O2):
@stderr = global i32* null
@stdout = global i32* null
@.str = [2 x i8] c"r\00"
define void @dump_output(i64 %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %1, 0
%8 = load i32*, i32** @stderr
%9 = load i32*, i32** @stdout
%10 = select i1 %7, i32* %9, i32* %8
%11 = call i32 @makeLogfilename(i8* %5, i32 256, i64 %0, i32 %1)
%12 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %24, label %14
14: ; preds = %2, %17
%15 = call i64 @fread(i8* %6, i32 1, i32 4096, i32* %12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = call i32 @fwrite(i8* %6, i32 1, i64 %15, i32* %10)
%19 = icmp ult i64 %15, 4096
br i1 %19, label %20, label %14
20: ; preds = %17, %14
%21 = call i32 @fclose(i32* %12)
%22 = call i32 @fflush(i32* %10)
%23 = call i32 @unlink(i8* %5)
br label %24
24: ; preds = %20, %2
ret void
}