#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_cc;
  scalar_t__ c_oflag;
  int /*<<< orphan*/ c_iflag;
  scalar_t__ c_lflag;
};

/* Variables and functions */
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ F_SETFL;
int HUPCL;
int /*<<< orphan*/ IGNPAR;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int init_uart(int baud_rate) {
  int fd_uart;
  struct termios tio;
  char dev[20] = {0};
  if ((fd_uart = open("/dev/ttyS1", O_RDWR | O_NDELAY | O_NOCTTY)) < 0) {
    printf("could not open %s\n", dev);
    return -1;
  } else {
    printf("open %s success\n", dev);
  }
  tio.c_cflag = baud_rate | CS8 | CREAD | CLOCAL;
  tio.c_cflag &= ~HUPCL;
  tio.c_lflag = 0;
  tio.c_iflag = IGNPAR;
  tio.c_oflag = 0;
  tio.c_cc[VTIME] = 0;
  tio.c_cc[VMIN] = 0;
  tcflush(fd_uart, TCIFLUSH);
  tcsetattr(fd_uart, TCSANOW, &tio);
  fcntl(fd_uart, F_SETFL, 0);
  return fd_uart;
}


x86 assembly (clang, -O0):
<init_uart>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
or     (%rax),%esi
mov    0x2e1c(%rip),%rax
or     (%rax),%esi
lea    0xe63(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11ca <init_uart+0x6a>
lea    -0x50(%rbp),%rsi
lea    0xe52(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <init_uart+0x134>
lea    -0x50(%rbp),%rsi
lea    0xe49(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
mov    0x2dec(%rip),%rcx
or     (%rcx),%eax
mov    0x2deb(%rip),%rcx
or     (%rcx),%eax
mov    0x2d82(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d64(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x30(%rbp),%rax
mov    0x2d7e(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0xc(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0xc(%rbp),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    -0xc(%rbp),%edi
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_uart>:
sub    $0x18,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x2eb6(%rip),%rax
mov    0x2e97(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
movl   $0x0,0x10(%rsp)
mov    0x2e8c(%rip),%rax
or     (%rax),%esi
lea    0xeab(%rip),%rdi
call   1040 <open@plt>
test   %eax,%eax
jns    1177 <init_uart+0x57>
lea    0xea6(%rip),%rdi
mov    %rsp,%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret
lea    0xea0(%rip),%rdi
mov    %rsp,%rsi
call   1030 <printf@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i64*, i64, i32, i64 }
@.str = [11 x i8] c"/dev/ttyS1\00"
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [19 x i8] c"could not open %s\0A\00"
@.str.2 = [17 x i8] c"open %s success\0A\00"
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@HUPCL = global i32 0
@IGNPAR = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@F_SETFL = global i32 0
define i32 @init_uart(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.termios
%6 = alloca [20 x i8]
store i32 %0, i32* %3
%7 = bitcast [20 x i8]* %6 to i8*
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NDELAY
%10 = or i32 %8, %9
%11 = load i32, i32* @O_NOCTTY
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%17 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %16)
store i32 -1, i32* %2
br label %57
18: ; preds = %1
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%20 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %18
%22 = load i32, i32* %3
%23 = load i32, i32* @CS8
%24 = or i32 %22, %23
%25 = load i32, i32* @CREAD
%26 = or i32 %24, %25
%27 = load i32, i32* @CLOCAL
%28 = or i32 %26, %27
%29 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @HUPCL
%31 = xor i32 %30, -1
%32 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%33 = load i32, i32* %32
%34 = and i32 %33, %31
store i32 %34, i32* %32
%35 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 4
store i64 0, i64* %35
%36 = load i32, i32* @IGNPAR
%37 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 3
store i32 %36, i32* %37
%38 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 2
store i64 0, i64* %38
%39 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%40 = load i64*, i64** %39
%41 = load i64, i64* @VTIME
%42 = getelementptr inbounds i64, i64* %40, i64 %41
store i64 0, i64* %42
%43 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%44 = load i64*, i64** %43
%45 = load i64, i64* @VMIN
%46 = getelementptr inbounds i64, i64* %44, i64 %45
store i64 0, i64* %46
%47 = load i32, i32* %4
%48 = load i32, i32* @TCIFLUSH
%49 = call i32 @tcflush(i32 %47, i32 %48)
%50 = load i32, i32* %4
%51 = load i32, i32* @TCSANOW
%52 = call i32 @tcsetattr(i32 %50, i32 %51, %struct.termios* %5)
%53 = load i32, i32* %4
%54 = load i32, i32* @F_SETFL
%55 = call i32 @fcntl(i32 %53, i32 %54, i32 0)
%56 = load i32, i32* %4
store i32 %56, i32* %2
br label %57
57: ; preds = %21, %15
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/ttyS1\00"
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [19 x i8] c"could not open %s\0A\00"
@.str.2 = [17 x i8] c"open %s success\0A\00"
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@HUPCL = global i32 0
@IGNPAR = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@F_SETFL = global i32 0
define i32 @init_uart(i32 %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NDELAY
%6 = or i32 %5, %4
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %3)
ret i32 -1
13: ; preds = %1
%14 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %3)
unreachable
}