#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 rate_to_constant(int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ warnx(char *, int, int, int);
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) {
      warnx("actual baudrate is %d / %d = %d", serinfo.baud_base,
            serinfo.custom_divisor, 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    13f8 <serial_open+0x258>
mov    -0x14(%rbp),%edi
call   1070 <rate_to_constant@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jne    1311 <serial_open+0x171>
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    13f8 <serial_open+0x258>
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    13f8 <serial_open+0x258>
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    13f8 <serial_open+0x258>
mov    -0x30(%rbp),%eax
imul   -0x14(%rbp),%eax
cmp    -0x2c(%rbp),%eax
je     130c <serial_open+0x16c>
mov    -0x2c(%rbp),%esi
mov    -0x30(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x2c(%rbp),%eax
cltd
idivl  -0x30(%rbp)
mov    -0x40(%rbp),%edx
mov    %eax,%ecx
lea    0xcf9(%rip),%rdi
call   1050 <warnx@plt>
jmp    1311 <serial_open+0x171>
mov    -0x34(%rbp),%edi
mov    0x2c8d(%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,-0x44(%rbp)
cmp    $0x0,%eax
je     134a <serial_open+0x1aa>
mov    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
jmp    1356 <serial_open+0x1b6>
mov    0x2c2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   10c0 <cfsetispeed@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
je     137c <serial_open+0x1dc>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x50(%rbp)
jmp    1388 <serial_open+0x1e8>
mov    0x2bfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1040 <cfsetospeed@plt>
lea    -0x18(%rbp),%rdi
call   1080 <cfmakeraw@plt>
mov    0x2be4(%rip),%rax
mov    (%rax),%eax
mov    0x2c13(%rip),%rcx
or     (%rcx),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2bd4(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2bff(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   10a0 <tcsetattr@plt>
cmp    $0x0,%rax
je     13f2 <serial_open+0x252>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f8 <serial_open+0x258>
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 = %d\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 %127
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 %86
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 %127
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 %127
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 %127
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 %85
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 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 0
%82 = load i32, i32* %81
%83 = sdiv i32 %80, %82
%84 = call i32 @warnx(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %76, i32 %78, i32 %83)
br label %85
85: ; preds = %74, %66
br label %86
86: ; preds = %85, %17
%87 = load i32, i32* %8
%88 = load i32, i32* @F_SETFL
%89 = call i32 @fcntl(i32 %87, i32 %88, i32 0)
%90 = load i32, i32* %8
%91 = call i32 @tcgetattr(i32 %90, %struct.termios* %6)
%92 = load i32, i32* %9
%93 = icmp ne i32 %92, 0
br i1 %93, label %94, label %95
94: ; preds = %86
br label %97
95: ; preds = %86
%96 = load i32, i32* @B38400
br label %97
97: ; preds = %95, %94
%98 = phi i32 [ %92, %94 ], [ %96, %95 ]
%99 = call i32 @cfsetispeed(%struct.termios* %6, i32 %98)
%100 = load i32, i32* %9
%101 = icmp ne i32 %100, 0
br i1 %101, label %102, label %103
102: ; preds = %97
br label %105
103: ; preds = %97
%104 = load i32, i32* @B38400
br label %105
105: ; preds = %103, %102
%106 = phi i32 [ %100, %102 ], [ %104, %103 ]
%107 = call i32 @cfsetospeed(%struct.termios* %6, i32 %106)
%108 = call i32 @cfmakeraw(%struct.termios* %6)
%109 = load i32, i32* @CLOCAL
%110 = load i32, i32* @CREAD
%111 = or i32 %109, %110
%112 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%113 = load i32, i32* %112
%114 = or i32 %113, %111
store i32 %114, i32* %112
%115 = load i32, i32* @CRTSCTS
%116 = xor i32 %115, -1
%117 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%118 = load i32, i32* %117
%119 = and i32 %118, %116
store i32 %119, i32* %117
%120 = load i32, i32* %8
%121 = load i32, i32* @TCSANOW
%122 = call i64 @tcsetattr(i32 %120, i32 %121, %struct.termios* %6)
%123 = icmp ne i64 %122, 0
br i1 %123, label %124, label %125
124: ; preds = %105
store i32 -1, i32* %3
br label %127
125: ; preds = %105
%126 = load i32, i32* %8
store i32 %126, i32* %3
br label %127
127: ; preds = %125, %124, %65, %59, %30, %16
%128 = load i32, i32* %3
ret i32 %128
}

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
}