#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;
  int /*<<< orphan*/ c_cflag;
};
typedef int /*<<< orphan*/ speed_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCAL;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetspeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ get_speed(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void set_tty_mode(int fd, int speed) {
  struct termios ttymode;
  speed_t spd = get_speed(speed);
  if (tcgetattr(fd, &ttymode) < 0) {
    perror("tcgetattr");
    exit(1);
  }
  cfmakeraw(&ttymode);
  cfsetspeed(&ttymode, spd);
  ttymode.c_cflag |= CLOCAL;
  ttymode.c_cc[VMIN] = 1;
  ttymode.c_cc[VTIME] = 0;
  if (tcsetattr(fd, TCSANOW, &ttymode) < 0) {
    perror("tcsetattr");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<set_tty_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <get_speed@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0x0,%rax
jge    11b5 <set_tty_mode+0x45>
lea    0xe5a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x18(%rbp),%rdi
call   1050 <cfmakeraw@plt>
mov    -0x1c(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1040 <cfsetspeed@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dfc(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2dd7(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x4(%rbp),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1238 <set_tty_mode+0xc8>
lea    0xde1(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_tty_mode>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %esi,%edi
call   1030 <get_speed@plt>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %ebx,%edi
call   1060 <tcgetattr@plt>
test   %rax,%rax
js     11fb <set_tty_mode+0x8b>
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <cfmakeraw@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <cfsetspeed@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
or     %eax,0x8(%rsp)
mov    (%rsp),%rax
mov    0x2e21(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e00(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
test   %rax,%rax
js     1204 <set_tty_mode+0x94>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdfe(%rip),%rdi
jmp    120b <set_tty_mode+0x9b>
lea    0xdff(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32*, i32 }
@.str = [10 x i8] c"tcgetattr\00"
@CLOCAL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
define void @set_tty_mode(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.termios
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = call i32 @get_speed(i32 %7)
store i32 %8, i32* %6
%9 = load i32, i32* %3
%10 = call i64 @tcgetattr(i32 %9, %struct.termios* %5)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = call i32 @cfmakeraw(%struct.termios* %5)
%17 = load i32, i32* %6
%18 = call i32 @cfsetspeed(%struct.termios* %5, i32 %17)
%19 = load i32, i32* @CLOCAL
%20 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%21 = load i32, i32* %20
%22 = or i32 %21, %19
store i32 %22, i32* %20
%23 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%24 = load i32*, i32** %23
%25 = load i64, i64* @VMIN
%26 = getelementptr inbounds i32, i32* %24, i64 %25
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%28 = load i32*, i32** %27
%29 = load i64, i64* @VTIME
%30 = getelementptr inbounds i32, i32* %28, i64 %29
store i32 0, i32* %30
%31 = load i32, i32* %3
%32 = load i32, i32* @TCSANOW
%33 = call i64 @tcsetattr(i32 %31, i32 %32, %struct.termios* %5)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %15
%36 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %15
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32 }
@.str = [10 x i8] c"tcgetattr\00"
@CLOCAL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
define void @set_tty_mode(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = tail call i32 @get_speed(i32 %1)
%6 = call i64 @tcgetattr(i32 %0, %struct.termios* %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = call i32 @cfmakeraw(%struct.termios* %3)
%13 = call i32 @cfsetspeed(%struct.termios* %3, i32 %5)
%14 = load i32, i32* @CLOCAL
%15 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%16 = load i32, i32* %15
%17 = or i32 %16, %14
store i32 %17, i32* %15
%18 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%19 = load i32*, i32** %18
%20 = load i64, i64* @VMIN
%21 = getelementptr inbounds i32, i32* %19, i64 %20
store i32 1, i32* %21
%22 = load i64, i64* @VTIME
%23 = getelementptr inbounds i32, i32* %19, i64 %22
store i32 0, i32* %23
%24 = load i32, i32* @TCSANOW
%25 = call i64 @tcsetattr(i32 %0, i32 %24, %struct.termios* %3)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %11
%28 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %11
ret void
}