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

/* Variables and functions */
int /*<<< orphan*/ B0;
int /*<<< orphan*/ CS8;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ IGNBRK;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfsetspeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ isatty(int);
int open(char const *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ wait_firmware(int);
int open_uart(const char *path, speed_t speed) {
  struct termios options = {0};
  int fd;
  fd = open(path, O_RDWR | O_NOCTTY);
  if (fd < 0)
    err(EXIT_FAILURE, "cannot open serial port");
  if (!isatty(fd))
    err(EXIT_FAILURE, "invalid serial port");
  if (speed != B0) {
    cfsetspeed(&options, speed);
    options.c_iflag |= IGNBRK;
    options.c_cflag |= CS8;
    if (tcsetattr(fd, TCSANOW, &options) < 0)
      err(EXIT_FAILURE, "cannot set tty attributes");
  }
  usleep(500);
  tcflush(fd, TCIOFLUSH);
  wait_firmware(fd);
  return fd;
}


x86 assembly (clang, -O0):
<open_uart>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    0x2dfd(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ec <open_uart+0x5c>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
call   1070 <err@plt>
mov    -0x1c(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    1212 <open_uart+0x82>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
call   1070 <err@plt>
mov    -0xc(%rbp),%eax
mov    0x2dac(%rip),%rcx
cmp    (%rcx),%eax
je     1287 <open_uart+0xf7>
mov    -0xc(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1060 <cfsetspeed@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1090 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1282 <open_uart+0xf2>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
call   1070 <err@plt>
jmp    1287 <open_uart+0xf7>
mov    $0x1f4,%edi
call   10b0 <usleep@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d1d(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x1c(%rbp),%edi
call   1080 <wait_firmware@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_uart>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
movl   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
mov    0x2e47(%rip),%rax
mov    0x2e18(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11cc <open_uart+0x4c>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
call   1060 <err@plt>
mov    %ebx,%edi
call   1030 <isatty@plt>
test   %eax,%eax
jne    11ec <open_uart+0x6c>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
call   1060 <err@plt>
mov    0x2dd5(%rip),%rax
cmp    %ebp,(%rax)
je     124a <open_uart+0xca>
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <cfsetspeed@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
or     %eax,0x4(%rsp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
or     %eax,(%rsp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1080 <tcsetattr@plt>
test   %rax,%rax
jns    124a <open_uart+0xca>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
call   1060 <err@plt>
mov    $0x1f4,%edi
call   10a0 <usleep@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    %ebx,%edi
call   1070 <wait_firmware@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [24 x i8] c"cannot open serial port\00"
@.str.1 = [20 x i8] c"invalid serial port\00"
@B0 = global i32 0
@IGNBRK = global i32 0
@CS8 = global i32 0
@TCSANOW = global i32 0
@.str.2 = [26 x i8] c"cannot set tty attributes\00"
@TCIOFLUSH = global i32 0
define i32 @open_uart(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.termios
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = bitcast %struct.termios* %5 to i8*
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %8, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @err(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %15, %2
%19 = load i32, i32* %6
%20 = call i32 @isatty(i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @err(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %22, %18
%26 = load i32, i32* %4
%27 = load i32, i32* @B0
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %48
29: ; preds = %25
%30 = load i32, i32* %4
%31 = call i32 @cfsetspeed(%struct.termios* %5, i32 %30)
%32 = load i32, i32* @IGNBRK
%33 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%34 = load i32, i32* %33
%35 = or i32 %34, %32
store i32 %35, i32* %33
%36 = load i32, i32* @CS8
%37 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%38 = load i32, i32* %37
%39 = or i32 %38, %36
store i32 %39, i32* %37
%40 = load i32, i32* %6
%41 = load i32, i32* @TCSANOW
%42 = call i64 @tcsetattr(i32 %40, i32 %41, %struct.termios* %5)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %29
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @err(i32 %45, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %44, %29
br label %48
48: ; preds = %47, %25
%49 = call i32 @usleep(i32 500)
%50 = load i32, i32* %6
%51 = load i32, i32* @TCIOFLUSH
%52 = call i32 @tcflush(i32 %50, i32 %51)
%53 = load i32, i32* %6
%54 = call i32 @wait_firmware(i32 %53)
%55 = load i32, i32* %6
ret i32 %55
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [24 x i8] c"cannot open serial port\00"
@.str.1 = [20 x i8] c"invalid serial port\00"
@B0 = global i32 0
@IGNBRK = global i32 0
@CS8 = global i32 0
@TCSANOW = global i32 0
@.str.2 = [26 x i8] c"cannot set tty attributes\00"
@TCIOFLUSH = global i32 0
define i32 @open_uart(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @err(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %10, %2
%14 = tail call i32 @isatty(i32 %8)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @err(i32 %17, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %16, %13
%20 = load i32, i32* @B0
%21 = icmp eq i32 %20, %1
br i1 %21, label %38, label %22
22: ; preds = %19
%23 = call i32 @cfsetspeed(%struct.termios* %3, i32 %1)
%24 = load i32, i32* @IGNBRK
%25 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%26 = load i32, i32* %25
%27 = or i32 %26, %24
store i32 %27, i32* %25
%28 = load i32, i32* @CS8
%29 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%30 = load i32, i32* %29
%31 = or i32 %30, %28
store i32 %31, i32* %29
%32 = load i32, i32* @TCSANOW
%33 = call i64 @tcsetattr(i32 %8, i32 %32, %struct.termios* %3)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %22
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @err(i32 %36, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %22, %35, %19
%39 = call i32 @usleep(i32 500)
%40 = load i32, i32* @TCIOFLUSH
%41 = call i32 @tcflush(i32 %8, i32 %40)
%42 = call i32 @wait_firmware(i32 %8)
ret i32 %8
}