#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdout;
int execute_child2(int *pipefd2, const char *filename) {
  (void)close(pipefd2[1]);
  char buffer[256];
  FILE *file;
  if (filename == NULL) {
    file = stdout;
  } else {
    file = fopen(filename, "w+");
    if (file == NULL) {
      (void)fprintf(stderr, "Error while executing fopen()!\n");
      return EXIT_FAILURE;
    }
  }
  int res;
  while ((res = read(pipefd2[0], buffer, 256)) != 0) {
    if (res < 0) {
      (void)fprintf(stderr, "Error while executing read()!\n");
      return EXIT_FAILURE;
    }
    if ((fwrite(buffer, sizeof(char), res, file)) == -1) {
      (void)fprintf(stderr, "Error while executing write()!\n");
      return EXIT_FAILURE;
    }
  }
  (void)close(pipefd2[0]);
  (void)fclose(file);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<execute_child2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11a0 <execute_child2+0x40>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x128(%rbp)
jmp    11f0 <execute_child2+0x90>
mov    -0x18(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    11eb <execute_child2+0x8b>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c3 <execute_child2+0x163>
jmp    11f0 <execute_child2+0x90>
jmp    11f5 <execute_child2+0x95>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x12c(%rbp)
cmp    $0x0,%eax
je     12a0 <execute_child2+0x140>
cmpl   $0x0,-0x12c(%rbp)
jge    124e <execute_child2+0xee>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c3 <execute_child2+0x163>
lea    -0x120(%rbp),%rdi
mov    -0x12c(%rbp),%edx
mov    -0x128(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
cmp    $0xffffffff,%eax
jne    129b <execute_child2+0x13b>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c3 <execute_child2+0x163>
jmp    11f5 <execute_child2+0x95>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x128(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2d0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute_child2>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x4(%rdi),%edi
call   1040 <close@plt>
test   %rbx,%rbx
je     11ab <execute_child2+0x4b>
lea    0xe7a(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11b5 <execute_child2+0x55>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
jmp    1228 <execute_child2+0xc8>
mov    0x2e1e(%rip),%rax
mov    (%rax),%r15
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
test   %eax,%eax
je     11ff <execute_child2+0x9f>
js     1218 <execute_child2+0xb8>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r15,%rcx
call   1080 <fwrite@plt>
cmp    $0xffffffff,%eax
jne    11c0 <execute_child2+0x60>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
jmp    1228 <execute_child2+0xc8>
mov    (%r14),%edi
call   1040 <close@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    0x2db2(%rip),%rax
jmp    1234 <execute_child2+0xd4>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdout = global i32* null
@.str = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"Error while executing fopen()!\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [31 x i8] c"Error while executing read()!\0A\00"
@.str.3 = [32 x i8] c"Error while executing write()!\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @execute_child2(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca i32*
%8 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
%9 = load i32*, i32** %4
%10 = getelementptr inbounds i32, i32* %9, i64 1
%11 = load i32, i32* %10
%12 = call i32 @close(i32 %11)
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load i32*, i32** @stdout
store i32* %16, i32** %7
br label %27
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %7
%20 = load i32*, i32** %7
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
store i32 %25, i32* %3
br label %61
26: ; preds = %17
br label %27
27: ; preds = %26, %15
br label %28
28: ; preds = %52, %27
%29 = load i32*, i32** %4
%30 = getelementptr inbounds i32, i32* %29, i64 0
%31 = load i32, i32* %30
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 256)
store i32 %33, i32* %8
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %53
35: ; preds = %28
%36 = load i32, i32* %8
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
store i32 %41, i32* %3
br label %61
42: ; preds = %35
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%44 = load i32, i32* %8
%45 = load i32*, i32** %7
%46 = call i32 @fwrite(i8* %43, i32 1, i32 %44, i32* %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %52
48: ; preds = %42
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
store i32 %51, i32* %3
br label %61
52: ; preds = %42
br label %28
53: ; preds = %28
%54 = load i32*, i32** %4
%55 = getelementptr inbounds i32, i32* %54, i64 0
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = load i32*, i32** %7
%59 = call i32 @fclose(i32* %58)
%60 = load i32, i32* @EXIT_SUCCESS
store i32 %60, i32* %3
br label %61
61: ; preds = %53, %48, %38, %22
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@stdout = global i32* null
@.str = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"Error while executing fopen()!\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [31 x i8] c"Error while executing read()!\0A\00"
@.str.3 = [32 x i8] c"Error while executing write()!\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @execute_child2(i32* nocapture %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds i32, i32* %0, i64 1
%5 = load i32, i32* %4
%6 = tail call i32 @close(i32 %5)
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = icmp eq i8* %1, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i32*, i32** @stdout
br label %17
11: ; preds = %2
%12 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %38
17: ; preds = %11, %9
%18 = phi i32* [ %10, %9 ], [ %12, %11 ]
br label %19
19: ; preds = %28, %17
%20 = load i32, i32* %0
%21 = call i32 @read(i32 %20, i8* %7, i32 256)
%22 = icmp eq i32 %21, 0
br i1 %22, label %34, label %23
23: ; preds = %19
%24 = icmp slt i32 %21, 0
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %38
28: ; preds = %23
%29 = call i32 @fwrite(i8* %7, i32 1, i32 %21, i32* %18)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %19
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %38
34: ; preds = %19
%35 = load i32, i32* %0
%36 = tail call i32 @close(i32 %35)
%37 = tail call i32 @fclose(i32* %18)
br label %38
38: ; preds = %25, %31, %34, %14
%39 = phi i32* [ @EXIT_FAILURE, %14 ], [ @EXIT_FAILURE, %25 ], [ @EXIT_FAILURE, %31 ], [ @EXIT_SUCCESS, %34 ]
%40 = load i32, i32* %39
ret i32 %40
}