#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_cc;
  scalar_t__ c_lflag;
  scalar_t__ c_cflag;
  scalar_t__ c_iflag;
};

/* Variables and functions */
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ fcntl(int /*<<< orphan*/, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(struct termios *, struct termios *, int);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
struct termios *terminal_setup(void) {
  struct termios *original = (struct termios *)malloc(sizeof(struct termios));
  struct termios stdio;
  tcgetattr(STDOUT_FILENO, original);
  memcpy(&stdio, original, sizeof(struct termios));
  stdio.c_iflag = 0;
  stdio.c_cflag = 0;
  stdio.c_lflag = 0;
  stdio.c_cc[VMIN] = 1;
  stdio.c_cc[VTIME] = 0;
  tcsetattr(STDOUT_FILENO, TCSANOW, &stdio);
  tcsetattr(STDOUT_FILENO, TCSAFLUSH, &stdio);
  fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
  return original;
}


x86 assembly (clang, -O0):
<terminal_setup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x20,%edi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    -0x8(%rbp),%rsi
lea    -0x28(%rbp),%rdi
mov    $0x20,%edx
call   1040 <memcpy@plt>
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e1b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2dee(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<terminal_setup>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    $0x20,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    0x2e83(%rip),%r15
mov    (%r15),%edi
mov    %rax,%rsi
call   1060 <tcgetattr@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x20,%edx
call   1040 <memcpy@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
movq   $0x0,0x18(%rsp)
mov    (%rsp),%rax
mov    0x2e21(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2df8(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    (%r15),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
mov    (%r15),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i64, i64, i64 }
@STDOUT_FILENO = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@TCSAFLUSH = global i32 0
@STDIN_FILENO = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define %struct.termios* @terminal_setup() {
%1 = alloca %struct.termios*
%2 = alloca %struct.termios
%3 = call i64 @malloc(i32 32)
%4 = inttoptr i64 %3 to %struct.termios*
store %struct.termios* %4, %struct.termios** %1
%5 = load i32, i32* @STDOUT_FILENO
%6 = load %struct.termios*, %struct.termios** %1
%7 = call i32 @tcgetattr(i32 %5, %struct.termios* %6)
%8 = load %struct.termios*, %struct.termios** %1
%9 = call i32 @memcpy(%struct.termios* %2, %struct.termios* %8, i32 32)
%10 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 3
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 2
store i64 0, i64* %11
%12 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
store i64 0, i64* %12
%13 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
%14 = load i32*, i32** %13
%15 = load i64, i64* @VMIN
%16 = getelementptr inbounds i32, i32* %14, i64 %15
store i32 1, i32* %16
%17 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
%18 = load i32*, i32** %17
%19 = load i64, i64* @VTIME
%20 = getelementptr inbounds i32, i32* %18, i64 %19
store i32 0, i32* %20
%21 = load i32, i32* @STDOUT_FILENO
%22 = load i32, i32* @TCSANOW
%23 = call i32 @tcsetattr(i32 %21, i32 %22, %struct.termios* %2)
%24 = load i32, i32* @STDOUT_FILENO
%25 = load i32, i32* @TCSAFLUSH
%26 = call i32 @tcsetattr(i32 %24, i32 %25, %struct.termios* %2)
%27 = load i32, i32* @STDIN_FILENO
%28 = load i32, i32* @F_SETFL
%29 = load i32, i32* @O_NONBLOCK
%30 = call i32 @fcntl(i32 %27, i32 %28, i32 %29)
%31 = load %struct.termios*, %struct.termios** %1
ret %struct.termios* %31
}

LLVM IR (-O2):
%struct.termios = type { i32*, i64, i64, i64 }
@STDOUT_FILENO = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@TCSAFLUSH = global i32 0
@STDIN_FILENO = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define %struct.termios* @terminal_setup() {
%1 = alloca %struct.termios
%2 = tail call i64 @malloc(i32 32)
%3 = inttoptr i64 %2 to %struct.termios*
%4 = bitcast %struct.termios* %1 to i8*
%5 = load i32, i32* @STDOUT_FILENO
%6 = tail call i32 @tcgetattr(i32 %5, %struct.termios* %3)
%7 = call i32 @memcpy(%struct.termios* %1, %struct.termios* %3, i32 32)
%8 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%9 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%10 = bitcast i64* %8 to i8*
%11 = load i32*, i32** %9
%12 = load i64, i64* @VMIN
%13 = getelementptr inbounds i32, i32* %11, i64 %12
store i32 1, i32* %13
%14 = load i64, i64* @VTIME
%15 = getelementptr inbounds i32, i32* %11, i64 %14
store i32 0, i32* %15
%16 = load i32, i32* @STDOUT_FILENO
%17 = load i32, i32* @TCSANOW
%18 = call i32 @tcsetattr(i32 %16, i32 %17, %struct.termios* %1)
%19 = load i32, i32* @STDOUT_FILENO
%20 = load i32, i32* @TCSAFLUSH
%21 = call i32 @tcsetattr(i32 %19, i32 %20, %struct.termios* %1)
%22 = load i32, i32* @STDIN_FILENO
%23 = load i32, i32* @F_SETFL
%24 = load i32, i32* @O_NONBLOCK
%25 = call i32 @fcntl(i32 %22, i32 %23, i32 %24)
ret %struct.termios* %3
}