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

/* Variables and functions */
int ASYNC_SPD_CUST;
int ASYNC_SPD_MASK;
int /*<<< orphan*/ B38400;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ TIOCGSERIAL;
int /*<<< orphan*/ TIOCSSERIAL;
scalar_t__ cfsetispeed(struct termios *, int /*<<< orphan*/);
scalar_t__ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int ioctl(int, int /*<<< orphan*/, struct serial_struct *);
int /*<<< orphan*/ printf(char *, unsigned int, unsigned int);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int SerialPort_setBaudRate(int fd, unsigned br) {
  struct termios config;
  struct serial_struct ss;
  unsigned closestSpeed;
  int res;
  res = tcgetattr(fd, &config);
  if (res < 0)
    return res;
  res = ioctl(fd, TIOCGSERIAL, &ss);
  if (res < 0)
    return res;
  ss.flags = (ss.flags & ~ASYNC_SPD_MASK) | ASYNC_SPD_CUST;
  ss.custom_divisor = (ss.baud_base + (br / 2)) / br;
  closestSpeed = ss.baud_base / ss.custom_divisor;
  if (closestSpeed < br * 95 / 100 || closestSpeed > br * 105 / 100) {
    errno = EINVAL;
    return -1;
  }
  res = ioctl(fd, TIOCSSERIAL, &ss);
  if (res < 0)
    return res;
  if (cfsetispeed(&config, B38400) != 0 || cfsetospeed(&config, B38400) != 0)
    return -1;
  res = tcsetattr(fd, TCSAFLUSH, &config);
  if (res < 0) {
    close(fd);
    return res;
  }
  printf("%d %d\n", closestSpeed, ss.custom_divisor);
  return 0;
}


x86 assembly (clang, -O0):
<SerialPort_setBaudRate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1070 <tcgetattr@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11a2 <SerialPort_setBaudRate+0x32>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1327 <SerialPort_setBaudRate+0x1b7>
mov    -0x8(%rbp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11cf <SerialPort_setBaudRate+0x5f>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1327 <SerialPort_setBaudRate+0x1b7>
mov    -0x20(%rbp),%eax
mov    0x2df7(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    0x2e01(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    -0xc(%rbp),%ecx
shr    $0x1,%ecx
add    %ecx,%eax
xor    %edx,%edx
divl   -0xc(%rbp)
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
xor    %edx,%edx
divl   -0x1c(%rbp)
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
imul   $0x5f,-0xc(%rbp),%eax
mov    $0x64,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jb     124a <SerialPort_setBaudRate+0xda>
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
imul   $0x69,-0xc(%rbp),%eax
mov    $0x64,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jbe    1268 <SerialPort_setBaudRate+0xf8>
mov    0x2d5f(%rip),%rax
mov    (%rax),%ecx
mov    0x2d66(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1327 <SerialPort_setBaudRate+0x1b7>
mov    -0x8(%rbp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1295 <SerialPort_setBaudRate+0x125>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1327 <SerialPort_setBaudRate+0x1b7>
mov    0x2d04(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   1090 <cfsetispeed@plt>
cmp    $0x0,%rax
jne    12cd <SerialPort_setBaudRate+0x15d>
mov    0x2ce8(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   1030 <cfsetospeed@plt>
cmp    $0x0,%rax
je     12d9 <SerialPort_setBaudRate+0x169>
movl   $0xffffffff,-0x4(%rbp)
jmp    1327 <SerialPort_setBaudRate+0x1b7>
mov    -0x8(%rbp),%edi
mov    0x2cf5(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    130e <SerialPort_setBaudRate+0x19e>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1327 <SerialPort_setBaudRate+0x1b7>
mov    -0x24(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xce5(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SerialPort_setBaudRate>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%r15d
mov    %rsp,%rsi
call   1070 <tcgetattr@plt>
test   %eax,%eax
js     129a <SerialPort_setBaudRate+0x12a>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r15d,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
js     129a <SerialPort_setBaudRate+0x12a>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2e25(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
mov    0x10(%rsp),%ecx
mov    %ebp,%eax
shr    %eax
add    %ecx,%eax
xor    %edx,%edx
div    %ebp
mov    %eax,%esi
mov    %eax,0xc(%rsp)
mov    %ecx,%eax
xor    %edx,%edx
div    %esi
mov    %eax,%r14d
imul   $0x5f,%ebp,%eax
imul   $0x51eb851f,%rax,%rax
shr    $0x25,%rax
cmp    %eax,%r14d
jb     120c <SerialPort_setBaudRate+0x9c>
imul   $0x69,%ebp,%eax
imul   $0x51eb851f,%rax,%rax
shr    $0x25,%rax
cmp    %eax,%r14d
jbe    1225 <SerialPort_setBaudRate+0xb5>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    0x2da4(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
jmp    129c <SerialPort_setBaudRate+0x12c>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r15d,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
js     129a <SerialPort_setBaudRate+0x12a>
mov    0x2d5a(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rsp,%rdi
call   1090 <cfsetispeed@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    129c <SerialPort_setBaudRate+0x12c>
mov    0x0(%rbp),%esi
mov    %rsp,%rdi
call   1030 <cfsetospeed@plt>
test   %rax,%rax
jne    129c <SerialPort_setBaudRate+0x12c>
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r15d,%edi
call   1080 <tcsetattr@plt>
test   %eax,%eax
js     12a9 <SerialPort_setBaudRate+0x139>
mov    0xc(%rsp),%edx
lea    0xd72(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
xor    %ebx,%ebx
jmp    129c <SerialPort_setBaudRate+0x12c>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebx
mov    %r15d,%edi
call   1060 <close@plt>
jmp    129c <SerialPort_setBaudRate+0x12c>

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.serial_struct = type { i32, i32, i32 }
@TIOCGSERIAL = global i32 0
@ASYNC_SPD_MASK = global i32 0
@ASYNC_SPD_CUST = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@TIOCSSERIAL = global i32 0
@B38400 = global i32 0
@TCSAFLUSH = global i32 0
@.str = [7 x i8] c"%d %d\0A\00"
define i32 @SerialPort_setBaudRate(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.termios
%7 = alloca %struct.serial_struct
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* %4
%11 = call i32 @tcgetattr(i32 %10, %struct.termios* %6)
store i32 %11, i32* %9
%12 = load i32, i32* %9
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* %9
store i32 %15, i32* %3
br label %91
16: ; preds = %2
%17 = load i32, i32* %4
%18 = load i32, i32* @TIOCGSERIAL
%19 = call i32 @ioctl(i32 %17, i32 %18, %struct.serial_struct* %7)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = load i32, i32* %9
store i32 %23, i32* %3
br label %91
24: ; preds = %16
%25 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 0
%26 = load i32, i32* %25
%27 = load i32, i32* @ASYNC_SPD_MASK
%28 = xor i32 %27, -1
%29 = and i32 %26, %28
%30 = load i32, i32* @ASYNC_SPD_CUST
%31 = or i32 %29, %30
%32 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 0
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 2
%34 = load i32, i32* %33
%35 = load i32, i32* %5
%36 = udiv i32 %35, 2
%37 = add i32 %34, %36
%38 = load i32, i32* %5
%39 = udiv i32 %37, %38
%40 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 1
store i32 %39, i32* %40
%41 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 2
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 1
%44 = load i32, i32* %43
%45 = udiv i32 %42, %44
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = load i32, i32* %5
%48 = mul i32 %47, 95
%49 = udiv i32 %48, 100
%50 = icmp ult i32 %46, %49
br i1 %50, label %57, label %51
51: ; preds = %24
%52 = load i32, i32* %8
%53 = load i32, i32* %5
%54 = mul i32 %53, 105
%55 = udiv i32 %54, 100
%56 = icmp ugt i32 %52, %55
br i1 %56, label %57, label %59
57: ; preds = %51, %24
%58 = load i32, i32* @EINVAL
store i32 %58, i32* @errno
store i32 -1, i32* %3
br label %91
59: ; preds = %51
%60 = load i32, i32* %4
%61 = load i32, i32* @TIOCSSERIAL
%62 = call i32 @ioctl(i32 %60, i32 %61, %struct.serial_struct* %7)
store i32 %62, i32* %9
%63 = load i32, i32* %9
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %59
%66 = load i32, i32* %9
store i32 %66, i32* %3
br label %91
67: ; preds = %59
%68 = load i32, i32* @B38400
%69 = call i64 @cfsetispeed(%struct.termios* %6, i32 %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %75, label %71
71: ; preds = %67
%72 = load i32, i32* @B38400
%73 = call i64 @cfsetospeed(%struct.termios* %6, i32 %72)
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %76
75: ; preds = %71, %67
store i32 -1, i32* %3
br label %91
76: ; preds = %71
%77 = load i32, i32* %4
%78 = load i32, i32* @TCSAFLUSH
%79 = call i32 @tcsetattr(i32 %77, i32 %78, %struct.termios* %6)
store i32 %79, i32* %9
%80 = load i32, i32* %9
%81 = icmp slt i32 %80, 0
br i1 %81, label %82, label %86
82: ; preds = %76
%83 = load i32, i32* %4
%84 = call i32 @close(i32 %83)
%85 = load i32, i32* %9
store i32 %85, i32* %3
br label %91
86: ; preds = %76
%87 = load i32, i32* %8
%88 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %7, i32 0, i32 1
%89 = load i32, i32* %88
%90 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %87, i32 %89)
store i32 0, i32* %3
br label %91
91: ; preds = %86, %82, %75, %65, %57, %22, %14
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.serial_struct = type { i32, i32, i32 }
@TIOCGSERIAL = global i32 0
@ASYNC_SPD_MASK = global i32 0
@ASYNC_SPD_CUST = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@TIOCSSERIAL = global i32 0
@B38400 = global i32 0
@TCSAFLUSH = global i32 0
@.str = [7 x i8] c"%d %d\0A\00"
define i32 @SerialPort_setBaudRate(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.serial_struct
%5 = bitcast %struct.termios* %3 to i8*
%6 = bitcast %struct.serial_struct* %4 to i8*
%7 = call i32 @tcgetattr(i32 %0, %struct.termios* %3)
%8 = icmp slt i32 %7, 0
br i1 %8, label %58, label %9
9: ; preds = %2
%10 = load i32, i32* @TIOCGSERIAL
%11 = call i32 @ioctl(i32 %0, i32 %10, %struct.serial_struct* %4)
%12 = icmp slt i32 %11, 0
br i1 %12, label %58, label %13
13: ; preds = %9
%14 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, 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* %4, i64 0, i32 2
%22 = load i32, i32* %21
%23 = lshr i32 %1, 1
%24 = add i32 %22, %23
%25 = udiv i32 %24, %1
%26 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i64 0, i32 1
store i32 %25, i32* %26
%27 = udiv i32 %22, %25
%28 = mul i32 %1, 95
%29 = udiv i32 %28, 100
%30 = icmp ult i32 %27, %29
br i1 %30, label %35, label %31
31: ; preds = %13
%32 = mul i32 %1, 105
%33 = udiv i32 %32, 100
%34 = icmp ugt i32 %27, %33
br i1 %34, label %35, label %37
35: ; preds = %31, %13
%36 = load i32, i32* @EINVAL
store i32 %36, i32* @errno
br label %58
37: ; preds = %31
%38 = load i32, i32* @TIOCSSERIAL
%39 = call i32 @ioctl(i32 %0, i32 %38, %struct.serial_struct* %4)
%40 = icmp slt i32 %39, 0
br i1 %40, label %58, label %41
41: ; preds = %37
%42 = load i32, i32* @B38400
%43 = call i64 @cfsetispeed(%struct.termios* %3, i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %58
45: ; preds = %41
%46 = load i32, i32* @B38400
%47 = call i64 @cfsetospeed(%struct.termios* %3, i32 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %58
49: ; preds = %45
%50 = load i32, i32* @TCSAFLUSH
%51 = call i32 @tcsetattr(i32 %0, i32 %50, %struct.termios* %3)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @close(i32 %0)
br label %58
55: ; preds = %49
%56 = load i32, i32* %26
%57 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %56)
br label %58
58: ; preds = %41, %45, %37, %9, %2, %55, %53, %35
%59 = phi i32 [ -1, %35 ], [ %51, %53 ], [ 0, %55 ], [ %7, %2 ], [ %11, %9 ], [ %39, %37 ], [ -1, %45 ], [ -1, %41 ]
ret i32 %59
}