#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;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
  int /*<<< orphan*/ c_iflag;
};
struct serial_struct {
  int flags;
  float custom_divisor;
  float baud_base;
};

/* Variables and functions */
int ASYNC_SPD_CUST;
int ASYNC_SPD_MASK;
int /*<<< orphan*/ B115200;
int CLOCAL;
int CREAD;
int CS8;
int CSIZE;
int CSTOPB;
int /*<<< orphan*/ IGNPAR;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCGSERIAL;
int /*<<< orphan*/ TIOCSSERIAL;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct serial_struct *);
int open(char *, int);
int /*<<< orphan*/ printf(char *, float, float);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int InitSerial() {
  int u = -1;
  u = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (u == -1) {
    fprintf(stderr, "Error open UART port.\n");
    return -1;
  }
  struct termios options;
  cfmakeraw(&options);
  cfsetispeed(&options, B115200);
  cfsetospeed(&options, B115200);
  options.c_cflag |= (CLOCAL | CREAD);
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;
  options.c_iflag |= IGNPAR;
  options.c_cflag &= ~CSTOPB;
  options.c_oflag = 0;
  options.c_lflag = 0;
  tcflush(u, TCIFLUSH);
  tcsetattr(u, TCSANOW, &options);
  struct serial_struct ss;
  ioctl(u, TIOCGSERIAL, &ss);
  float BAUDRATE = 115200;
  ss.flags = (ss.flags & ~ASYNC_SPD_MASK) | ASYNC_SPD_CUST;
  ss.custom_divisor = ss.baud_base / BAUDRATE;
  printf("ss.baud_base:%d\nss.custom_divisor:%d\n", ss.baud_base,
         ss.custom_divisor);
  ioctl(u, TIOCSSERIAL, &ss);
  return u;
}


x86 assembly (clang, -O0):
<InitSerial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0xffffffff,-0x8(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2de9(%rip),%rax
or     (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
lea    0xe43(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11f4 <InitSerial+0x64>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1330 <InitSerial+0x1a0>
lea    -0x28(%rbp),%rdi
call   1080 <cfmakeraw@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   10b0 <cfsetispeed@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%eax
mov    0x2d8f(%rip),%rcx
or     (%rcx),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d0f(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2cf0(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d0b(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x8(%rbp),%edi
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    -0x8(%rbp),%edi
mov    0x2cc5(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <ioctl@plt>
movss  0xd32(%rip),%xmm0
movss  %xmm0,-0x3c(%rbp)
mov    -0x38(%rbp),%eax
mov    0x2ceb(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    0x2cfd(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
movss  -0x30(%rbp),%xmm0
divss  -0x3c(%rbp),%xmm0
movss  %xmm0,-0x34(%rbp)
movss  -0x30(%rbp),%xmm0
movss  -0x34(%rbp),%xmm1
lea    0xd18(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2cd9(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <ioctl@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<InitSerial>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    0x2e38(%rip),%rax
mov    0x2df1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd6(%rip),%rax
or     (%rax),%esi
lea    0xe49(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12d2 <InitSerial+0x142>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1080 <cfmakeraw@plt>
mov    0x2d99(%rip),%r15
mov    (%r15),%esi
mov    %r14,%rdi
call   10b0 <cfsetispeed@plt>
mov    (%r15),%esi
mov    %r14,%rdi
call   1030 <cfsetospeed@plt>
mov    0x2d5c(%rip),%rax
mov    0x2dbd(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     0x10(%rsp),%ecx
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ecx,%eax
mov    0x2d99(%rip),%rcx
or     (%rcx),%eax
mov    0x2d48(%rip),%rcx
mov    (%rcx),%ecx
or     %ecx,0x28(%rsp)
mov    0x2d2b(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
and    %eax,%ecx
mov    %ecx,0x10(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1090 <tcsetattr@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <ioctl@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
not    %eax
and    (%rsp),%eax
mov    0x2d53(%rip),%rcx
or     (%rcx),%eax
mov    %eax,(%rsp)
movss  0x8(%rsp),%xmm0
movaps %xmm0,%xmm1
divss  0xd55(%rip),%xmm1
movss  %xmm1,0x4(%rsp)
lea    0xd70(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <ioctl@plt>
jmp    12e7 <InitSerial+0x157>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
lea    0xd2f(%rip),%rsi
call   1070 <fprintf@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32, i64, i64, i32 }
%struct.serial_struct = type { i32, float, float }
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"Error open UART port.\0A\00"
@B115200 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@IGNPAR = global i32 0
@CSTOPB = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@TIOCGSERIAL = global i32 0
@ASYNC_SPD_MASK = global i32 0
@ASYNC_SPD_CUST = global i32 0
@.str.2 = [38 x i8] c"ss.baud_base:%d\0Ass.custom_divisor:%d\0A\00"
@TIOCSSERIAL = global i32 0
define i32 @InitSerial() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.termios
%4 = alloca %struct.serial_struct
%5 = alloca float
store i32 -1, i32* %2
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %6, %7
%9 = load i32, i32* @O_NDELAY
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %80
17: ; preds = %0
%18 = call i32 @cfmakeraw(%struct.termios* %3)
%19 = load i32, i32* @B115200
%20 = call i32 @cfsetispeed(%struct.termios* %3, i32 %19)
%21 = load i32, i32* @B115200
%22 = call i32 @cfsetospeed(%struct.termios* %3, i32 %21)
%23 = load i32, i32* @CLOCAL
%24 = load i32, i32* @CREAD
%25 = or i32 %23, %24
%26 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%27 = load i32, i32* %26
%28 = or i32 %27, %25
store i32 %28, i32* %26
%29 = load i32, i32* @CSIZE
%30 = xor i32 %29, -1
%31 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%32 = load i32, i32* %31
%33 = and i32 %32, %30
store i32 %33, i32* %31
%34 = load i32, i32* @CS8
%35 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%36 = load i32, i32* %35
%37 = or i32 %36, %34
store i32 %37, i32* %35
%38 = load i32, i32* @IGNPAR
%39 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 3
%40 = load i32, i32* %39
%41 = or i32 %40, %38
store i32 %41, i32* %39
%42 = load i32, i32* @CSTOPB
%43 = xor i32 %42, -1
%44 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%45 = load i32, i32* %44
%46 = and i32 %45, %43
store i32 %46, i32* %44
%47 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 2
store i64 0, i64* %47
%48 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
store i64 0, i64* %48
%49 = load i32, i32* %2
%50 = load i32, i32* @TCIFLUSH
%51 = call i32 @tcflush(i32 %49, i32 %50)
%52 = load i32, i32* %2
%53 = load i32, i32* @TCSANOW
%54 = call i32 @tcsetattr(i32 %52, i32 %53, %struct.termios* %3)
%55 = load i32, i32* %2
%56 = load i32, i32* @TIOCGSERIAL
%57 = call i32 @ioctl(i32 %55, i32 %56, %struct.serial_struct* %4)
store float 1.152000e+05, float* %5
%58 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i32 0, i32 0
%59 = load i32, i32* %58
%60 = load i32, i32* @ASYNC_SPD_MASK
%61 = xor i32 %60, -1
%62 = and i32 %59, %61
%63 = load i32, i32* @ASYNC_SPD_CUST
%64 = or i32 %62, %63
%65 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i32 0, i32 0
store i32 %64, i32* %65
%66 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i32 0, i32 2
%67 = load float, float* %66
%68 = load float, float* %5
%69 = fdiv float %67, %68
%70 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i32 0, i32 1
store float %69, float* %70
%71 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i32 0, i32 2
%72 = load float, float* %71
%73 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i32 0, i32 1
%74 = load float, float* %73
%75 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), float %72, float %74)
%76 = load i32, i32* %2
%77 = load i32, i32* @TIOCSSERIAL
%78 = call i32 @ioctl(i32 %76, i32 %77, %struct.serial_struct* %4)
%79 = load i32, i32* %2
store i32 %79, i32* %1
br label %80
80: ; preds = %17, %14
%81 = load i32, i32* %1
ret i32 %81
}

LLVM IR (-O2):
%struct.termios = type { i32, i64, i64, i32 }
%struct.serial_struct = type { i32, float, float }
@.str = [13 x i8] c"/dev/ttyAMA0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"Error open UART port.\0A\00"
@B115200 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@IGNPAR = global i32 0
@CSTOPB = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@TIOCGSERIAL = global i32 0
@ASYNC_SPD_MASK = global i32 0
@ASYNC_SPD_CUST = global i32 0
@.str.2 = [38 x i8] c"ss.baud_base:%d\0Ass.custom_divisor:%d\0A\00"
@TIOCSSERIAL = global i32 0
define i32 @InitSerial() {
%1 = alloca %struct.termios
%2 = alloca %struct.serial_struct
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = load i32, i32* @O_NDELAY
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %61
13: ; preds = %0
%14 = bitcast %struct.termios* %1 to i8*
%15 = call i32 @cfmakeraw(%struct.termios* %1)
%16 = load i32, i32* @B115200
%17 = call i32 @cfsetispeed(%struct.termios* %1, i32 %16)
%18 = load i32, i32* @B115200
%19 = call i32 @cfsetospeed(%struct.termios* %1, i32 %18)
%20 = load i32, i32* @CLOCAL
%21 = load i32, i32* @CREAD
%22 = or i32 %21, %20
%23 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%24 = load i32, i32* %23
%25 = or i32 %22, %24
%26 = load i32, i32* @CSIZE
%27 = xor i32 %26, -1
%28 = and i32 %25, %27
%29 = load i32, i32* @CS8
%30 = or i32 %29, %28
%31 = load i32, i32* @IGNPAR
%32 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 3
%33 = load i32, i32* %32
%34 = or i32 %33, %31
store i32 %34, i32* %32
%35 = load i32, i32* @CSTOPB
%36 = xor i32 %35, -1
%37 = and i32 %30, %36
store i32 %37, i32* %23
%38 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%39 = bitcast i64* %38 to i8*
%40 = load i32, i32* @TCIFLUSH
%41 = call i32 @tcflush(i32 %8, i32 %40)
%42 = load i32, i32* @TCSANOW
%43 = call i32 @tcsetattr(i32 %8, i32 %42, %struct.termios* %1)
%44 = bitcast %struct.serial_struct* %2 to i8*
%45 = load i32, i32* @TIOCGSERIAL
%46 = call i32 @ioctl(i32 %8, i32 %45, %struct.serial_struct* %2)
%47 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %2, i64 0, i32 0
%48 = load i32, i32* %47
%49 = load i32, i32* @ASYNC_SPD_MASK
%50 = xor i32 %49, -1
%51 = and i32 %48, %50
%52 = load i32, i32* @ASYNC_SPD_CUST
%53 = or i32 %51, %52
store i32 %53, i32* %47
%54 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %2, i64 0, i32 2
%55 = load float, float* %54
%56 = fdiv float %55, 1.152000e+05
%57 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %2, i64 0, i32 1
store float %56, float* %57
%58 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), float %55, float %56)
%59 = load i32, i32* @TIOCSSERIAL
%60 = call i32 @ioctl(i32 %8, i32 %59, %struct.serial_struct* %2)
br label %61
61: ; preds = %13, %10
ret i32 %8
}