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

/* Variables and functions */
int /*<<< orphan*/ TCSANOW;
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);
  }
  if (tcsetattr(fd, TCSANOW, &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    -0x4(%rbp),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
jge    122a <set_tty_speed+0xca>
lea    0xe07(%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     11c4 <set_tty_speed+0x64>
lea    0x8(%rsp),%rdi
mov    %ebp,%esi
call   1060 <cfsetispeed@plt>
test   %rax,%rax
js     11cd <set_tty_speed+0x6d>
lea    0x8(%rsp),%rdi
mov    %r14d,%esi
call   1030 <cfsetospeed@plt>
test   %rax,%rax
js     11d6 <set_tty_speed+0x76>
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1050 <tcsetattr@plt>
test   %rax,%rax
js     11df <set_tty_speed+0x7f>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe35(%rip),%rdi
jmp    11e6 <set_tty_speed+0x86>
lea    0xe36(%rip),%rdi
jmp    11e6 <set_tty_speed+0x86>
lea    0xe39(%rip),%rdi
jmp    11e6 <set_tty_speed+0x86>
lea    0xe3c(%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"
@TCSANOW = 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* %4
%30 = load i32, i32* @TCSANOW
%31 = call i64 @tcsetattr(i32 %29, i32 %30, %struct.termios* %7)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; 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"
@TCSANOW = 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* @TCSANOW
%25 = call i64 @tcsetattr(i32 %0, i32 %24, %struct.termios* %4)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %23
ret void
}