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

/* Variables and functions */
int /*<<< orphan*/ CRTSCTS;
int /*<<< orphan*/ TCSAFLUSH;
scalar_t__ cfsetispeed(struct termios *, int /*<<< orphan*/);
scalar_t__ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void set_tty_speed(int fd, speed_t new_ispeed, speed_t new_ospeed) {
  struct termios ttymodes;
  if (tcgetattr(fd, &ttymodes) < 0) {
    perror("tcgetattr");
    exit(1);
  }
  if (cfsetispeed(&ttymodes, new_ispeed) < 0) {
    perror("cfsetispeed");
    exit(1);
  }
  if (cfsetospeed(&ttymodes, new_ospeed) < 0) {
    perror("cfsetospeed");
    exit(1);
  }
  ttymodes.c_cflag |= CRTSCTS;
  if (tcsetattr(fd, TCSAFLUSH, &ttymodes) < 0) {
    perror("tcsetattr");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<set_tty_speed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <tcgetattr@plt>
cmp    $0x0,%rax
jge    119d <set_tty_speed+0x3d>
lea    0xe72(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%esi
lea    -0x10(%rbp),%rdi
call   1060 <cfsetispeed@plt>
cmp    $0x0,%rax
jge    11c9 <set_tty_speed+0x69>
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%esi
lea    -0x10(%rbp),%rdi
call   1030 <cfsetospeed@plt>
cmp    $0x0,%rax
jge    11f5 <set_tty_speed+0x95>
lea    0xe30(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1239 <set_tty_speed+0xd9>
lea    0xdf8(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_tty_speed>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   1040 <tcgetattr@plt>
test   %rax,%rax
js     11d1 <set_tty_speed+0x71>
lea    0x8(%rsp),%rdi
mov    %ebp,%esi
call   1060 <cfsetispeed@plt>
test   %rax,%rax
js     11da <set_tty_speed+0x7a>
lea    0x8(%rsp),%rdi
mov    %r14d,%esi
call   1030 <cfsetospeed@plt>
test   %rax,%rax
js     11e3 <set_tty_speed+0x83>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
or     %eax,0x8(%rsp)
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1050 <tcsetattr@plt>
test   %rax,%rax
js     11ec <set_tty_speed+0x8c>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe28(%rip),%rdi
jmp    11f3 <set_tty_speed+0x93>
lea    0xe29(%rip),%rdi
jmp    11f3 <set_tty_speed+0x93>
lea    0xe2c(%rip),%rdi
jmp    11f3 <set_tty_speed+0x93>
lea    0xe2f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [10 x i8] c"tcgetattr\00"
@.str.1 = [12 x i8] c"cfsetispeed\00"
@.str.2 = [12 x i8] c"cfsetospeed\00"
@CRTSCTS = global i32 0
@TCSAFLUSH = global i32 0
@.str.3 = [10 x i8] c"tcsetattr\00"
define void @set_tty_speed(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %4 to i8*
%6 = call i64 @tcgetattr(i32 %0, %struct.termios* %4)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %3
%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 = %3
%12 = call i64 @cfsetispeed(%struct.termios* %4, i32 %1)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %11
%18 = call i64 @cfsetospeed(%struct.termios* %4, i32 %2)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %17
%24 = load i32, i32* @CRTSCTS
%25 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%26 = load i32, i32* %25
%27 = or i32 %26, %24
store i32 %27, i32* %25
%28 = load i32, i32* @TCSAFLUSH
%29 = call i64 @tcsetattr(i32 %0, i32 %28, %struct.termios* %4)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %23
%32 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %23
ret void
}