#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 EXIT_SUCCESS;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int OpenChatFellow();
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, ...);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int fellow_desc = 0;
  int file_flags;
  char fellow_chr;
  char keyboard_chr;
  int result = 0;
  fellow_desc = OpenChatFellow();
  file_flags = fcntl(fellow_desc, F_GETFL);
  file_flags = file_flags | O_NONBLOCK;
  fcntl(fellow_desc, F_SETFL, file_flags);
  file_flags = fcntl(STDOUT_FILENO, F_GETFL);
  file_flags = file_flags | O_NONBLOCK;
  fcntl(STDOUT_FILENO, F_SETFL, file_flags);
  do {
    result = read(fellow_desc, &fellow_chr, 1);
    if (result != -1)
      write(STDOUT_FILENO, &fellow_chr, 1);
    result = read(STDIN_FILENO, &keyboard_chr, 1);
    if (result != -1)
      write(STDOUT_FILENO, &keyboard_chr, 1);
  } while (keyboard_chr != 'Q');
  close(fellow_desc);
  close(STDOUT_FILENO);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1070 <OpenChatFellow@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e49(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e08(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     122e <main+0xde>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1269 <main+0x119>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    126e <main+0x11e>
movsbl -0xe(%rbp),%eax
cmp    $0x51,%eax
jne    11f9 <main+0xa9>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    0x2d28(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1070 <OpenChatFellow@plt>
mov    %eax,%r14d
mov    0x2e5b(%rip),%r12
mov    (%r12),%esi
mov    %eax,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2e5f(%rip),%rbp
or     0x0(%rbp),%eax
mov    0x2e45(%rip),%rbx
mov    (%rbx),%esi
mov    %r14d,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2e50(%rip),%r15
mov    (%r15),%edi
mov    (%r12),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
or     0x0(%rbp),%eax
mov    (%r15),%edi
mov    (%rbx),%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
lea    0xf(%rsp),%rbx
mov    0x2ded(%rip),%r12
lea    0xe(%rsp),%rbp
jmp    11e7 <main+0x97>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmpb   $0x51,0xe(%rsp)
je     1234 <main+0xe4>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     120c <main+0xbc>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    (%r12),%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     11e0 <main+0x90>
mov    (%r15),%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    11e0 <main+0x90>
mov    %r14d,%edi
call   1050 <close@plt>
mov    (%r15),%edi
call   1050 <close@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %6
%7 = call i32 (...) @OpenChatFellow()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = load i32, i32* @F_GETFL
%10 = call i32 (i32, i32, ...) @fcntl(i32 %8, i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %11, %12
store i32 %13, i32* %3
%14 = load i32, i32* %2
%15 = load i32, i32* @F_SETFL
%16 = load i32, i32* %3
%17 = call i32 (i32, i32, ...) @fcntl(i32 %14, i32 %15, i32 %16)
%18 = load i32, i32* @STDOUT_FILENO
%19 = load i32, i32* @F_GETFL
%20 = call i32 (i32, i32, ...) @fcntl(i32 %18, i32 %19)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = load i32, i32* @O_NONBLOCK
%23 = or i32 %21, %22
store i32 %23, i32* %3
%24 = load i32, i32* @STDOUT_FILENO
%25 = load i32, i32* @F_SETFL
%26 = load i32, i32* %3
%27 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %25, i32 %26)
br label %28
28: ; preds = %45, %0
%29 = load i32, i32* %2
%30 = call i32 @read(i32 %29, i8* %4, i32 1)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp ne i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* @STDOUT_FILENO
%35 = call i32 @write(i32 %34, i8* %4, i32 1)
br label %36
36: ; preds = %33, %28
%37 = load i32, i32* @STDIN_FILENO
%38 = call i32 @read(i32 %37, i8* %5, i32 1)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp ne i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* @STDOUT_FILENO
%43 = call i32 @write(i32 %42, i8* %5, i32 1)
br label %44
44: ; preds = %41, %36
br label %45
45: ; preds = %44
%46 = load i8, i8* %5
%47 = sext i8 %46 to i32
%48 = icmp ne i32 %47, 81
br i1 %48, label %28, label %49
49: ; preds = %45
%50 = load i32, i32* %2
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* @STDOUT_FILENO
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* @EXIT_SUCCESS
ret i32 %54
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i8
%2 = alloca i8
%3 = tail call i32 (...) @OpenChatFellow()
%4 = load i32, i32* @F_GETFL
%5 = tail call i32 (i32, i32, ...) @fcntl(i32 %3, i32 %4)
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %5
%8 = load i32, i32* @F_SETFL
%9 = tail call i32 (i32, i32, ...) @fcntl(i32 %3, i32 %8, i32 %7)
%10 = load i32, i32* @STDOUT_FILENO
%11 = load i32, i32* @F_GETFL
%12 = tail call i32 (i32, i32, ...) @fcntl(i32 %10, i32 %11)
%13 = load i32, i32* @O_NONBLOCK
%14 = or i32 %13, %12
%15 = load i32, i32* @STDOUT_FILENO
%16 = load i32, i32* @F_SETFL
%17 = tail call i32 (i32, i32, ...) @fcntl(i32 %15, i32 %16, i32 %14)
br label %18
18: ; preds = %31, %0
%19 = call i32 @read(i32 %3, i8* %1, i32 1)
%20 = icmp eq i32 %19, -1
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* @STDOUT_FILENO
%23 = call i32 @write(i32 %22, i8* %1, i32 1)
br label %24
24: ; preds = %21, %18
%25 = load i32, i32* @STDIN_FILENO
%26 = call i32 @read(i32 %25, i8* %2, i32 1)
%27 = icmp eq i32 %26, -1
br i1 %27, label %31, label %28
28: ; preds = %24
%29 = load i32, i32* @STDOUT_FILENO
%30 = call i32 @write(i32 %29, i8* %2, i32 1)
br label %31
31: ; preds = %24, %28
%32 = load i8, i8* %2
%33 = icmp eq i8 %32, 81
br i1 %33, label %34, label %18
34: ; preds = %31
%35 = tail call i32 @close(i32 %3)
%36 = load i32, i32* @STDOUT_FILENO
%37 = tail call i32 @close(i32 %36)
%38 = load i32, i32* @EXIT_SUCCESS
ret i32 %38
}