#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*/ errno;
int ioctl(int, int /*<<< orphan*/, struct serial_struct *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int to_speed(int);
int init_serial_port(int fd, int speed) {
  tcflag_t speed_preset = to_speed(speed);
  if (speed_preset == B0) {
    struct serial_struct serial;
    if (ioctl(fd, TIOCGSERIAL, &serial) == -1)
      return -1;
    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;
      return -1;
    }
    if (ioctl(fd, TIOCSSERIAL, &serial) == -1)
      return -1;
    speed_preset = B38400;
  }
  struct termios term;
  term.c_cflag = speed_preset | CS8 | CLOCAL | CREAD;
  cfmakeraw(&term);
  if (tcsetattr(fd, TCSANOW, &term) == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<init_serial_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <to_speed@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%eax
jne    126d <init_serial_port+0x12d>
mov    -0x8(%rbp),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1195 <init_serial_port+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <init_serial_port+0x188>
mov    -0x20(%rbp),%eax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    0x2e3b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
add    %ecx,%eax
cltd
idivl  -0xc(%rbp)
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
cltd
idivl  -0x1c(%rbp)
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
imul   $0x62,-0xc(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jl     1219 <init_serial_port+0xd9>
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
imul   $0x66,-0xc(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jle    1237 <init_serial_port+0xf7>
mov    0x2db0(%rip),%rax
mov    (%rax),%ecx
mov    0x2d77(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <init_serial_port+0x188>
mov    -0x8(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1261 <init_serial_port+0x121>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <init_serial_port+0x188>
mov    0x2d18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2d41(%rip),%rcx
or     (%rcx),%eax
mov    0x2d08(%rip),%rcx
or     (%rcx),%eax
mov    0x2d37(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
call   1040 <cfmakeraw@plt>
mov    -0x8(%rbp),%edi
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    12c1 <init_serial_port+0x181>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c8 <init_serial_port+0x188>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_serial_port>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
mov    %esi,%edi
call   1050 <to_speed@plt>
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%eax
jne    122f <init_serial_port+0xef>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1205 <init_serial_port+0xc5>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2e54(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
mov    0x10(%rsp),%ecx
mov    %ebp,%eax
shr    $0x1f,%eax
add    %ebp,%eax
sar    %eax
add    %ecx,%eax
cltd
idiv   %ebp
mov    %eax,%esi
mov    %eax,0xc(%rsp)
mov    %ecx,%eax
cltd
idiv   %esi
imul   $0x62,%ebp,%ecx
movslq %ecx,%rcx
imul   $0x51eb851f,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
cmp    %ecx,%eax
jl     11f3 <init_serial_port+0xb3>
imul   $0x66,%ebp,%ecx
movslq %ecx,%rcx
imul   $0x51eb851f,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
cmp    %ecx,%eax
jle    120c <init_serial_port+0xcc>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    0x2d9d(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    127a <init_serial_port+0x13a>
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1205 <init_serial_port+0xc5>
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    0x2d82(%rip),%rcx
or     (%rcx),%eax
mov    0x2d49(%rip),%rcx
or     (%rcx),%eax
mov    0x2d78(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <cfmakeraw@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1060 <tcsetattr@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
add    $0x18,%rsp
pop    %rbx
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 @init_serial_port(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.serial_struct
%8 = alloca i32
%9 = alloca %struct.termios
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* %5
%11 = call i32 @to_speed(i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = load i32, i32* @B0
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %64
15: ; preds = %2
%16 = load i32, i32* %4
%17 = load i32, i32* @TIOCGSERIAL
%18 = call i32 @ioctl(i32 %16, i32 %17, %struct.serial_struct* %7)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 -1, i32* %3
br label %80
21: ; preds = %15
%22 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @ASYNC_SPD_MASK
%25 = xor i32 %24, -1
%26 = and i32 %23, %25
%27 = load i32, i32* @ASYNC_SPD_CUST
%28 = or i32 %26, %27
%29 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 0
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 2
%31 = load i32, i32* %30
%32 = load i32, i32* %5
%33 = sdiv i32 %32, 2
%34 = add nsw i32 %31, %33
%35 = load i32, i32* %5
%36 = sdiv i32 %34, %35
%37 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 1
store i32 %36, i32* %37
%38 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 2
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 1
%41 = load i32, i32* %40
%42 = sdiv i32 %39, %41
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = load i32, i32* %5
%45 = mul nsw i32 %44, 98
%46 = sdiv i32 %45, 100
%47 = icmp slt i32 %43, %46
br i1 %47, label %54, label %48
48: ; preds = %21
%49 = load i32, i32* %8
%50 = load i32, i32* %5
%51 = mul nsw i32 %50, 102
%52 = sdiv i32 %51, 100
%53 = icmp sgt i32 %49, %52
br i1 %53, label %54, label %56
54: ; preds = %48, %21
%55 = load i32, i32* @ENOTSUP
store i32 %55, i32* @errno
store i32 -1, i32* %3
br label %80
56: ; preds = %48
%57 = load i32, i32* %4
%58 = load i32, i32* @TIOCSSERIAL
%59 = call i32 @ioctl(i32 %57, i32 %58, %struct.serial_struct* %7)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %62
61: ; preds = %56
store i32 -1, i32* %3
br label %80
62: ; preds = %56
%63 = load i32, i32* @B38400
store i32 %63, i32* %6
br label %64
64: ; preds = %62, %2
%65 = load i32, i32* %6
%66 = load i32, i32* @CS8
%67 = or i32 %65, %66
%68 = load i32, i32* @CLOCAL
%69 = or i32 %67, %68
%70 = load i32, i32* @CREAD
%71 = or i32 %69, %70
%72 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
store i32 %71, i32* %72
%73 = call i32 @cfmakeraw(%struct.termios* %9)
%74 = load i32, i32* %4
%75 = load i32, i32* @TCSANOW
%76 = call i32 @tcsetattr(i32 %74, i32 %75, %struct.termios* %9)
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %79
78: ; preds = %64
store i32 -1, i32* %3
br label %80
79: ; preds = %64
store i32 0, i32* %3
br label %80
80: ; preds = %79, %78, %61, %54, %20
%81 = load i32, i32* %3
ret i32 %81
}

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