#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__ pid_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_CREAT;
int O_NONBLOCK;
int O_WRONLY;
int OpenChatFellow();
int STDIN_FILENO;
int STDOUT_FILENO;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, ...);
scalar_t__ fork();
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  pid_t childPid = -1;
  int status;
  int fellow_desc = -1;
  int file_flags;
  int result = 0;
  char fellow_chr;
  char keyboard_chr;
  fellow_desc = OpenChatFellow();
  childPid = fork();
  if (childPid > 0) {
    file_flags = fcntl(STDOUT_FILENO, F_GETFL);
    file_flags = file_flags | O_NONBLOCK;
    fcntl(STDOUT_FILENO, F_SETFL, file_flags);
    do {
      result = read(STDIN_FILENO, &keyboard_chr, 1);
      if (result != -1)
        write(STDOUT_FILENO, &keyboard_chr, 1);
    } while (keyboard_chr != 'Q' && keyboard_chr != 'q');
    printf("\nWait for child process to terminate.\n");
    wait(&status);
    printf("\nChild terminated.\n");
    close(STDOUT_FILENO);
  } else if (childPid == 0) {
    int fd = -1;
    fd = open("/tmp/file.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
    if (fd == -1) {
      perror("Open");
      exit(EXIT_FAILURE);
    }
    do {
      result = read(fellow_desc, &fellow_chr, 1);
      if (result != -1) {
        write(STDOUT_FILENO, &fellow_chr, 1);
        write(fd, &fellow_chr, 1);
      }
    } while (fellow_chr != 'z');
    close(fd);
    close(fellow_desc);
    exit(EXIT_SUCCESS);
  } else
    perror("fork() error");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0xffffffffffffffff,-0x10(%rbp)
ff
movl   $0xffffffff,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   10a0 <OpenChatFellow@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    12da <main+0x12a>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2dbd(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    -0x22(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
je     1277 <main+0xc7>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    -0x22(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    127c <main+0xcc>
movsbl -0x22(%rbp),%ecx
xor    %eax,%eax
cmp    $0x51,%ecx
mov    %al,-0x29(%rbp)
je     129b <main+0xeb>
movsbl -0x22(%rbp),%eax
cmp    $0x71,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    123c <main+0x8c>
lea    0xd53(%rip),%rdi
call   1050 <printf@plt>
lea    -0x14(%rbp),%rdi
call   10c0 <wait@plt>
lea    0xd65(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
jmp    13cf <main+0x21f>
cmpq   $0x0,-0x10(%rbp)
jne    13be <main+0x20e>
movl   $0xffffffff,-0x28(%rbp)
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
mov    0x2c8c(%rip),%rax
or     (%rax),%esi
mov    0x2ca3(%rip),%rax
mov    (%rax),%edx
mov    0x2cc2(%rip),%rax
or     (%rax),%edx
lea    0xd24(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1343 <main+0x193>
lea    0xd19(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c74(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    1348 <main+0x198>
mov    -0x18(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
je     138e <main+0x1de>
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x28(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    1393 <main+0x1e3>
movsbl -0x21(%rbp),%eax
cmp    $0x7a,%eax
jne    1348 <main+0x198>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    0x2be9(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xc89(%rip),%rdi
call   1090 <perror@plt>
jmp    13cf <main+0x21f>
mov    0x2bca(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10a0 <OpenChatFellow@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
jle    1290 <main+0xe0>
mov    0x2e11(%rip),%r14
mov    (%r14),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2de7(%rip),%rcx
or     (%rcx),%eax
mov    (%r14),%edi
mov    0x2dc3(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2d89(%rip),%rbp
lea    0x2(%rsp),%rbx
jmp    122b <main+0x7b>
cs nopw 0x0(%rax,%rax,1)
movzbl 0x2(%rsp),%eax
and    $0xdf,%al
cmp    $0x51,%al
je     1252 <main+0xa2>
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     1220 <main+0x70>
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
jmp    1220 <main+0x70>
lea    0xdc7(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   10c0 <wait@plt>
lea    0xdd7(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%edi
call   1060 <close@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
je     12a0 <main+0xf0>
lea    0xd7a(%rip),%rdi
call   1090 <perror@plt>
jmp    127c <main+0xcc>
mov    0x2d51(%rip),%rax
mov    0x2cda(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2cef(%rip),%rax
mov    0x2d10(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xd35(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    12ef <main+0x13f>
lea    0xd32(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    %eax,%r14d
lea    0x3(%rsp),%rbp
mov    0x2cea(%rip),%r15
jmp    1307 <main+0x157>
cmpb   $0x7a,0x3(%rsp)
je     133d <main+0x18d>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     1300 <main+0x150>
mov    (%r15),%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
jmp    1300 <main+0x150>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2c4d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@STDIN_FILENO = global i32 0
@.str = [39 x i8] c"\0AWait for child process to terminate.\0A\00"
@.str.1 = [20 x i8] c"\0AChild terminated.\0A\00"
@.str.2 = [14 x i8] c"/tmp/file.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [5 x i8] c"Open\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.4 = [13 x i8] c"fork() error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca i32
store i32 0, i32* %1
store i64 -1, i64* %2
store i32 -1, i32* %4
store i32 0, i32* %6
%10 = call i32 (...) @OpenChatFellow()
store i32 %10, i32* %4
%11 = call i64 (...) @fork()
store i64 %11, i64* %2
%12 = load i64, i64* %2
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %50
14: ; preds = %0
%15 = load i32, i32* @STDOUT_FILENO
%16 = load i32, i32* @F_GETFL
%17 = call i32 (i32, i32, ...) @fcntl(i32 %15, i32 %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = load i32, i32* @O_NONBLOCK
%20 = or i32 %18, %19
store i32 %20, i32* %5
%21 = load i32, i32* @STDOUT_FILENO
%22 = load i32, i32* @F_SETFL
%23 = load i32, i32* %5
%24 = call i32 (i32, i32, ...) @fcntl(i32 %21, i32 %22, i32 %23)
br label %25
25: ; preds = %42, %14
%26 = load i32, i32* @STDIN_FILENO
%27 = call i32 @read(i32 %26, i8* %8, i32 1)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp ne i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i32, i32* @STDOUT_FILENO
%32 = call i32 @write(i32 %31, i8* %8, i32 1)
br label %33
33: ; preds = %30, %25
br label %34
34: ; preds = %33
%35 = load i8, i8* %8
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 81
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i8, i8* %8
%40 = sext i8 %39 to i32
%41 = icmp ne i32 %40, 113
br label %42
42: ; preds = %38, %34
%43 = phi i1 [ false, %34 ], [ %41, %38 ]
br i1 %43, label %25, label %44
44: ; preds = %42
%45 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%46 = call i32 @wait(i32* %3)
%47 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%48 = load i32, i32* @STDOUT_FILENO
%49 = call i32 @close(i32 %48)
br label %93
50: ; preds = %0
%51 = load i64, i64* %2
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %90
53: ; preds = %50
store i32 -1, i32* %9
%54 = load i32, i32* @O_CREAT
%55 = load i32, i32* @O_WRONLY
%56 = or i32 %54, %55
%57 = load i32, i32* @S_IRUSR
%58 = load i32, i32* @S_IWUSR
%59 = or i32 %57, %58
%60 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %56, i32 %59)
store i32 %60, i32* %9
%61 = load i32, i32* %9
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %67
63: ; preds = %53
%64 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
67: ; preds = %53
br label %68
68: ; preds = %79, %67
%69 = load i32, i32* %4
%70 = call i32 @read(i32 %69, i8* %7, i32 1)
store i32 %70, i32* %6
%71 = load i32, i32* %6
%72 = icmp ne i32 %71, -1
br i1 %72, label %73, label %78
73: ; preds = %68
%74 = load i32, i32* @STDOUT_FILENO
%75 = call i32 @write(i32 %74, i8* %7, i32 1)
%76 = load i32, i32* %9
%77 = call i32 @write(i32 %76, i8* %7, i32 1)
br label %78
78: ; preds = %73, %68
br label %79
79: ; preds = %78
%80 = load i8, i8* %7
%81 = sext i8 %80 to i32
%82 = icmp ne i32 %81, 122
br i1 %82, label %68, label %83
83: ; preds = %79
%84 = load i32, i32* %9
%85 = call i32 @close(i32 %84)
%86 = load i32, i32* %4
%87 = call i32 @close(i32 %86)
%88 = load i32, i32* @EXIT_SUCCESS
%89 = call i32 @exit(i32 %88)
unreachable
90: ; preds = %50
%91 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %92
92: ; preds = %90
br label %93
93: ; preds = %92, %44
%94 = load i32, i32* @EXIT_SUCCESS
ret i32 %94
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@STDIN_FILENO = global i32 0
@.str.2 = [14 x i8] c"/tmp/file.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [5 x i8] c"Open\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.4 = [13 x i8] c"fork() error\00"
@str = [38 x i8] c"\0AWait for child process to terminate.\00"
@str.5 = [19 x i8] c"\0AChild terminated.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i8
%4 = bitcast i32* %1 to i8*
%5 = tail call i32 (...) @OpenChatFellow()
%6 = tail call i64 (...) @fork()
%7 = icmp sgt i64 %6, 0
br i1 %7, label %8, label %34
8: ; preds = %0
%9 = load i32, i32* @STDOUT_FILENO
%10 = load i32, i32* @F_GETFL
%11 = tail call i32 (i32, i32, ...) @fcntl(i32 %9, i32 %10)
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %12, %11
%14 = load i32, i32* @STDOUT_FILENO
%15 = load i32, i32* @F_SETFL
%16 = tail call i32 (i32, i32, ...) @fcntl(i32 %14, i32 %15, i32 %13)
br label %17
17: ; preds = %24, %8
%18 = load i32, i32* @STDIN_FILENO
%19 = call i32 @read(i32 %18, i8* %3, i32 1)
%20 = icmp eq i32 %19, -1
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = load i32, i32* @STDOUT_FILENO
%23 = call i32 @write(i32 %22, i8* %3, i32 1)
br label %24
24: ; preds = %17, %21
%25 = load i8, i8* %3
%26 = and i8 %25, -33
%27 = icmp eq i8 %26, 81
br i1 %27, label %28, label %17
28: ; preds = %24
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%30 = call i32 @wait(i32* %1)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%32 = load i32, i32* @STDOUT_FILENO
%33 = call i32 @close(i32 %32)
br label %66
34: ; preds = %0
%35 = icmp eq i64 %6, 0
br i1 %35, label %36, label %64
36: ; preds = %34
%37 = load i32, i32* @O_CREAT
%38 = load i32, i32* @O_WRONLY
%39 = or i32 %38, %37
%40 = load i32, i32* @S_IRUSR
%41 = load i32, i32* @S_IWUSR
%42 = or i32 %41, %40
%43 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %39, i32 %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %49
45: ; preds = %36
%46 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = tail call i32 @exit(i32 %47)
unreachable
49: ; preds = %36, %56
%50 = call i32 @read(i32 %5, i8* %2, i32 1)
%51 = icmp eq i32 %50, -1
br i1 %51, label %56, label %52
52: ; preds = %49
%53 = load i32, i32* @STDOUT_FILENO
%54 = call i32 @write(i32 %53, i8* %2, i32 1)
%55 = call i32 @write(i32 %43, i8* %2, i32 1)
br label %56
56: ; preds = %49, %52
%57 = load i8, i8* %2
%58 = icmp eq i8 %57, 122
br i1 %58, label %59, label %49
59: ; preds = %56
%60 = tail call i32 @close(i32 %43)
%61 = tail call i32 @close(i32 %5)
%62 = load i32, i32* @EXIT_SUCCESS
%63 = tail call i32 @exit(i32 %62)
unreachable
64: ; preds = %34
%65 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %66
66: ; preds = %64, %28
%67 = load i32, i32* @EXIT_SUCCESS
ret i32 %67
}