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

/* Variables and functions */
int ASYNC_LOW_LATENCY;
int ASYNC_SPD_CUST;
int /*<<< orphan*/ B38400;
int /*<<< orphan*/ TIOCGSERIAL;
int /*<<< orphan*/ TIOCSSERIAL;
int _osmo_serial_set_baudrate(int, int /*<<< orphan*/);
int errno;
int ioctl(int, int /*<<< orphan*/, struct serial_struct *);
int osmo_serial_set_custom_baudrate(int fd, int baudrate) {
  int rc;
  struct serial_struct ser_info;
  rc = ioctl(fd, TIOCGSERIAL, &ser_info);
  if (rc < 0) {
    do {
    } while (0);
    return -errno;
  }
  ser_info.flags = ASYNC_SPD_CUST | ASYNC_LOW_LATENCY;
  ser_info.custom_divisor = ser_info.baud_base / baudrate;
  rc = ioctl(fd, TIOCSSERIAL, &ser_info);
  if (rc < 0) {
    do {
    } while (0);
    return -errno;
  }
  return _osmo_serial_set_baudrate(fd, B38400);
}


x86 assembly (clang, -O0):
<osmo_serial_set_custom_baudrate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    116d <osmo_serial_set_custom_baudrate+0x4d>
jmp    1155 <osmo_serial_set_custom_baudrate+0x35>
jmp    115a <osmo_serial_set_custom_baudrate+0x3a>
xor    %eax,%eax
mov    0x2e65(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11df <osmo_serial_set_custom_baudrate+0xbf>
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    0x2e5b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11cb <osmo_serial_set_custom_baudrate+0xab>
jmp    11b3 <osmo_serial_set_custom_baudrate+0x93>
jmp    11b8 <osmo_serial_set_custom_baudrate+0x98>
xor    %eax,%eax
mov    0x2e07(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11df <osmo_serial_set_custom_baudrate+0xbf>
mov    -0x8(%rbp),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
call   1030 <_osmo_serial_set_baudrate@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<osmo_serial_set_custom_baudrate>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1040 <ioctl@plt>
test   %eax,%eax
js     118d <osmo_serial_set_custom_baudrate+0x6d>
mov    0x2ea0(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    0x10(%rsp),%eax
cltd
idiv   %ebp
mov    %eax,0xc(%rsp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %eax,%eax
js     118d <osmo_serial_set_custom_baudrate+0x6d>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <_osmo_serial_set_baudrate@plt>
jmp    1198 <osmo_serial_set_custom_baudrate+0x78>
mov    0x2e34(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.serial_struct = type { i32, i32, i32 }
@TIOCGSERIAL = global i32 0
@errno = global i32 0
@ASYNC_SPD_CUST = global i32 0
@ASYNC_LOW_LATENCY = global i32 0
@TIOCSSERIAL = global i32 0
@B38400 = global i32 0
define i32 @osmo_serial_set_custom_baudrate(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.serial_struct
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = load i32, i32* @TIOCGSERIAL
%10 = call i32 @ioctl(i32 %8, i32 %9, %struct.serial_struct* %7)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
br label %14
14: ; preds = %13
br label %15
15: ; preds = %14
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
store i32 %17, i32* %3
br label %42
18: ; preds = %2
%19 = load i32, i32* @ASYNC_SPD_CUST
%20 = load i32, i32* @ASYNC_LOW_LATENCY
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 0
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 2
%24 = load i32, i32* %23
%25 = load i32, i32* %5
%26 = sdiv i32 %24, %25
%27 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 1
store i32 %26, i32* %27
%28 = load i32, i32* %4
%29 = load i32, i32* @TIOCSSERIAL
%30 = call i32 @ioctl(i32 %28, i32 %29, %struct.serial_struct* %7)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %18
br label %34
34: ; preds = %33
br label %35
35: ; preds = %34
%36 = load i32, i32* @errno
%37 = sub nsw i32 0, %36
store i32 %37, i32* %3
br label %42
38: ; preds = %18
%39 = load i32, i32* %4
%40 = load i32, i32* @B38400
%41 = call i32 @_osmo_serial_set_baudrate(i32 %39, i32 %40)
store i32 %41, i32* %3
br label %42
42: ; preds = %38, %35, %15
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
%struct.serial_struct = type { i32, i32, i32 }
@TIOCGSERIAL = global i32 0
@errno = global i32 0
@ASYNC_SPD_CUST = global i32 0
@ASYNC_LOW_LATENCY = global i32 0
@TIOCSSERIAL = global i32 0
@B38400 = global i32 0
define i32 @osmo_serial_set_custom_baudrate(i32 %0, i32 %1) {
%3 = alloca %struct.serial_struct
%4 = bitcast %struct.serial_struct* %3 to i8*
%5 = load i32, i32* @TIOCGSERIAL
%6 = call i32 @ioctl(i32 %0, i32 %5, %struct.serial_struct* %3)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @errno
%10 = sub nsw i32 0, %9
br label %29
11: ; preds = %2
%12 = load i32, i32* @ASYNC_SPD_CUST
%13 = load i32, i32* @ASYNC_LOW_LATENCY
%14 = or i32 %13, %12
%15 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %3, i64 0, i32 0
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %3, i64 0, i32 2
%17 = load i32, i32* %16
%18 = sdiv i32 %17, %1
%19 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %3, i64 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @TIOCSSERIAL
%21 = call i32 @ioctl(i32 %0, i32 %20, %struct.serial_struct* %3)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %11
%24 = load i32, i32* @errno
%25 = sub nsw i32 0, %24
br label %29
26: ; preds = %11
%27 = load i32, i32* @B38400
%28 = call i32 @_osmo_serial_set_baudrate(i32 %0, i32 %27)
br label %29
29: ; preds = %26, %23, %8
%30 = phi i32 [ %10, %8 ], [ %25, %23 ], [ %28, %26 ]
ret i32 %30
}