#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;
  int c_cflag;
  int c_iflag;
  int *c_cc;
  scalar_t__ c_oflag;
};

/* Variables and functions */
int /*<<< orphan*/ B57600;
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int CSIZE;
int ECHO;
int ECHOE;
int ECHOK;
int ECHOKE;
int ICANON;
int IGNBRK;
int IGNPAR;
int PARENB;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ tcdrain(int);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void tty_setup(int fd) {
  struct termios t;
  memset(&t, 0, sizeof(struct termios));
  t.c_oflag = 0;
  t.c_lflag = 0;
  t.c_cflag &= ~(CSIZE | PARENB);
  t.c_cflag |= CS8 | CREAD;
  t.c_cflag |= CLOCAL;
  t.c_iflag = IGNBRK | IGNPAR;
  t.c_lflag &= ~ICANON;
  t.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE);
  t.c_cc[VMIN] = 0;
  t.c_cc[VTIME] = 10;
  t.c_cflag &= ~CRTSCTS;
  cfsetispeed(&t, B57600);
  cfsetospeed(&t, B57600);
  tcdrain(fd);
  if (tcsetattr(fd, TCSANOW, &t) < 0) {
    perror("tcssetattr");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<tty_setup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    0x2e3e(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    0x2e03(%rip),%rcx
or     (%rcx),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    0x2d8c(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    0x2dbd(%rip),%rcx
or     (%rcx),%eax
mov    0x2d74(%rip),%rcx
or     (%rcx),%eax
mov    0x2d73(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d7d(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2d30(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xa,(%rax,%rcx,4)
mov    0x2cff(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d35(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   1070 <cfsetispeed@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   1040 <cfsetospeed@plt>
mov    -0x4(%rbp),%edi
call   1030 <tcdrain@plt>
mov    -0x4(%rbp),%edi
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    12cd <tty_setup+0x15d>
lea    0xd42(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_setup>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
movq   $0x0,0x18(%rsp)
mov    0x2e24(%rip),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x4(%rsp),%ecx
mov    0x2e1c(%rip),%rax
mov    0x2e1d(%rip),%rdx
or     (%rax),%ecx
or     (%rdx),%ecx
mov    0x2dba(%rip),%rax
or     (%rax),%ecx
mov    0x2dd9(%rip),%rax
mov    0x2dc2(%rip),%rdx
mov    (%rdx),%edx
or     (%rax),%edx
mov    %ecx,0x4(%rsp)
mov    %edx,0x8(%rsp)
movl   $0x0,(%rsp)
mov    0x10(%rsp),%rax
mov    0x2dd3(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d9a(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xa,(%rax,%rcx,4)
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x4(%rsp)
mov    0x2d92(%rip),%rbx
mov    (%rbx),%esi
mov    %r14,%rdi
call   1070 <cfsetispeed@plt>
mov    (%rbx),%esi
mov    %r14,%rdi
call   1040 <cfsetospeed@plt>
mov    %ebp,%edi
call   1030 <tcdrain@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
test   %rax,%rax
js     1262 <tty_setup+0xf2>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd97(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32, i32*, i64 }
@CSIZE = global i32 0
@PARENB = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHOKE = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@CRTSCTS = global i32 0
@B57600 = global i32 0
@TCSANOW = global i32 0
@.str = [11 x i8] c"tcssetattr\00"
define void @tty_setup(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.termios
store i32 %0, i32* %2
%4 = call i32 @memset(%struct.termios* %3, i32 0, i32 32)
%5 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 4
store i64 0, i64* %5
%6 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
store i32 0, i32* %6
%7 = load i32, i32* @CSIZE
%8 = load i32, i32* @PARENB
%9 = or i32 %7, %8
%10 = xor i32 %9, -1
%11 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%12 = load i32, i32* %11
%13 = and i32 %12, %10
store i32 %13, i32* %11
%14 = load i32, i32* @CS8
%15 = load i32, i32* @CREAD
%16 = or i32 %14, %15
%17 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%18 = load i32, i32* %17
%19 = or i32 %18, %16
store i32 %19, i32* %17
%20 = load i32, i32* @CLOCAL
%21 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%22 = load i32, i32* %21
%23 = or i32 %22, %20
store i32 %23, i32* %21
%24 = load i32, i32* @IGNBRK
%25 = load i32, i32* @IGNPAR
%26 = or i32 %24, %25
%27 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* @ICANON
%29 = xor i32 %28, -1
%30 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%31 = load i32, i32* %30
%32 = and i32 %31, %29
store i32 %32, i32* %30
%33 = load i32, i32* @ECHO
%34 = load i32, i32* @ECHOE
%35 = or i32 %33, %34
%36 = load i32, i32* @ECHOK
%37 = or i32 %35, %36
%38 = load i32, i32* @ECHOKE
%39 = or i32 %37, %38
%40 = xor i32 %39, -1
%41 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%42 = load i32, i32* %41
%43 = and i32 %42, %40
store i32 %43, i32* %41
%44 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 3
%45 = load i32*, i32** %44
%46 = load i64, i64* @VMIN
%47 = getelementptr inbounds i32, i32* %45, i64 %46
store i32 0, i32* %47
%48 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 3
%49 = load i32*, i32** %48
%50 = load i64, i64* @VTIME
%51 = getelementptr inbounds i32, i32* %49, i64 %50
store i32 10, i32* %51
%52 = load i32, i32* @CRTSCTS
%53 = xor i32 %52, -1
%54 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%55 = load i32, i32* %54
%56 = and i32 %55, %53
store i32 %56, i32* %54
%57 = load i32, i32* @B57600
%58 = call i32 @cfsetispeed(%struct.termios* %3, i32 %57)
%59 = load i32, i32* @B57600
%60 = call i32 @cfsetospeed(%struct.termios* %3, i32 %59)
%61 = load i32, i32* %2
%62 = call i32 @tcdrain(i32 %61)
%63 = load i32, i32* %2
%64 = load i32, i32* @TCSANOW
%65 = call i64 @tcsetattr(i32 %63, i32 %64, %struct.termios* %3)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %1
%68 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %1
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32, i32*, i64 }
@CSIZE = global i32 0
@PARENB = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHOKE = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@CRTSCTS = global i32 0
@B57600 = global i32 0
@TCSANOW = global i32 0
@.str = [11 x i8] c"tcssetattr\00"
define void @tty_setup(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = call i32 @memset(%struct.termios* %2, i32 0, i32 32)
%5 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 4
store i64 0, i64* %5
%6 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%7 = load i32, i32* @CSIZE
%8 = load i32, i32* @PARENB
%9 = or i32 %8, %7
%10 = xor i32 %9, -1
%11 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%12 = load i32, i32* %11
%13 = and i32 %12, %10
%14 = load i32, i32* @CS8
%15 = load i32, i32* @CREAD
%16 = or i32 %14, %13
%17 = or i32 %16, %15
%18 = load i32, i32* @CLOCAL
%19 = or i32 %17, %18
store i32 %19, i32* %11
%20 = load i32, i32* @IGNBRK
%21 = load i32, i32* @IGNPAR
%22 = or i32 %21, %20
%23 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
store i32 %22, i32* %23
store i32 0, i32* %6
%24 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
%25 = load i32*, i32** %24
%26 = load i64, i64* @VMIN
%27 = getelementptr inbounds i32, i32* %25, i64 %26
store i32 0, i32* %27
%28 = load i64, i64* @VTIME
%29 = getelementptr inbounds i32, i32* %25, i64 %28
store i32 10, i32* %29
%30 = load i32, i32* @CRTSCTS
%31 = xor i32 %30, -1
%32 = load i32, i32* %11
%33 = and i32 %32, %31
store i32 %33, i32* %11
%34 = load i32, i32* @B57600
%35 = call i32 @cfsetispeed(%struct.termios* %2, i32 %34)
%36 = load i32, i32* @B57600
%37 = call i32 @cfsetospeed(%struct.termios* %2, i32 %36)
%38 = call i32 @tcdrain(i32 %0)
%39 = load i32, i32* @TCSANOW
%40 = call i64 @tcsetattr(i32 %0, i32 %39, %struct.termios* %2)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %1
%43 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %1
ret void
}