#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_cflag;
  int *c_cc;
  int /*<<< orphan*/ c_lflag;
};
typedef int /*<<< orphan*/ cfg;

/* Variables and functions */
int /*<<< orphan*/ B115200;
int CLOCAL;
int CREAD;
int CS8;
int CSIZE;
int CSTOPB;
int /*<<< orphan*/ ICANON;
int PARENB;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCOFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ bzero(struct termios *, int);
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void init_tty(int fd) {
  struct termios cfg;
  bzero(&cfg, sizeof(cfg));
  cfmakeraw(&cfg);
  cfsetispeed(&cfg, B115200);
  cfsetospeed(&cfg, B115200);
  cfg.c_cflag |= CLOCAL | CREAD;
  cfg.c_cflag &= ~CSIZE;
  cfg.c_cflag |= CS8;
  cfg.c_cflag &= ~PARENB;
  cfg.c_cflag &= ~CSTOPB;
  cfg.c_lflag &= ~ICANON;
  cfg.c_cc[VMIN] = 1;
  cfg.c_cc[VTIME] = 0;
  tcflush(fd, TCIFLUSH);
  tcflush(fd, TCOFLUSH);
  if (tcsetattr(fd, TCSANOW, &cfg) != 0) {
    perror("tcsetattr() failed");
    exit(0);
  }
}


x86 assembly (clang, -O0):
<init_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x18,%esi
call   1090 <bzero@plt>
lea    -0x20(%rbp),%rdi
call   1050 <cfmakeraw@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdi
call   1070 <cfsetispeed@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    0x2dfa(%rip),%rcx
or     (%rcx),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d68(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d80(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2d43(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x4(%rbp),%edi
mov    0x2d3f(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x4(%rbp),%edi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x4(%rbp),%edi
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     12b3 <init_tty+0x133>
lea    0xd59(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_tty>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0x18,%esi
call   1090 <bzero@plt>
mov    %r14,%rdi
call   1050 <cfmakeraw@plt>
mov    0x2df5(%rip),%rbx
mov    (%rbx),%esi
mov    %r14,%rdi
call   1070 <cfsetispeed@plt>
mov    (%rbx),%esi
mov    %r14,%rdi
call   1030 <cfsetospeed@plt>
mov    0x2db2(%rip),%rax
mov    0x2e03(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     0x8(%rsp),%ecx
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ecx,%eax
mov    0x2ddf(%rip),%rcx
or     (%rcx),%eax
mov    0x2dee(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2d85(%rip),%rdx
or     (%rdx),%ecx
not    %ecx
and    %eax,%ecx
mov    %ecx,0x8(%rsp)
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2da0(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d67(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <tcflush@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <tcflush@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
test   %rax,%rax
jne    127c <init_tty+0xfc>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd7d(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i32 }
@B115200 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@ICANON = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCIFLUSH = global i32 0
@TCOFLUSH = global i32 0
@TCSANOW = global i32 0
@.str = [19 x i8] c"tcsetattr() failed\00"
define void @init_tty(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.termios
store i32 %0, i32* %2
%4 = call i32 @bzero(%struct.termios* %3, i32 24)
%5 = call i32 @cfmakeraw(%struct.termios* %3)
%6 = load i32, i32* @B115200
%7 = call i32 @cfsetispeed(%struct.termios* %3, i32 %6)
%8 = load i32, i32* @B115200
%9 = call i32 @cfsetospeed(%struct.termios* %3, i32 %8)
%10 = load i32, i32* @CLOCAL
%11 = load i32, i32* @CREAD
%12 = or i32 %10, %11
%13 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%14 = load i32, i32* %13
%15 = or i32 %14, %12
store i32 %15, i32* %13
%16 = load i32, i32* @CSIZE
%17 = xor i32 %16, -1
%18 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, %17
store i32 %20, i32* %18
%21 = load i32, i32* @CS8
%22 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%23 = load i32, i32* %22
%24 = or i32 %23, %21
store i32 %24, i32* %22
%25 = load i32, i32* @PARENB
%26 = xor i32 %25, -1
%27 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%28 = load i32, i32* %27
%29 = and i32 %28, %26
store i32 %29, i32* %27
%30 = load i32, i32* @CSTOPB
%31 = xor i32 %30, -1
%32 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%33 = load i32, i32* %32
%34 = and i32 %33, %31
store i32 %34, i32* %32
%35 = load i32, i32* @ICANON
%36 = xor i32 %35, -1
%37 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 2
%38 = load i32, i32* %37
%39 = and i32 %38, %36
store i32 %39, i32* %37
%40 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%41 = load i32*, i32** %40
%42 = load i64, i64* @VMIN
%43 = getelementptr inbounds i32, i32* %41, i64 %42
store i32 1, i32* %43
%44 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%45 = load i32*, i32** %44
%46 = load i64, i64* @VTIME
%47 = getelementptr inbounds i32, i32* %45, i64 %46
store i32 0, i32* %47
%48 = load i32, i32* %2
%49 = load i32, i32* @TCIFLUSH
%50 = call i32 @tcflush(i32 %48, i32 %49)
%51 = load i32, i32* %2
%52 = load i32, i32* @TCOFLUSH
%53 = call i32 @tcflush(i32 %51, i32 %52)
%54 = load i32, i32* %2
%55 = load i32, i32* @TCSANOW
%56 = call i64 @tcsetattr(i32 %54, i32 %55, %struct.termios* %3)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %1
%59 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%60 = call i32 @exit(i32 0)
unreachable
61: ; preds = %1
ret void
}

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