#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 flags;
  int custom_divisor;
  int baud_base;
};

/* Variables and functions */
int ASYNC_SPD_CUST;
int ASYNC_SPD_MASK;
int B38400;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ ENOTSUP;
int O_NOCTTY;
int O_RDWR;
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 serial_open_raw(const char *dev, int speed) {
  int fd = open(dev, O_RDWR | O_NOCTTY);
  if (fd == -1)
    goto not_open;
  struct termios term;
  term.c_cflag = B38400 | CS8 | CLOCAL | CREAD;
  cfmakeraw(&term);
  if (tcsetattr(fd, TCSANOW, &term) == -1)
    goto fail;
  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;
  return fd;
fail:
  close(fd);
not_open:
  return -1;
}


x86 assembly (clang, -O0):
<serial_open_raw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    0x2e2d(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    118c <serial_open_raw+0x3c>
jmp    12d3 <serial_open_raw+0x183>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    0x2e14(%rip),%rcx
or     (%rcx),%eax
mov    0x2ddb(%rip),%rcx
or     (%rcx),%eax
mov    0x2e0a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <cfmakeraw@plt>
mov    -0x18(%rbp),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    11df <serial_open_raw+0x8f>
jmp    12cb <serial_open_raw+0x17b>
mov    -0x18(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1202 <serial_open_raw+0xb2>
jmp    12cb <serial_open_raw+0x17b>
mov    -0x30(%rbp),%eax
mov    0x2db4(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    0x2dce(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%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,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
cltd
idivl  -0x2c(%rbp)
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
imul   $0x62,-0x14(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
cmp    %ecx,%eax
jl     1286 <serial_open_raw+0x136>
mov    -0x34(%rbp),%eax
mov    %eax,-0x40(%rbp)
imul   $0x66,-0x14(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
cmp    %ecx,%eax
jle    129d <serial_open_raw+0x14d>
mov    0x2d3b(%rip),%rax
mov    (%rax),%ecx
mov    0x2d02(%rip),%rax
mov    %ecx,(%rax)
jmp    12cb <serial_open_raw+0x17b>
mov    -0x18(%rbp),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    12c0 <serial_open_raw+0x170>
jmp    12cb <serial_open_raw+0x17b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12da <serial_open_raw+0x18a>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_open_raw>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    0x2e74(%rip),%rax
mov    0x2e35(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1070 <open@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     1283 <serial_open_raw+0x133>
mov    %eax,%ebp
mov    0x2dec(%rip),%rax
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2de2(%rip),%rax
or     (%rax),%ecx
mov    0x2e11(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x10(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <cfmakeraw@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1060 <tcsetattr@plt>
cmp    $0xffffffff,%eax
je     127c <serial_open_raw+0x12c>
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     127c <serial_open_raw+0x12c>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
not    %eax
and    (%rsp),%eax
mov    0x2de1(%rip),%rcx
or     (%rcx),%eax
mov    %eax,(%rsp)
mov    0x8(%rsp),%ecx
mov    %r15d,%eax
shr    $0x1f,%eax
add    %r15d,%eax
sar    %eax
add    %ecx,%eax
cltd
idiv   %r15d
mov    %eax,%esi
mov    %eax,0x4(%rsp)
mov    %ecx,%eax
cltd
idiv   %esi
imul   $0x62,%r15d,%ecx
movslq %ecx,%rcx
imul   $0x51eb851f,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
cmp    %ecx,%eax
jl     126a <serial_open_raw+0x11a>
imul   $0x66,%r15d,%ecx
movslq %ecx,%rcx
imul   $0x51eb851f,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
cmp    %ecx,%eax
jle    1291 <serial_open_raw+0x141>
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
mov    0x2d1e(%rip),%rcx
mov    %eax,(%rcx)
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     127c <serial_open_raw+0x12c>
mov    %ebp,%r14d
jmp    1283 <serial_open_raw+0x133>

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

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