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

/* Variables and functions */
int /*<<< orphan*/ CSTOPB;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ bot_serial_translate_baud(int);
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int open(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int bot_serial_open(const char *port, int baud, int blocking) {
  struct termios opts;
  int flags = O_RDWR | O_NOCTTY;
  if (!blocking)
    flags |= O_NONBLOCK;
  int fd = open(port, flags, 0);
  if (fd == -1)
    return -1;
  if (tcgetattr(fd, &opts)) {
    printf("*** %i\n", fd);
    perror("tcgetattr");
    return -1;
  }
  cfsetispeed(&opts, bot_serial_translate_baud(baud));
  cfsetospeed(&opts, bot_serial_translate_baud(baud));
  cfmakeraw(&opts);
  opts.c_cflag &= ~CSTOPB;
  if (tcsetattr(fd, TCSANOW, &opts)) {
    perror("tcsetattr");
    return -1;
  }
  tcflush(fd, TCIOFLUSH);
  return fd;
}


x86 assembly (clang, -O0):
<bot_serial_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    0x2e06(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11e0 <bot_serial_open+0x40>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1207 <bot_serial_open+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d3 <bot_serial_open+0x133>
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <tcgetattr@plt>
cmp    $0x0,%rax
je     1244 <bot_serial_open+0xa4>
mov    -0x28(%rbp),%esi
lea    0xdd9(%rip),%rdi
call   1050 <printf@plt>
lea    0xdd5(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d3 <bot_serial_open+0x133>
mov    -0x14(%rbp),%edi
call   1060 <bot_serial_translate_baud@plt>
mov    %eax,%esi
lea    -0x20(%rbp),%rdi
call   10b0 <cfsetispeed@plt>
mov    -0x14(%rbp),%edi
call   1060 <bot_serial_translate_baud@plt>
mov    %eax,%esi
lea    -0x20(%rbp),%rdi
call   1030 <cfsetospeed@plt>
lea    -0x20(%rbp),%rdi
call   1070 <cfmakeraw@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%edi
mov    0x2d49(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1090 <tcsetattr@plt>
cmp    $0x0,%rax
je     12bc <bot_serial_open+0x11c>
lea    0xd67(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d3 <bot_serial_open+0x133>
mov    -0x28(%rbp),%edi
mov    0x2cfa(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bot_serial_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    0x2e38(%rip),%rax
mov    0x2e11(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %eax,%eax
test   %edx,%edx
jne    11ca <bot_serial_open+0x2a>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
or     %eax,%esi
xor    %edx,%edx
call   10a0 <open@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     1255 <bot_serial_open+0xb5>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <tcgetattr@plt>
test   %rax,%rax
jne    1263 <bot_serial_open+0xc3>
mov    %ebx,%edi
call   1060 <bot_serial_translate_baud@plt>
mov    %rsp,%r15
mov    %r15,%rdi
mov    %eax,%esi
call   10b0 <cfsetispeed@plt>
mov    %ebx,%edi
call   1060 <bot_serial_translate_baud@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1030 <cfsetospeed@plt>
mov    %r15,%rdi
call   1070 <cfmakeraw@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,(%rsp)
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1090 <tcsetattr@plt>
test   %rax,%rax
jne    127f <bot_serial_open+0xdf>
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <tcflush@plt>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd96(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
lea    0xd90(%rip),%rdi
call   10c0 <perror@plt>
jmp    1255 <bot_serial_open+0xb5>
lea    0xd8c(%rip),%rdi
call   10c0 <perror@plt>
jmp    1255 <bot_serial_open+0xb5>

LLVM IR (-O0):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [8 x i8] c"*** %i\0A\00"
@.str.1 = [10 x i8] c"tcgetattr\00"
@CSTOPB = global i32 0
@TCSANOW = global i32 0
@.str.2 = [10 x i8] c"tcsetattr\00"
@TCIOFLUSH = global i32 0
define i32 @bot_serial_open(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.termios
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %11, %12
store i32 %13, i32* %9
%14 = load i32, i32* %7
%15 = icmp ne i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %3
%17 = load i32, i32* @O_NONBLOCK
%18 = load i32, i32* %9
%19 = or i32 %18, %17
store i32 %19, i32* %9
br label %20
20: ; preds = %16, %3
%21 = load i8*, i8** %5
%22 = load i32, i32* %9
%23 = call i32 @open(i8* %21, i32 %22, i32 0)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 -1, i32* %4
br label %59
27: ; preds = %20
%28 = load i32, i32* %10
%29 = call i64 @tcgetattr(i32 %28, %struct.termios* %8)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = load i32, i32* %10
%33 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %59
35: ; preds = %27
%36 = load i32, i32* %6
%37 = call i32 @bot_serial_translate_baud(i32 %36)
%38 = call i32 @cfsetispeed(%struct.termios* %8, i32 %37)
%39 = load i32, i32* %6
%40 = call i32 @bot_serial_translate_baud(i32 %39)
%41 = call i32 @cfsetospeed(%struct.termios* %8, i32 %40)
%42 = call i32 @cfmakeraw(%struct.termios* %8)
%43 = load i32, i32* @CSTOPB
%44 = xor i32 %43, -1
%45 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%46 = load i32, i32* %45
%47 = and i32 %46, %44
store i32 %47, i32* %45
%48 = load i32, i32* %10
%49 = load i32, i32* @TCSANOW
%50 = call i64 @tcsetattr(i32 %48, i32 %49, %struct.termios* %8)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %35
%53 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %59
54: ; preds = %35
%55 = load i32, i32* %10
%56 = load i32, i32* @TCIOFLUSH
%57 = call i32 @tcflush(i32 %55, i32 %56)
%58 = load i32, i32* %10
store i32 %58, i32* %4
br label %59
59: ; preds = %54, %52, %31, %26
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [8 x i8] c"*** %i\0A\00"
@.str.1 = [10 x i8] c"tcgetattr\00"
@CSTOPB = global i32 0
@TCSANOW = global i32 0
@.str.2 = [10 x i8] c"tcsetattr\00"
@TCIOFLUSH = global i32 0
define i32 @bot_serial_open(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %4 to i8*
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %7, %6
%9 = icmp eq i32 %2, 0
%10 = load i32, i32* @O_NONBLOCK
%11 = select i1 %9, i32 %10, i32 0
%12 = or i32 %8, %11
%13 = tail call i32 @open(i8* %0, i32 %12, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %40, label %15
15: ; preds = %3
%16 = call i64 @tcgetattr(i32 %13, %struct.termios* %4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %13)
%20 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %40
21: ; preds = %15
%22 = call i32 @bot_serial_translate_baud(i32 %1)
%23 = call i32 @cfsetispeed(%struct.termios* %4, i32 %22)
%24 = call i32 @bot_serial_translate_baud(i32 %1)
%25 = call i32 @cfsetospeed(%struct.termios* %4, i32 %24)
%26 = call i32 @cfmakeraw(%struct.termios* %4)
%27 = load i32, i32* @CSTOPB
%28 = xor i32 %27, -1
%29 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%30 = load i32, i32* %29
%31 = and i32 %30, %28
store i32 %31, i32* %29
%32 = load i32, i32* @TCSANOW
%33 = call i64 @tcsetattr(i32 %13, i32 %32, %struct.termios* %4)
%34 = icmp eq i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %21
%36 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %40
37: ; preds = %21
%38 = load i32, i32* @TCIOFLUSH
%39 = call i32 @tcflush(i32 %13, i32 %38)
br label %40
40: ; preds = %3, %37, %35, %18
%41 = phi i32 [ -1, %18 ], [ -1, %35 ], [ %13, %37 ], [ -1, %3 ]
ret i32 %41
}