#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 c_lflag;
};

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int main(int argc, char **argv) {
  struct termios org, new;
  tcgetattr(STDIN_FILENO, &org);
  new = org;
  new.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &new);
  int fd = open("/dev/stdin");
  char c;
  do {
    fprintf(stderr, "Type a character: ");
    read(fd, &c, 1);
    fprintf(stderr, "You typed '%c'.\n", c);
  } while (c != 'q');
  close(fd);
  tcsetattr(STDIN_FILENO, TCSANOW, &org);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    0x2e42(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <tcsetattr@plt>
lea    0xe35(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x24(%rbp),%edi
lea    -0x25(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
movsbl -0x25(%rbp),%edx
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movsbl -0x25(%rbp),%eax
cmp    $0x71,%eax
jne    11d3 <main+0x73>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <tcsetattr@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e53(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1060 <tcgetattr@plt>
mov    0x2e38(%rip),%rax
mov    0x2e51(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    %ecx,0x10(%rsp)
mov    (%rbx),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
call   1070 <tcsetattr@plt>
lea    0xe47(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
mov    0x2e09(%rip),%r13
lea    0xe3d(%rip),%r14
lea    0x7(%rsp),%r15
lea    0xe44(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    0x0(%r13),%edi
movsbl 0x7(%rsp),%ebp
mov    %r12,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
cmp    $0x71,%ebp
jne    11e0 <main+0x80>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1070 <tcsetattr@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@.str = [11 x i8] c"/dev/stdin\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Type a character: \00"
@.str.2 = [17 x i8] c"You typed '%c'.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.termios
%7 = alloca %struct.termios
%8 = alloca i32
%9 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @STDIN_FILENO
%11 = call i32 @tcgetattr(i32 %10, %struct.termios* %6)
%12 = bitcast %struct.termios* %7 to i8*
%13 = bitcast %struct.termios* %6 to i8*
%14 = load i32, i32* @ICANON
%15 = load i32, i32* @ECHO
%16 = or i32 %14, %15
%17 = xor i32 %16, -1
%18 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, %17
store i32 %20, i32* %18
%21 = load i32, i32* @STDIN_FILENO
%22 = load i32, i32* @TCSANOW
%23 = call i32 @tcsetattr(i32 %21, i32 %22, %struct.termios* %7)
%24 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 %24, i32* %8
br label %25
25: ; preds = %34, %2
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* %8
%29 = call i32 @read(i32 %28, i8* %9, i32 1)
%30 = load i32, i32* @stderr
%31 = load i8, i8* %9
%32 = sext i8 %31 to i32
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %25
%35 = load i8, i8* %9
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 113
br i1 %37, label %25, label %38
38: ; preds = %34
%39 = load i32, i32* %8
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* @STDIN_FILENO
%42 = load i32, i32* @TCSANOW
%43 = call i32 @tcsetattr(i32 %41, i32 %42, %struct.termios* %6)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@.str = [11 x i8] c"/dev/stdin\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Type a character: \00"
@.str.2 = [17 x i8] c"You typed '%c'.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = alloca i8
%6 = bitcast %struct.termios* %3 to i8*
%7 = bitcast %struct.termios* %4 to i8*
%8 = load i32, i32* @STDIN_FILENO
%9 = call i32 @tcgetattr(i32 %8, %struct.termios* %3)
%10 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%11 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%12 = load i32, i32* %10
%13 = load i32, i32* @ICANON
%14 = load i32, i32* @ECHO
%15 = or i32 %14, %13
%16 = xor i32 %15, -1
%17 = and i32 %12, %16
store i32 %17, i32* %11
%18 = load i32, i32* @STDIN_FILENO
%19 = load i32, i32* @TCSANOW
%20 = call i32 @tcsetattr(i32 %18, i32 %19, %struct.termios* %4)
%21 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %22, %2
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @read(i32 %21, i8* %5, i32 1)
%26 = load i32, i32* @stderr
%27 = load i8, i8* %5
%28 = sext i8 %27 to i32
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = icmp eq i8 %27, 113
br i1 %30, label %31, label %22
31: ; preds = %22
%32 = call i32 @close(i32 %21)
%33 = load i32, i32* @STDIN_FILENO
%34 = load i32, i32* @TCSANOW
%35 = call i32 @tcsetattr(i32 %33, i32 %34, %struct.termios* %3)
ret i32 0
}