#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 */

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit_error(char *);
int fork();
int input_timeout(int, int);
int open(char *, int, int);
int read(int, char *, int);
int /*<<< orphan*/ waitms(int);
int /*<<< orphan*/ write(int, char *, int);
int handle_output_buffer(int terminal_out, char *buffer) {
  int pid, terminal_buffer = open(buffer, O_WRONLY | O_CREAT, 0777);
  if (-1 == terminal_buffer)
    exit_error("creating buffer <id>_out failed");
  pid = fork();
  if (-1 == pid)
    exit_error("fork failed");
  if (0 != pid)
    return pid;
  while (-1 != input_timeout(terminal_out, 0)) {
    char buf;
    if (1 == input_timeout(terminal_out, 100)) {
      int res = read(terminal_out, &buf, 1);
      if (-1 == res)
        break;
      if (0 == res)
        waitms(100);
      else
        write(terminal_buffer, &buf, 1);
    }
  }
  close(terminal_buffer);
  _exit(1);
}


x86 assembly (clang, -O0):
<handle_output_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    0x2e45(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%eax
jne    11dc <handle_output_buffer+0x4c>
lea    0xe29(%rip),%rdi
call   1070 <exit_error@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    1200 <handle_output_buffer+0x70>
lea    0xe25(%rip),%rdi
call   1070 <exit_error@plt>
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
je     1214 <handle_output_buffer+0x84>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret
jmp    1219 <handle_output_buffer+0x89>
mov    -0x4(%rbp),%edi
xor    %esi,%esi
call   10a0 <input_timeout@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     12aa <handle_output_buffer+0x11a>
mov    -0x4(%rbp),%edi
mov    $0x64,%esi
call   10a0 <input_timeout@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    12a5 <handle_output_buffer+0x115>
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    %eax,-0x20(%rbp)
mov    $0xffffffff,%eax
cmp    -0x20(%rbp),%eax
jne    1275 <handle_output_buffer+0xe5>
jmp    12aa <handle_output_buffer+0x11a>
xor    %eax,%eax
cmp    -0x20(%rbp),%eax
jne    128f <handle_output_buffer+0xff>
mov    $0x64,%edi
call   1040 <waitms@plt>
jmp    12a0 <handle_output_buffer+0x110>
mov    -0x18(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
jmp    12a5 <handle_output_buffer+0x115>
jmp    1219 <handle_output_buffer+0x89>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

x86 assembly (clang, -O2):
<handle_output_buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rax
mov    %edi,%r15d
mov    0x2e2c(%rip),%rcx
mov    0x2e4d(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %rax,%rdi
mov    $0x1ff,%edx
call   1090 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11cf <handle_output_buffer+0x3f>
lea    0xe36(%rip),%rdi
call   1070 <exit_error@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1201 <handle_output_buffer+0x71>
test   %ebx,%ebx
jne    120d <handle_output_buffer+0x7d>
mov    %r15d,%edi
xor    %esi,%esi
call   10a0 <input_timeout@plt>
cmp    $0xffffffff,%eax
jne    121a <handle_output_buffer+0x8a>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
lea    0xe18(%rip),%rdi
call   1070 <exit_error@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x7(%rsp),%r14
jmp    123a <handle_output_buffer+0xaa>
mov    $0x64,%edi
call   1040 <waitms@plt>
mov    %r15d,%edi
xor    %esi,%esi
call   10a0 <input_timeout@plt>
cmp    $0xffffffff,%eax
je     11f0 <handle_output_buffer+0x60>
mov    %r15d,%edi
mov    $0x64,%esi
call   10a0 <input_timeout@plt>
cmp    $0x1,%eax
jne    122b <handle_output_buffer+0x9b>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %eax,%eax
je     1221 <handle_output_buffer+0x91>
cmp    $0xffffffff,%eax
je     11f0 <handle_output_buffer+0x60>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
jmp    122b <handle_output_buffer+0x9b>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str = [32 x i8] c"creating buffer <id>_out failed\00"
@.str.1 = [12 x i8] c"fork failed\00"
define i32 @handle_output_buffer(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %9, i32 %12, i32 511)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 -1, %14
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @exit_error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = call i32 (...) @fork()
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp eq i32 -1, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @exit_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = load i32, i32* %5
%26 = icmp ne i32 0, %25
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = load i32, i32* %5
ret i32 %28
29: ; preds = %24
br label %30
30: ; preds = %53, %29
%31 = load i32, i32* %3
%32 = call i32 @input_timeout(i32 %31, i32 0)
%33 = icmp ne i32 -1, %32
br i1 %33, label %34, label %54
34: ; preds = %30
%35 = load i32, i32* %3
%36 = call i32 @input_timeout(i32 %35, i32 100)
%37 = icmp eq i32 1, %36
br i1 %37, label %38, label %53
38: ; preds = %34
%39 = load i32, i32* %3
%40 = call i32 @read(i32 %39, i8* %7, i32 1)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp eq i32 -1, %41
br i1 %42, label %43, label %44
43: ; preds = %38
br label %54
44: ; preds = %38
%45 = load i32, i32* %8
%46 = icmp eq i32 0, %45
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 @waitms(i32 100)
br label %52
49: ; preds = %44
%50 = load i32, i32* %6
%51 = call i32 @write(i32 %50, i8* %7, i32 1)
br label %52
52: ; preds = %49, %47
br label %53
53: ; preds = %52, %34
br label %30
54: ; preds = %43, %30
%55 = load i32, i32* %6
%56 = call i32 @close(i32 %55)
%57 = call i32 @_exit(i32 1)
unreachable
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str = [32 x i8] c"creating buffer <id>_out failed\00"
@.str.1 = [12 x i8] c"fork failed\00"
define i32 @handle_output_buffer(i32 %0, i8* nocapture %1) {
%3 = alloca i8
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %1, i32 %6, i32 511)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @exit_error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = tail call i32 (...) @fork()
switch i32 %12, label %18 [
i32 -1, label %16
i32 0, label %13
]
13: ; preds = %11
%14 = tail call i32 @input_timeout(i32 %0, i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %32, label %19
16: ; preds = %11
%17 = tail call i32 @exit_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
ret i32 %12
19: ; preds = %13, %29
%20 = tail call i32 @input_timeout(i32 %0, i32 100)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = call i32 @read(i32 %0, i8* %3, i32 1)
switch i32 %23, label %27 [
i32 -1, label %24
i32 0, label %25
]
24: ; preds = %22
br label %32
25: ; preds = %22
%26 = tail call i32 @waitms(i32 100)
br label %29
27: ; preds = %22
%28 = call i32 @write(i32 %7, i8* %3, i32 1)
br label %29
29: ; preds = %27, %25, %19
%30 = tail call i32 @input_timeout(i32 %0, i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %19
32: ; preds = %29, %13, %24
%33 = tail call i32 @close(i32 %7)
%34 = tail call i32 @_exit(i32 1)
unreachable
}