#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;
};

/* Variables and functions */
int /*<<< orphan*/ TCIOFLUSH;
int TCSAFLUSH;
int TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int tcsetattr(int, int, struct termios *);
void serial_speed(int fd, int speed) {
  struct termios term;
  tcgetattr(fd, &term);
  cfmakeraw(&term);
  cfsetospeed(&term, speed);
  cfsetispeed(&term, speed);
  if (tcsetattr(fd, TCSANOW | TCSAFLUSH, &term) == -1)
    err(1, "tcsetattr");
  tcflush(fd, TCIOFLUSH);
  return;
}


x86 assembly (clang, -O0):
<serial_speed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1070 <tcgetattr@plt>
lea    -0x10(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    -0x8(%rbp),%esi
lea    -0x10(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    -0x8(%rbp),%esi
lea    -0x10(%rbp),%rdi
call   1090 <cfsetispeed@plt>
mov    -0x4(%rbp),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2e2a(%rip),%rax
or     (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    11e3 <serial_speed+0x73>
mov    $0x1,%edi
lea    0xe22(%rip),%rsi
call   1050 <err@plt>
mov    -0x4(%rbp),%edi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_speed>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1070 <tcgetattr@plt>
mov    %rbx,%rdi
call   1060 <cfmakeraw@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <cfsetospeed@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1090 <cfsetispeed@plt>
mov    0x2e33(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14d,%edi
mov    %rbx,%rdx
call   1080 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    11d9 <serial_speed+0x69>
lea    0xe31(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1040 <tcflush@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@TCSANOW = global i32 0
@TCSAFLUSH = global i32 0
@.str = [10 x i8] c"tcsetattr\00"
@TCIOFLUSH = global i32 0
define void @serial_speed(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.termios
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
%7 = call i32 @tcgetattr(i32 %6, %struct.termios* %5)
%8 = call i32 @cfmakeraw(%struct.termios* %5)
%9 = load i32, i32* %4
%10 = call i32 @cfsetospeed(%struct.termios* %5, i32 %9)
%11 = load i32, i32* %4
%12 = call i32 @cfsetispeed(%struct.termios* %5, i32 %11)
%13 = load i32, i32* %3
%14 = load i32, i32* @TCSANOW
%15 = load i32, i32* @TCSAFLUSH
%16 = or i32 %14, %15
%17 = call i32 @tcsetattr(i32 %13, i32 %16, %struct.termios* %5)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = load i32, i32* %3
%23 = load i32, i32* @TCIOFLUSH
%24 = call i32 @tcflush(i32 %22, i32 %23)
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@TCSANOW = global i32 0
@TCSAFLUSH = global i32 0
@.str = [10 x i8] c"tcsetattr\00"
@TCIOFLUSH = global i32 0
define void @serial_speed(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = call i32 @tcgetattr(i32 %0, %struct.termios* %3)
%6 = call i32 @cfmakeraw(%struct.termios* %3)
%7 = call i32 @cfsetospeed(%struct.termios* %3, i32 %1)
%8 = call i32 @cfsetispeed(%struct.termios* %3, i32 %1)
%9 = load i32, i32* @TCSANOW
%10 = load i32, i32* @TCSAFLUSH
%11 = or i32 %10, %9
%12 = call i32 @tcsetattr(i32 %0, i32 %11, %struct.termios* %3)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i32, i32* @TCIOFLUSH
%18 = call i32 @tcflush(i32 %0, i32 %17)
ret void
}