#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 {
  float custom_divisor;
  float baud_base;
  int /*<<< orphan*/ flags;
};

/* Variables and functions */
int /*<<< orphan*/ ASYNC_SPD_CUST;
int /*<<< orphan*/ B1000000;
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);
  struct serial_struct ss;
  ioctl(u, TIOCGSERIAL, &ss);
  float BAUDRATE = 1000000;
  ss.flags = ASYNC_SPD_CUST;
  ss.custom_divisor = ss.baud_base / BAUDRATE;
  ioctl(u, TIOCSSERIAL, &ss);
  ioctl(u, TIOCGSERIAL, &ss);
  printf("ss.baud_base:%d\nss.custom_divisor:%d\n", ss.baud_base,
         ss.custom_divisor);
  cfsetispeed(&options, B1000000);
  cfsetospeed(&options, B1000000);
  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);
  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    1334 <InitSerial+0x1a4>
lea    -0x28(%rbp),%rdi
call   1080 <cfmakeraw@plt>
mov    -0x8(%rbp),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <ioctl@plt>
movss  0xde6(%rip),%xmm0
movss  %xmm0,-0x3c(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movss  -0x34(%rbp),%xmm0
divss  -0x3c(%rbp),%xmm0
movss  %xmm0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <ioctl@plt>
mov    -0x8(%rbp),%edi
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <ioctl@plt>
movss  -0x34(%rbp),%xmm0
movss  -0x38(%rbp),%xmm1
lea    0xdb3(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   10b0 <cfsetispeed@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2cbb(%rip),%rax
mov    (%rax),%eax
mov    0x2d1a(%rip),%rcx
or     (%rcx),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cfb(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2cf1(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2c9a(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2c7b(%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    0x2c8e(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x8(%rbp),%edi
mov    0x2cad(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1090 <tcsetattr@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   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x2e36(%rip),%rax
mov    0x2def(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd4(%rip),%rax
or     (%rax),%esi
lea    0xe47(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12e2 <InitSerial+0x152>
lea    0x18(%rsp),%r14
mov    %r14,%rdi
call   1080 <cfmakeraw@plt>
mov    0x2d9f(%rip),%r12
mov    (%r12),%esi
lea    0x8(%rsp),%r15
mov    %ebx,%edi
mov    %r15,%rdx
call   1060 <ioctl@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movss  0xc(%rsp),%xmm0
divss  0xdf1(%rip),%xmm0
movss  %xmm0,0x8(%rsp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1060 <ioctl@plt>
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1060 <ioctl@plt>
movss  0x8(%rsp),%xmm1
movss  0xc(%rsp),%xmm0
lea    0xddf(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d5b(%rip),%r15
mov    (%r15),%esi
mov    %r14,%rdi
call   10b0 <cfsetispeed@plt>
mov    (%r15),%esi
mov    %r14,%rdi
call   1030 <cfsetospeed@plt>
mov    0x2cee(%rip),%rax
mov    0x2d4f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     0x18(%rsp),%ecx
mov    0x2d30(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ecx,%eax
mov    0x2d2b(%rip),%rcx
or     (%rcx),%eax
mov    0x2cda(%rip),%rcx
mov    (%rcx),%ecx
or     %ecx,0x30(%rsp)
mov    0x2cbd(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
and    %eax,%ecx
mov    %ecx,0x18(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x20(%rsp)
mov    0x2cdc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    0x2cfc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1090 <tcsetattr@plt>
jmp    12f7 <InitSerial+0x167>
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
lea    0xd1f(%rip),%rsi
call   1070 <fprintf@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32, i64, i64, i32 }
%struct.serial_struct = type { float, float, i32 }
@.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"
@TIOCGSERIAL = global i32 0
@ASYNC_SPD_CUST = global i32 0
@TIOCSSERIAL = global i32 0
@.str.2 = [38 x i8] c"ss.baud_base:%d\0Ass.custom_divisor:%d\0A\00"
@B1000000 = 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
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 %77
17: ; preds = %0
%18 = call i32 @cfmakeraw(%struct.termios* %3)
%19 = load i32, i32* %2
%20 = load i32, i32* @TIOCGSERIAL
%21 = call i32 @ioctl(i32 %19, i32 %20, %struct.serial_struct* %4)
store float 1.000000e+06, float* %5
%22 = load i32, i32* @ASYNC_SPD_CUST
%23 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i32 0, i32 2
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i32 0, i32 1
%25 = load float, float* %24
%26 = load float, float* %5
%27 = fdiv float %25, %26
%28 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i32 0, i32 0
store float %27, float* %28
%29 = load i32, i32* %2
%30 = load i32, i32* @TIOCSSERIAL
%31 = call i32 @ioctl(i32 %29, i32 %30, %struct.serial_struct* %4)
%32 = load i32, i32* %2
%33 = load i32, i32* @TIOCGSERIAL
%34 = call i32 @ioctl(i32 %32, i32 %33, %struct.serial_struct* %4)
%35 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i32 0, i32 1
%36 = load float, float* %35
%37 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i32 0, i32 0
%38 = load float, float* %37
%39 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), float %36, float %38)
%40 = load i32, i32* @B1000000
%41 = call i32 @cfsetispeed(%struct.termios* %3, i32 %40)
%42 = load i32, i32* @B1000000
%43 = call i32 @cfsetospeed(%struct.termios* %3, i32 %42)
%44 = load i32, i32* @CLOCAL
%45 = load i32, i32* @CREAD
%46 = or i32 %44, %45
%47 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%48 = load i32, i32* %47
%49 = or i32 %48, %46
store i32 %49, i32* %47
%50 = load i32, i32* @CSIZE
%51 = xor i32 %50, -1
%52 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%53 = load i32, i32* %52
%54 = and i32 %53, %51
store i32 %54, i32* %52
%55 = load i32, i32* @CS8
%56 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%57 = load i32, i32* %56
%58 = or i32 %57, %55
store i32 %58, i32* %56
%59 = load i32, i32* @IGNPAR
%60 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 3
%61 = load i32, i32* %60
%62 = or i32 %61, %59
store i32 %62, i32* %60
%63 = load i32, i32* @CSTOPB
%64 = xor i32 %63, -1
%65 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%66 = load i32, i32* %65
%67 = and i32 %66, %64
store i32 %67, i32* %65
%68 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 2
store i64 0, i64* %68
%69 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
store i64 0, i64* %69
%70 = load i32, i32* %2
%71 = load i32, i32* @TCIFLUSH
%72 = call i32 @tcflush(i32 %70, i32 %71)
%73 = load i32, i32* %2
%74 = load i32, i32* @TCSANOW
%75 = call i32 @tcsetattr(i32 %73, i32 %74, %struct.termios* %3)
%76 = load i32, i32* %2
store i32 %76, i32* %1
br label %77
77: ; preds = %17, %14
%78 = load i32, i32* %1
ret i32 %78
}

LLVM IR (-O2):
%struct.termios = type { i32, i64, i64, i32 }
%struct.serial_struct = type { float, float, i32 }
@.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"
@TIOCGSERIAL = global i32 0
@ASYNC_SPD_CUST = global i32 0
@TIOCSSERIAL = global i32 0
@.str.2 = [38 x i8] c"ss.baud_base:%d\0Ass.custom_divisor:%d\0A\00"
@B1000000 = 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
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 %60
13: ; preds = %0
%14 = bitcast %struct.termios* %1 to i8*
%15 = call i32 @cfmakeraw(%struct.termios* %1)
%16 = bitcast %struct.serial_struct* %2 to i8*
%17 = load i32, i32* @TIOCGSERIAL
%18 = call i32 @ioctl(i32 %8, i32 %17, %struct.serial_struct* %2)
%19 = load i32, i32* @ASYNC_SPD_CUST
%20 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %2, i64 0, i32 2
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %2, i64 0, i32 1
%22 = load float, float* %21
%23 = fdiv float %22, 1.000000e+06
%24 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %2, i64 0, i32 0
store float %23, float* %24
%25 = load i32, i32* @TIOCSSERIAL
%26 = call i32 @ioctl(i32 %8, i32 %25, %struct.serial_struct* %2)
%27 = load i32, i32* @TIOCGSERIAL
%28 = call i32 @ioctl(i32 %8, i32 %27, %struct.serial_struct* %2)
%29 = load float, float* %21
%30 = load float, float* %24
%31 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), float %29, float %30)
%32 = load i32, i32* @B1000000
%33 = call i32 @cfsetispeed(%struct.termios* %1, i32 %32)
%34 = load i32, i32* @B1000000
%35 = call i32 @cfsetospeed(%struct.termios* %1, i32 %34)
%36 = load i32, i32* @CLOCAL
%37 = load i32, i32* @CREAD
%38 = or i32 %37, %36
%39 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%40 = load i32, i32* %39
%41 = or i32 %38, %40
%42 = load i32, i32* @CSIZE
%43 = xor i32 %42, -1
%44 = and i32 %41, %43
%45 = load i32, i32* @CS8
%46 = or i32 %45, %44
%47 = load i32, i32* @IGNPAR
%48 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 3
%49 = load i32, i32* %48
%50 = or i32 %49, %47
store i32 %50, i32* %48
%51 = load i32, i32* @CSTOPB
%52 = xor i32 %51, -1
%53 = and i32 %46, %52
store i32 %53, i32* %39
%54 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%55 = bitcast i64* %54 to i8*
%56 = load i32, i32* @TCIFLUSH
%57 = call i32 @tcflush(i32 %8, i32 %56)
%58 = load i32, i32* @TCSANOW
%59 = call i32 @tcsetattr(i32 %8, i32 %58, %struct.termios* %1)
br label %60
60: ; preds = %13, %10
ret i32 %8
}