#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 c_cflag;
};
struct serial_struct {
  int custom_divisor;
  int baud_base;
  int /*<<< orphan*/ flags;
  scalar_t__ *reserved_char;
};

/* Variables and functions */
int /*<<< orphan*/ ASYNC_SPD_CUST;
int /*<<< orphan*/ ASYNC_SPD_MASK;
int /*<<< orphan*/ B38400;
int CLOCAL;
int CREAD;
int CRTSCTS;
int /*<<< orphan*/ F_SETFL;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCGSERIAL;
int /*<<< orphan*/ TIOCSSERIAL;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct serial_struct *);
int open(char const *, int);
int /*<<< orphan*/ printf(char *, int, int, float);
int rate_to_constant(int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int serial_open(const char *device, int rate) {
  struct termios options;
  struct serial_struct serinfo;
  int fd;
  int speed = 0;
  if ((fd = open(device, O_RDWR | O_NOCTTY)) == -1)
    return -1;
  speed = rate_to_constant(rate);
  if (speed == 0) {
    serinfo.reserved_char[0] = 0;
    if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
      return -1;
    serinfo.flags &= ~ASYNC_SPD_MASK;
    serinfo.flags |= ASYNC_SPD_CUST;
    serinfo.custom_divisor = (serinfo.baud_base + (rate / 2)) / rate;
    if (serinfo.custom_divisor < 1)
      serinfo.custom_divisor = 1;
    if (ioctl(fd, TIOCSSERIAL, &serinfo) < 0)
      return -1;
    if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
      return -1;
    if (serinfo.custom_divisor * rate != serinfo.baud_base) {
      printf("actual baudrate is %d / %d = %f", serinfo.baud_base,
             serinfo.custom_divisor,
             (float)serinfo.baud_base / serinfo.custom_divisor);
    }
  }
  fcntl(fd, F_SETFL, 0);
  tcgetattr(fd, &options);
  cfsetispeed(&options, speed ?: B38400);
  cfsetospeed(&options, speed ?: B38400);
  cfmakeraw(&options);
  options.c_cflag |= (CLOCAL | CREAD);
  options.c_cflag &= ~CRTSCTS;
  if (tcsetattr(fd, TCSANOW, &options) != 0)
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<serial_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    0x2de6(%rip),%rax
or     (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    11e9 <serial_open+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f7 <serial_open+0x257>
mov    -0x14(%rbp),%edi
call   1070 <rate_to_constant@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jne    1310 <serial_open+0x170>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x34(%rbp),%edi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1234 <serial_open+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f7 <serial_open+0x257>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
cltd
idivl  -0x14(%rbp)
mov    %eax,-0x30(%rbp)
cmpl   $0x1,-0x30(%rbp)
jge    1285 <serial_open+0xe5>
movl   $0x1,-0x30(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    12b0 <serial_open+0x110>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f7 <serial_open+0x257>
mov    -0x34(%rbp),%edi
mov    0x2ce6(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    12db <serial_open+0x13b>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f7 <serial_open+0x257>
mov    -0x30(%rbp),%eax
imul   -0x14(%rbp),%eax
cmp    -0x2c(%rbp),%eax
je     130b <serial_open+0x16b>
mov    -0x2c(%rbp),%esi
mov    -0x30(%rbp),%edx
cvtsi2ssl -0x2c(%rbp),%xmm0
cvtsi2ssl -0x30(%rbp),%xmm1
divss  %xmm1,%xmm0
lea    0xcfa(%rip),%rdi
call   1050 <printf@plt>
jmp    1310 <serial_open+0x170>
mov    -0x34(%rbp),%edi
mov    0x2c8e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0x34(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <tcgetattr@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x40(%rbp)
cmp    $0x0,%eax
je     1349 <serial_open+0x1a9>
mov    -0x40(%rbp),%eax
mov    %eax,-0x44(%rbp)
jmp    1355 <serial_open+0x1b5>
mov    0x2c30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   10c0 <cfsetispeed@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x48(%rbp)
cmp    $0x0,%eax
je     137b <serial_open+0x1db>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    1387 <serial_open+0x1e7>
mov    0x2bfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1040 <cfsetospeed@plt>
lea    -0x18(%rbp),%rdi
call   1080 <cfmakeraw@plt>
mov    0x2be5(%rip),%rax
mov    (%rax),%eax
mov    0x2c14(%rip),%rcx
or     (%rcx),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2bd5(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2c00(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   10a0 <tcsetattr@plt>
cmp    $0x0,%rax
je     13f1 <serial_open+0x251>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f7 <serial_open+0x257>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    0x2e57(%rip),%rax
mov    0x2e30(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1235 <serial_open+0xb5>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <rate_to_constant@plt>
mov    %eax,%ebp
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
lea    0x8(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1070 <tcgetattr@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   10a0 <cfsetispeed@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <cfsetospeed@plt>
mov    %r14,%rdi
call   1060 <cfmakeraw@plt>
mov    0x2daf(%rip),%rax
mov    0x2dd8(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     0x8(%rsp),%ecx
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ecx,%eax
mov    %eax,0x8(%rsp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1080 <tcsetattr@plt>
neg    %rax
sbb    %eax,%eax
or     %ebx,%eax
jmp    123a <serial_open+0xba>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.serial_struct = type { i32, i32, i32, i64* }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TIOCGSERIAL = global i32 0
@ASYNC_SPD_MASK = global i32 0
@ASYNC_SPD_CUST = global i32 0
@TIOCSSERIAL = global i32 0
@.str = [32 x i8] c"actual baudrate is %d / %d = %f\00"
@F_SETFL = global i32 0
@B38400 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@CRTSCTS = global i32 0
@TCSANOW = global i32 0
define i32 @serial_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.termios
%7 = alloca %struct.serial_struct
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %9
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %10, i32 %13)
store i32 %14, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %129
17: ; preds = %2
%18 = load i32, i32* %5
%19 = call i32 @rate_to_constant(i32 %18)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %88
22: ; preds = %17
%23 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 3
%24 = load i64*, i64** %23
%25 = getelementptr inbounds i64, i64* %24, i64 0
store i64 0, i64* %25
%26 = load i32, i32* %8
%27 = load i32, i32* @TIOCGSERIAL
%28 = call i64 @ioctl(i32 %26, i32 %27, %struct.serial_struct* %7)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %22
store i32 -1, i32* %3
br label %129
31: ; preds = %22
%32 = load i32, i32* @ASYNC_SPD_MASK
%33 = xor i32 %32, -1
%34 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 2
%35 = load i32, i32* %34
%36 = and i32 %35, %33
store i32 %36, i32* %34
%37 = load i32, i32* @ASYNC_SPD_CUST
%38 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 2
%39 = load i32, i32* %38
%40 = or i32 %39, %37
store i32 %40, i32* %38
%41 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 1
%42 = load i32, i32* %41
%43 = load i32, i32* %5
%44 = sdiv i32 %43, 2
%45 = add nsw i32 %42, %44
%46 = load i32, i32* %5
%47 = sdiv i32 %45, %46
%48 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 0
store i32 %47, i32* %48
%49 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 0
%50 = load i32, i32* %49
%51 = icmp slt i32 %50, 1
br i1 %51, label %52, label %54
52: ; preds = %31
%53 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 0
store i32 1, i32* %53
br label %54
54: ; preds = %52, %31
%55 = load i32, i32* %8
%56 = load i32, i32* @TIOCSSERIAL
%57 = call i64 @ioctl(i32 %55, i32 %56, %struct.serial_struct* %7)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %54
store i32 -1, i32* %3
br label %129
60: ; preds = %54
%61 = load i32, i32* %8
%62 = load i32, i32* @TIOCGSERIAL
%63 = call i64 @ioctl(i32 %61, i32 %62, %struct.serial_struct* %7)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %60
store i32 -1, i32* %3
br label %129
66: ; preds = %60
%67 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 0
%68 = load i32, i32* %67
%69 = load i32, i32* %5
%70 = mul nsw i32 %68, %69
%71 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 1
%72 = load i32, i32* %71
%73 = icmp ne i32 %70, %72
br i1 %73, label %74, label %87
74: ; preds = %66
%75 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 1
%76 = load i32, i32* %75
%77 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 0
%78 = load i32, i32* %77
%79 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 1
%80 = load i32, i32* %79
%81 = sitofp i32 %80 to float
%82 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 0
%83 = load i32, i32* %82
%84 = sitofp i32 %83 to float
%85 = fdiv float %81, %84
%86 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %76, i32 %78, float %85)
br label %87
87: ; preds = %74, %66
br label %88
88: ; preds = %87, %17
%89 = load i32, i32* %8
%90 = load i32, i32* @F_SETFL
%91 = call i32 @fcntl(i32 %89, i32 %90, i32 0)
%92 = load i32, i32* %8
%93 = call i32 @tcgetattr(i32 %92, %struct.termios* %6)
%94 = load i32, i32* %9
%95 = icmp ne i32 %94, 0
br i1 %95, label %96, label %97
96: ; preds = %88
br label %99
97: ; preds = %88
%98 = load i32, i32* @B38400
br label %99
99: ; preds = %97, %96
%100 = phi i32 [ %94, %96 ], [ %98, %97 ]
%101 = call i32 @cfsetispeed(%struct.termios* %6, i32 %100)
%102 = load i32, i32* %9
%103 = icmp ne i32 %102, 0
br i1 %103, label %104, label %105
104: ; preds = %99
br label %107
105: ; preds = %99
%106 = load i32, i32* @B38400
br label %107
107: ; preds = %105, %104
%108 = phi i32 [ %102, %104 ], [ %106, %105 ]
%109 = call i32 @cfsetospeed(%struct.termios* %6, i32 %108)
%110 = call i32 @cfmakeraw(%struct.termios* %6)
%111 = load i32, i32* @CLOCAL
%112 = load i32, i32* @CREAD
%113 = or i32 %111, %112
%114 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%115 = load i32, i32* %114
%116 = or i32 %115, %113
store i32 %116, i32* %114
%117 = load i32, i32* @CRTSCTS
%118 = xor i32 %117, -1
%119 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%120 = load i32, i32* %119
%121 = and i32 %120, %118
store i32 %121, i32* %119
%122 = load i32, i32* %8
%123 = load i32, i32* @TCSANOW
%124 = call i64 @tcsetattr(i32 %122, i32 %123, %struct.termios* %6)
%125 = icmp ne i64 %124, 0
br i1 %125, label %126, label %127
126: ; preds = %107
store i32 -1, i32* %3
br label %129
127: ; preds = %107
%128 = load i32, i32* %8
store i32 %128, i32* %3
br label %129
129: ; preds = %127, %126, %65, %59, %30, %16
%130 = load i32, i32* %3
ret i32 %130
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TIOCGSERIAL = global i32 0
@ASYNC_SPD_MASK = global i32 0
@ASYNC_SPD_CUST = global i32 0
@TIOCSSERIAL = global i32 0
@F_SETFL = global i32 0
@B38400 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@CRTSCTS = global i32 0
@TCSANOW = global i32 0
define i32 @serial_open(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 eq i32 %8, -1
br i1 %9, label %32, label %10
10: ; preds = %2
%11 = tail call i32 @rate_to_constant(i32 %1)
%12 = icmp ne i32 %11, 0
%13 = load i32, i32* @F_SETFL
%14 = call i32 @fcntl(i32 %8, i32 %13, i32 0)
%15 = call i32 @tcgetattr(i32 %8, %struct.termios* %3)
%16 = call i32 @cfsetispeed(%struct.termios* %3, i32 %11)
%17 = call i32 @cfsetospeed(%struct.termios* %3, i32 %11)
%18 = call i32 @cfmakeraw(%struct.termios* %3)
%19 = load i32, i32* @CLOCAL
%20 = load i32, i32* @CREAD
%21 = or i32 %20, %19
%22 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = or i32 %21, %23
%25 = load i32, i32* @CRTSCTS
%26 = xor i32 %25, -1
%27 = and i32 %24, %26
store i32 %27, i32* %22
%28 = load i32, i32* @TCSANOW
%29 = call i64 @tcsetattr(i32 %8, i32 %28, %struct.termios* %3)
%30 = icmp eq i64 %29, 0
%31 = select i1 %30, i32 %8, i32 -1
br label %32
32: ; preds = %10, %2
%33 = phi i32 [ -1, %2 ], [ %31, %10 ]
ret i32 %33
}