#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 */
struct termios {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ err_exit(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdin_setup(struct termios *);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int /*<<< orphan*/ write(int, int *, int);
void *input_thread(void *arg) {
  int fd, data, *_arg;
  struct termios old;
  _arg = arg;
  fd = *_arg;
  if (fd < 0)
    err_exit(-EXIT_FAILURE, "Invalid file descriptor: %i\n", fd);
  stdin_setup(&old);
  system("/bin/stty raw");
  while (!0) {
    fread(&data, 1, 1, stdin);
    data &= 0xFF;
    if (data == 0x3 || data == 0x4) {
      tcsetattr(STDIN_FILENO, TCSANOW, &old);
      system("/bin/stty cooked");
      exit(-EXIT_SUCCESS);
    }
    write(fd, &data, 1);
  }
  pthread_exit(NULL);
}


x86 assembly (clang, -O0):
<input_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11b1 <input_thread+0x41>
xor    %edi,%edi
mov    0x2e30(%rip),%rax
sub    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe54(%rip),%rsi
call   1070 <err_exit@plt>
lea    -0x20(%rbp),%rdi
call   1030 <stdin_setup@plt>
lea    0xe5c(%rip),%rdi
call   1060 <system@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%ecx
lea    -0x10(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    -0x10(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x3,-0x10(%rbp)
je     11fe <input_thread+0x8e>
cmpl   $0x4,-0x10(%rbp)
jne    1235 <input_thread+0xc5>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1080 <tcsetattr@plt>
lea    0xe0b(%rip),%rdi
call   1060 <system@plt>
xor    %edi,%edi
mov    0x2d9a(%rip),%rax
sub    (%rax),%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
jmp    11c6 <input_thread+0x56>

x86 assembly (clang, -O2):
<input_thread>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    (%rdi),%ebp
test   %ebp,%ebp
jns    1197 <input_thread+0x27>
mov    0x2e4b(%rip),%rax
xor    %edi,%edi
sub    (%rax),%edi
lea    0xe70(%rip),%rsi
mov    %ebp,%edx
call   1070 <err_exit@plt>
lea    0x8(%rsp),%rdi
call   1030 <stdin_setup@plt>
lea    0xe75(%rip),%rdi
call   1060 <system@plt>
mov    0x2e34(%rip),%r14
mov    (%r14),%ecx
lea    0x4(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
movzbl 0x4(%rsp),%eax
mov    %eax,0x4(%rsp)
add    $0xfffffffd,%eax
cmp    $0x2,%eax
jb     1225 <input_thread+0xb5>
lea    0x4(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    (%r14),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
movzbl 0x4(%rsp),%eax
mov    %eax,0x4(%rsp)
add    $0xfffffffd,%eax
cmp    $0x2,%eax
jae    11f0 <input_thread+0x80>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1080 <tcsetattr@plt>
lea    0xde3(%rip),%rdi
call   1060 <system@plt>
mov    0x2d74(%rip),%rax
xor    %edi,%edi
sub    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@EXIT_FAILURE = global i32 0
@.str = [29 x i8] c"Invalid file descriptor: %i\0A\00"
@.str.1 = [14 x i8] c"/bin/stty raw\00"
@stdin = global i32 0
@STDIN_FILENO = global i32 0
@TCSANOW = global i32 0
@.str.2 = [17 x i8] c"/bin/stty cooked\00"
@EXIT_SUCCESS = global i32 0
define i8* @input_thread(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.termios
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = bitcast i8* %7 to i32*
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = load i32, i32* %9
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @EXIT_FAILURE
%15 = sub nsw i32 0, %14
%16 = load i32, i32* %3
%17 = call i32 @err_exit(i32 %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %13, %1
%19 = call i32 @stdin_setup(%struct.termios* %6)
%20 = call i32 @system(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %18, %39
%22 = load i32, i32* @stdin
%23 = call i32 @fread(i32* %4, i32 1, i32 1, i32 %22)
%24 = load i32, i32* %4
%25 = and i32 %24, 255
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, 3
br i1 %27, label %31, label %28
28: ; preds = %21
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, 4
br i1 %30, label %31, label %39
31: ; preds = %28, %21
%32 = load i32, i32* @STDIN_FILENO
%33 = load i32, i32* @TCSANOW
%34 = call i32 @tcsetattr(i32 %32, i32 %33, %struct.termios* %6)
%35 = call i32 @system(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @EXIT_SUCCESS
%37 = sub nsw i32 0, %36
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %28
%40 = load i32, i32* %3
%41 = call i32 @write(i32 %40, i32* %4, i32 1)
br label %21
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@EXIT_FAILURE = global i32 0
@.str = [29 x i8] c"Invalid file descriptor: %i\0A\00"
@.str.1 = [14 x i8] c"/bin/stty raw\00"
@stdin = global i32 0
@STDIN_FILENO = global i32 0
@TCSANOW = global i32 0
@.str.2 = [17 x i8] c"/bin/stty cooked\00"
@EXIT_SUCCESS = global i32 0
define noalias i8* @input_thread(i8* nocapture %0) {
%2 = alloca i32
%3 = alloca %struct.termios
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.termios* %3 to i8*
%6 = bitcast i8* %0 to i32*
%7 = load i32, i32* %6
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @EXIT_FAILURE
%11 = sub nsw i32 0, %10
%12 = tail call i32 @err_exit(i32 %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %13
13: ; preds = %9, %1
%14 = call i32 @stdin_setup(%struct.termios* %3)
%15 = call i32 @system(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @stdin
%17 = call i32 @fread(i32* %2, i32 1, i32 1, i32 %16)
%18 = load i32, i32* %2
%19 = and i32 %18, 255
store i32 %19, i32* %2
%20 = add nsw i32 %19, -3
%21 = icmp ult i32 %20, 2
br i1 %21, label %22, label %30
22: ; preds = %30, %13
%23 = load i32, i32* @STDIN_FILENO
%24 = load i32, i32* @TCSANOW
%25 = call i32 @tcsetattr(i32 %23, i32 %24, %struct.termios* %3)
%26 = call i32 @system(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_SUCCESS
%28 = sub nsw i32 0, %27
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %13, %30
%31 = call i32 @write(i32 %7, i32* %2, i32 1)
%32 = load i32, i32* @stdin
%33 = call i32 @fread(i32* %2, i32 1, i32 1, i32 %32)
%34 = load i32, i32* %2
%35 = and i32 %34, 255
store i32 %35, i32* %2
%36 = add nsw i32 %35, -3
%37 = icmp ult i32 %36, 2
br i1 %37, label %22, label %30
}