#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 */
typedef int tcflag_t;
struct termios {
  int c_cflag;
};
struct serial_struct {
  int flags;
  int custom_divisor;
  int baud_base;
};

/* Variables and functions */
int ASYNC_SPD_CUST;
int ASYNC_SPD_MASK;
int B0;
int B38400;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ ENOTSUP;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCGSERIAL;
int /*<<< orphan*/ TIOCSSERIAL;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int ioctl(int, int /*<<< orphan*/, struct serial_struct *);
int open(char const *, int);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int to_speed(int);
int serial_open_raw(const char *dev, int flags, int speed) {
  int fd = open(dev, flags);
  if (fd == -1)
    goto not_open;
  if (speed == 0)
    return fd;
  tcflag_t speed_preset = to_speed(speed);
  if (speed_preset == B0) {
    struct serial_struct serial;
    if (ioctl(fd, TIOCGSERIAL, &serial) == -1)
      goto fail;
    serial.flags = (serial.flags & ~ASYNC_SPD_MASK) | ASYNC_SPD_CUST;
    serial.custom_divisor = (serial.baud_base + (speed / 2)) / speed;
    int real_speed = serial.baud_base / serial.custom_divisor;
    if (real_speed < speed * 98 / 100 || real_speed > speed * 102 / 100) {
      errno = ENOTSUP;
      goto fail;
    }
    if (ioctl(fd, TIOCSSERIAL, &serial) == -1)
      goto fail;
    speed_preset = B38400;
  }
  struct termios term;
  term.c_cflag = speed_preset | CS8 | CLOCAL | CREAD;
  cfmakeraw(&term);
  if (tcsetattr(fd, TCSANOW, &term) == -1)
    goto fail;
  return fd;
fail:
  close(fd);
not_open:
  return -1;
}


x86 assembly (clang, -O0):
<serial_open_raw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1190 <serial_open_raw+0x30>
jmp    130f <serial_open_raw+0x1af>
cmpl   $0x0,-0x18(%rbp)
jne    11a5 <serial_open_raw+0x45>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1316 <serial_open_raw+0x1b6>
mov    -0x18(%rbp),%edi
call   1060 <to_speed@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2dee(%rip),%rcx
cmp    (%rcx),%eax
jne    12af <serial_open_raw+0x14f>
mov    -0x1c(%rbp),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11e5 <serial_open_raw+0x85>
jmp    1307 <serial_open_raw+0x1a7>
mov    -0x30(%rbp),%eax
mov    0x2dd9(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    0x2deb(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
add    %ecx,%eax
cltd
idivl  -0x18(%rbp)
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
cltd
idivl  -0x2c(%rbp)
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x3c(%rbp)
imul   $0x62,-0x18(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
cmp    %ecx,%eax
jl     1269 <serial_open_raw+0x109>
mov    -0x34(%rbp),%eax
mov    %eax,-0x44(%rbp)
imul   $0x66,-0x18(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
cmp    %ecx,%eax
jle    1280 <serial_open_raw+0x120>
mov    0x2d60(%rip),%rax
mov    (%rax),%ecx
mov    0x2d27(%rip),%rax
mov    %ecx,(%rax)
jmp    1307 <serial_open_raw+0x1a7>
mov    -0x1c(%rbp),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    12a3 <serial_open_raw+0x143>
jmp    1307 <serial_open_raw+0x1a7>
mov    0x2cd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2cff(%rip),%rcx
or     (%rcx),%eax
mov    0x2cc6(%rip),%rcx
or     (%rcx),%eax
mov    0x2cf5(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
lea    -0x38(%rbp),%rdi
call   1050 <cfmakeraw@plt>
mov    -0x1c(%rbp),%edi
mov    0x2cf5(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    12fc <serial_open_raw+0x19c>
jmp    1307 <serial_open_raw+0x1a7>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1316 <serial_open_raw+0x1b6>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_open_raw>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebx
call   1080 <open@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     12b8 <serial_open_raw+0x158>
mov    %eax,%ebp
test   %ebx,%ebx
je     12ac <serial_open_raw+0x14c>
mov    %ebx,%edi
call   1060 <to_speed@plt>
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%eax
jne    1268 <serial_open_raw+0x108>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     12b1 <serial_open_raw+0x151>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x10(%rsp),%eax
mov    0x2e16(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x10(%rsp)
mov    0x18(%rsp),%ecx
mov    %ebx,%eax
shr    $0x1f,%eax
add    %ebx,%eax
sar    %eax
add    %ecx,%eax
cltd
idiv   %ebx
mov    %eax,%esi
mov    %eax,0x14(%rsp)
mov    %ecx,%eax
cltd
idiv   %esi
imul   $0x62,%ebx,%ecx
movslq %ecx,%rcx
imul   $0x51eb851f,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
cmp    %ecx,%eax
jl     1231 <serial_open_raw+0xd1>
imul   $0x66,%ebx,%ecx
movslq %ecx,%rcx
imul   $0x51eb851f,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
cmp    %ecx,%eax
jle    1245 <serial_open_raw+0xe5>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    0x2d5f(%rip),%rcx
mov    %eax,(%rcx)
jmp    12b1 <serial_open_raw+0x151>
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     12b1 <serial_open_raw+0x151>
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
mov    0x2d49(%rip),%rcx
or     (%rcx),%eax
mov    0x2d10(%rip),%rcx
or     (%rcx),%eax
mov    0x2d3f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <cfmakeraw@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1070 <tcsetattr@plt>
cmp    $0xffffffff,%eax
je     12b1 <serial_open_raw+0x151>
mov    %ebp,%r14d
jmp    12b8 <serial_open_raw+0x158>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.serial_struct = type { i32, i32, i32 }
%struct.termios = type { i32 }
@B0 = global i32 0
@TIOCGSERIAL = global i32 0
@ASYNC_SPD_MASK = global i32 0
@ASYNC_SPD_CUST = global i32 0
@ENOTSUP = global i32 0
@errno = global i32 0
@TIOCSSERIAL = global i32 0
@B38400 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@TCSANOW = global i32 0
define i32 @serial_open_raw(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.serial_struct
%11 = alloca i32
%12 = alloca %struct.termios
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %3
br label %99
19: ; preds = %3
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = load i32, i32* %8
store i32 %23, i32* %4
br label %100
24: ; preds = %19
%25 = load i32, i32* %7
%26 = call i32 @to_speed(i32 %25)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = load i32, i32* @B0
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %79
30: ; preds = %24
%31 = load i32, i32* %8
%32 = load i32, i32* @TIOCGSERIAL
%33 = call i32 @ioctl(i32 %31, i32 %32, %struct.serial_struct* %10)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %30
br label %96
36: ; preds = %30
%37 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %10, i32 0, i32 0
%38 = load i32, i32* %37
%39 = load i32, i32* @ASYNC_SPD_MASK
%40 = xor i32 %39, -1
%41 = and i32 %38, %40
%42 = load i32, i32* @ASYNC_SPD_CUST
%43 = or i32 %41, %42
%44 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %10, i32 0, i32 0
store i32 %43, i32* %44
%45 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %10, i32 0, i32 2
%46 = load i32, i32* %45
%47 = load i32, i32* %7
%48 = sdiv i32 %47, 2
%49 = add nsw i32 %46, %48
%50 = load i32, i32* %7
%51 = sdiv i32 %49, %50
%52 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %10, i32 0, i32 1
store i32 %51, i32* %52
%53 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %10, i32 0, i32 2
%54 = load i32, i32* %53
%55 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %10, i32 0, i32 1
%56 = load i32, i32* %55
%57 = sdiv i32 %54, %56
store i32 %57, i32* %11
%58 = load i32, i32* %11
%59 = load i32, i32* %7
%60 = mul nsw i32 %59, 98
%61 = sdiv i32 %60, 100
%62 = icmp slt i32 %58, %61
br i1 %62, label %69, label %63
63: ; preds = %36
%64 = load i32, i32* %11
%65 = load i32, i32* %7
%66 = mul nsw i32 %65, 102
%67 = sdiv i32 %66, 100
%68 = icmp sgt i32 %64, %67
br i1 %68, label %69, label %71
69: ; preds = %63, %36
%70 = load i32, i32* @ENOTSUP
store i32 %70, i32* @errno
br label %96
71: ; preds = %63
%72 = load i32, i32* %8
%73 = load i32, i32* @TIOCSSERIAL
%74 = call i32 @ioctl(i32 %72, i32 %73, %struct.serial_struct* %10)
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %77
76: ; preds = %71
br label %96
77: ; preds = %71
%78 = load i32, i32* @B38400
store i32 %78, i32* %9
br label %79
79: ; preds = %77, %24
%80 = load i32, i32* %9
%81 = load i32, i32* @CS8
%82 = or i32 %80, %81
%83 = load i32, i32* @CLOCAL
%84 = or i32 %82, %83
%85 = load i32, i32* @CREAD
%86 = or i32 %84, %85
%87 = getelementptr inbounds %struct.termios, %struct.termios* %12, i32 0, i32 0
store i32 %86, i32* %87
%88 = call i32 @cfmakeraw(%struct.termios* %12)
%89 = load i32, i32* %8
%90 = load i32, i32* @TCSANOW
%91 = call i32 @tcsetattr(i32 %89, i32 %90, %struct.termios* %12)
%92 = icmp eq i32 %91, -1
br i1 %92, label %93, label %94
93: ; preds = %79
br label %96
94: ; preds = %79
%95 = load i32, i32* %8
store i32 %95, i32* %4
br label %100
96: ; preds = %93, %76, %69, %35
%97 = load i32, i32* %8
%98 = call i32 @close(i32 %97)
br label %99
99: ; preds = %96, %18
store i32 -1, i32* %4
br label %100
100: ; preds = %99, %94, %22
%101 = load i32, i32* %4
ret i32 %101
}

LLVM IR (-O2):
%struct.serial_struct = type { i32, i32, i32 }
%struct.termios = type { i32 }
@B0 = global i32 0
@TIOCGSERIAL = global i32 0
@ASYNC_SPD_MASK = global i32 0
@ASYNC_SPD_CUST = global i32 0
@ENOTSUP = global i32 0
@errno = global i32 0
@TIOCSSERIAL = global i32 0
@B38400 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@TCSANOW = global i32 0
define i32 @serial_open_raw(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca %struct.serial_struct
%5 = alloca %struct.termios
%6 = tail call i32 @open(i8* %0, i32 %1)
%7 = icmp eq i32 %6, -1
br i1 %7, label %65, label %8
8: ; preds = %3
%9 = icmp eq i32 %2, 0
br i1 %9, label %65, label %10
10: ; preds = %8
%11 = tail call i32 @to_speed(i32 %2)
%12 = load i32, i32* @B0
%13 = icmp eq i32 %11, %12
br i1 %13, label %14, label %50
14: ; preds = %10
%15 = bitcast %struct.serial_struct* %4 to i8*
%16 = load i32, i32* @TIOCGSERIAL
%17 = call i32 @ioctl(i32 %6, i32 %16, %struct.serial_struct* %4)
%18 = icmp eq i32 %17, -1
br i1 %18, label %47, label %19
19: ; preds = %14
%20 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i64 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @ASYNC_SPD_MASK
%23 = xor i32 %22, -1
%24 = and i32 %21, %23
%25 = load i32, i32* @ASYNC_SPD_CUST
%26 = or i32 %24, %25
store i32 %26, i32* %20
%27 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i64 0, i32 2
%28 = load i32, i32* %27
%29 = sdiv i32 %2, 2
%30 = add nsw i32 %28, %29
%31 = sdiv i32 %30, %2
%32 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i64 0, i32 1
store i32 %31, i32* %32
%33 = sdiv i32 %28, %31
%34 = mul nsw i32 %2, 98
%35 = sdiv i32 %34, 100
%36 = icmp slt i32 %33, %35
br i1 %36, label %41, label %37
37: ; preds = %19
%38 = mul nsw i32 %2, 102
%39 = sdiv i32 %38, 100
%40 = icmp sgt i32 %33, %39
br i1 %40, label %41, label %43
41: ; preds = %37, %19
%42 = load i32, i32* @ENOTSUP
store i32 %42, i32* @errno
br label %47
43: ; preds = %37
%44 = load i32, i32* @TIOCSSERIAL
%45 = call i32 @ioctl(i32 %6, i32 %44, %struct.serial_struct* %4)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %14, %41, %43
br label %63
48: ; preds = %43
%49 = load i32, i32* @B38400
br label %50
50: ; preds = %48, %10
%51 = phi i32 [ %49, %48 ], [ %11, %10 ]
%52 = load i32, i32* @CS8
%53 = or i32 %52, %51
%54 = load i32, i32* @CLOCAL
%55 = or i32 %53, %54
%56 = load i32, i32* @CREAD
%57 = or i32 %55, %56
%58 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 0
store i32 %57, i32* %58
%59 = call i32 @cfmakeraw(%struct.termios* %5)
%60 = load i32, i32* @TCSANOW
%61 = call i32 @tcsetattr(i32 %6, i32 %60, %struct.termios* %5)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %47, %50
%64 = call i32 @close(i32 %6)
br label %65
65: ; preds = %63, %3, %50, %8
%66 = phi i32 [ %6, %8 ], [ %6, %50 ], [ -1, %3 ], [ -1, %63 ]
ret i32 %66
}