#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*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetspeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ final(int, char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
struct termios serial_set_raw_speed(int tty, speed_t speed) {
  struct termios term, ret;
  if (tcgetattr(tty, &ret) < 0) {
    final(1, "Failed to get tty settings.");
  }
  term = ret;
  cfmakeraw(&term);
  cfsetspeed(&term, speed);
  tcflush(tty, TCIFLUSH);
  if (tcsetattr(tty, TCSANOW, &term) < 0) {
    final(1, "Failed to set tty settings.");
  }
  return ret;
}


x86 assembly (clang, -O0):
<serial_set_raw_speed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0x0,%rax
jge    1195 <serial_set_raw_speed+0x35>
mov    $0x1,%edi
lea    0xe70(%rip),%rsi
call   1050 <final@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    -0x10(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1040 <cfsetspeed@plt>
mov    -0xc(%rbp),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0xc(%rbp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%rax
jge    11f1 <serial_set_raw_speed+0x91>
mov    $0x1,%edi
lea    0xe30(%rip),%rsi
call   1050 <final@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_set_raw_speed>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
mov    %rsp,%rsi
call   1070 <tcgetattr@plt>
test   %rax,%rax
jns    118b <serial_set_raw_speed+0x2b>
lea    0xe7f(%rip),%rsi
mov    $0x1,%edi
call   1050 <final@plt>
mov    (%rsp),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <cfmakeraw@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1040 <cfsetspeed@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1080 <tcsetattr@plt>
test   %rax,%rax
jns    11e3 <serial_set_raw_speed+0x83>
lea    0xe43(%rip),%rsi
mov    $0x1,%edi
call   1050 <final@plt>
mov    (%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [28 x i8] c"Failed to get tty settings.\00"
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [28 x i8] c"Failed to set tty settings.\00"
define i32 @serial_set_raw_speed(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.termios
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = call i64 @tcgetattr(i32 %7, %struct.termios* %3)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @final(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = bitcast %struct.termios* %6 to i8*
%14 = bitcast %struct.termios* %3 to i8*
%15 = call i32 @cfmakeraw(%struct.termios* %6)
%16 = load i32, i32* %5
%17 = call i32 @cfsetspeed(%struct.termios* %6, i32 %16)
%18 = load i32, i32* %4
%19 = load i32, i32* @TCIFLUSH
%20 = call i32 @tcflush(i32 %18, i32 %19)
%21 = load i32, i32* %4
%22 = load i32, i32* @TCSANOW
%23 = call i64 @tcsetattr(i32 %21, i32 %22, %struct.termios* %6)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %12
%26 = call i32 @final(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %12
%28 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%29 = load i32, i32* %28
ret i32 %29
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [28 x i8] c"Failed to get tty settings.\00"
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [28 x i8] c"Failed to set tty settings.\00"
define i32 @serial_set_raw_speed(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %4 to i8*
%6 = call i64 @tcgetattr(i32 %0, %struct.termios* %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @final(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%12 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%13 = load i32, i32* %11
store i32 %13, i32* %12
%14 = call i32 @cfmakeraw(%struct.termios* %4)
%15 = call i32 @cfsetspeed(%struct.termios* %4, i32 %1)
%16 = load i32, i32* @TCIFLUSH
%17 = call i32 @tcflush(i32 %0, i32 %16)
%18 = load i32, i32* @TCSANOW
%19 = call i64 @tcsetattr(i32 %0, i32 %18, %struct.termios* %4)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %10
%22 = call i32 @final(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %10
%24 = load i32, i32* %11
ret i32 %24
}