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

/* Variables and functions */
int B115200;
int B19200;
int B38400;
int B4800;
int B57600;
int B9600;
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int CSIZE;
int CSTOPB;
int ECHO;
int ECHOE;
int ICANON;
int ISIG;
int IXANY;
int IXOFF;
int IXON;
int /*<<< orphan*/ OPOST;
int O_NONBLOCK;
int O_RDWR;
int PARENB;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int open(char const *, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int serial_init(const char *serialport, int baud) {
  struct termios toptions;
  int fd;
  if (serialport == NULL) {
    perror("serialport is null");
    return -1;
  }
  fd = open(serialport, O_RDWR | O_NONBLOCK);
  if (fd == -1) {
    perror("serial_init: Unable to open port ");
    return -1;
  }
  if (tcgetattr(fd, &toptions) < 0) {
    perror("serial_init: Couldn't get term attributes");
    return -1;
  }
  speed_t brate = baud;
  switch (baud) {
  case 4800:
    brate = B4800;
    break;
  case 9600:
    brate = B9600;
    break;
  case 19200:
    brate = B19200;
    break;
  case 38400:
    brate = B38400;
    break;
  case 57600:
    brate = B57600;
    break;
  case 115200:
    brate = B115200;
    break;
  default:
    perror("baud not supported");
    return -1;
  }
  cfsetispeed(&toptions, brate);
  cfsetospeed(&toptions, brate);
  toptions.c_cflag &= ~PARENB;
  toptions.c_cflag &= ~CSTOPB;
  toptions.c_cflag &= ~CSIZE;
  toptions.c_cflag |= CS8;
  toptions.c_cflag &= ~CRTSCTS;
  toptions.c_cflag |= CREAD | CLOCAL;
  toptions.c_iflag &= ~(IXON | IXOFF | IXANY);
  toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  toptions.c_oflag &= ~OPOST;
  toptions.c_cc[VMIN] = 0;
  toptions.c_cc[VTIME] = 0;
  tcsetattr(fd, TCSANOW, &toptions);
  if (tcsetattr(fd, TCSAFLUSH, &toptions) < 0) {
    perror("init_serial: Couldn't set term attributes");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<serial_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1192 <serial_init+0x32>
lea    0xe7f(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1461 <serial_init+0x301>
mov    -0x10(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    11d2 <serial_init+0x72>
lea    0xe52(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1461 <serial_init+0x301>
mov    -0x3c(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1040 <tcgetattr@plt>
cmp    $0x0,%rax
jge    1200 <serial_init+0xa0>
lea    0xe46(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1461 <serial_init+0x301>
mov    -0x14(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
sub    $0x12c0,%eax
je     127b <serial_init+0x11b>
jmp    121c <serial_init+0xbc>
mov    -0x44(%rbp),%eax
sub    $0x2580,%eax
je     128c <serial_init+0x12c>
jmp    122f <serial_init+0xcf>
mov    -0x44(%rbp),%eax
sub    $0x4b00,%eax
je     129d <serial_init+0x13d>
jmp    1242 <serial_init+0xe2>
mov    -0x44(%rbp),%eax
sub    $0x9600,%eax
je     12ae <serial_init+0x14e>
jmp    1255 <serial_init+0xf5>
mov    -0x44(%rbp),%eax
sub    $0xe100,%eax
je     12bf <serial_init+0x15f>
jmp    1268 <serial_init+0x108>
mov    -0x44(%rbp),%eax
sub    $0x1c200,%eax
je     12d0 <serial_init+0x170>
jmp    12e1 <serial_init+0x181>
mov    0x2d06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
jmp    12f9 <serial_init+0x199>
mov    0x2ced(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
jmp    12f9 <serial_init+0x199>
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
jmp    12f9 <serial_init+0x199>
mov    0x2c53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
jmp    12f9 <serial_init+0x199>
mov    0x2c92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
jmp    12f9 <serial_init+0x199>
mov    0x2c69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
jmp    12f9 <serial_init+0x199>
lea    0xd77(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1461 <serial_init+0x301>
mov    -0x40(%rbp),%esi
lea    -0x38(%rbp),%rdi
call   1070 <cfsetispeed@plt>
mov    -0x40(%rbp),%esi
lea    -0x38(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2c98(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2bee(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2c24(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2c22(%rip),%rax
mov    (%rax),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2bc3(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2c09(%rip),%rax
mov    (%rax),%eax
mov    0x2b98(%rip),%rcx
or     (%rcx),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2c31(%rip),%rax
mov    (%rax),%eax
mov    0x2c40(%rip),%rcx
or     (%rcx),%eax
mov    0x2baf(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2b85(%rip),%rax
mov    (%rax),%eax
mov    0x2bec(%rip),%rcx
or     (%rcx),%eax
mov    0x2bdb(%rip),%rcx
or     (%rcx),%eax
mov    0x2bca(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2be8(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2b7a(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x28(%rbp),%rax
mov    0x2b34(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x3c(%rbp),%edi
mov    0x2b8f(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    -0x3c(%rbp),%edi
mov    0x2baa(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
jge    145b <serial_init+0x2fb>
lea    0xc28(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1461 <serial_init+0x301>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_init>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
test   %rdi,%rdi
je     1337 <serial_init+0x1d7>
mov    %esi,%ebp
mov    0x2e66(%rip),%rax
mov    0x2e47(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1340 <serial_init+0x1e0>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1040 <tcgetattr@plt>
test   %rax,%rax
js     1349 <serial_init+0x1e9>
cmp    $0x95ff,%ebp
jg     11d5 <serial_init+0x75>
cmp    $0x12c0,%ebp
je     11fa <serial_init+0x9a>
cmp    $0x2580,%ebp
je     1203 <serial_init+0xa3>
cmp    $0x4b00,%ebp
jne    1365 <serial_init+0x205>
mov    0x2e1d(%rip),%rax
jmp    121c <serial_init+0xbc>
cmp    $0x9600,%ebp
je     120c <serial_init+0xac>
cmp    $0xe100,%ebp
je     1215 <serial_init+0xb5>
cmp    $0x1c200,%ebp
jne    1365 <serial_init+0x205>
mov    0x2d48(%rip),%rax
jmp    121c <serial_init+0xbc>
mov    0x2d87(%rip),%rax
jmp    121c <serial_init+0xbc>
mov    0x2d76(%rip),%rax
jmp    121c <serial_init+0xbc>
mov    0x2cf5(%rip),%rax
jmp    121c <serial_init+0xbc>
mov    0x2d3c(%rip),%rax
mov    (%rax),%ebp
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebp,%esi
call   1070 <cfsetispeed@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <cfsetospeed@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    0x2cd3(%rip),%rcx
or     (%rcx),%eax
mov    0x2d12(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    (%rsp),%eax
mov    0x2d14(%rip),%rcx
or     (%rcx),%eax
mov    0x2cbb(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
and    %eax,%ecx
mov    0x2d06(%rip),%rax
mov    0x2c97(%rip),%rdx
or     (%rax),%ecx
or     (%rdx),%ecx
mov    %ecx,(%rsp)
mov    0x2d31(%rip),%rax
mov    0x2d42(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2caf(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x4(%rsp)
mov    0x2c88(%rip),%rax
mov    0x2cf1(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2cde(%rip),%rax
or     (%rax),%ecx
mov    0x2ccd(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    0x2cee(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2c82(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    0x2c40(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    0x2c9e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
mov    0x2cbb(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
test   %rax,%rax
jns    135a <serial_init+0x1fa>
lea    0xd3d(%rip),%rdi
jmp    1350 <serial_init+0x1f0>
lea    0xcc2(%rip),%rdi
jmp    1350 <serial_init+0x1f0>
lea    0xccc(%rip),%rdi
jmp    1350 <serial_init+0x1f0>
lea    0xce5(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xcf3(%rip),%rdi
jmp    1350 <serial_init+0x1f0>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32, i64*, i32 }
@.str = [19 x i8] c"serialport is null\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [34 x i8] c"serial_init: Unable to open port \00"
@.str.2 = [42 x i8] c"serial_init: Couldn't get term attributes\00"
@B4800 = global i32 0
@B9600 = global i32 0
@B19200 = global i32 0
@B38400 = global i32 0
@B57600 = global i32 0
@B115200 = global i32 0
@.str.3 = [19 x i8] c"baud not supported\00"
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@CRTSCTS = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ISIG = global i32 0
@OPOST = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@TCSAFLUSH = global i32 0
@.str.4 = [42 x i8] c"init_serial: Couldn't set term attributes\00"
define i32 @serial_init(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.termios
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %125
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_NONBLOCK
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %14, i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %125
23: ; preds = %13
%24 = load i32, i32* %7
%25 = call i64 @tcgetattr(i32 %24, %struct.termios* %6)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %125
29: ; preds = %23
%30 = load i32, i32* %5
store i32 %30, i32* %8
%31 = load i32, i32* %5
switch i32 %31, label %44 [
i32 4800, label %32
i32 9600, label %34
i32 19200, label %36
i32 38400, label %38
i32 57600, label %40
i32 115200, label %42
]
32: ; preds = %29
%33 = load i32, i32* @B4800
store i32 %33, i32* %8
br label %46
34: ; preds = %29
%35 = load i32, i32* @B9600
store i32 %35, i32* %8
br label %46
36: ; preds = %29
%37 = load i32, i32* @B19200
store i32 %37, i32* %8
br label %46
38: ; preds = %29
%39 = load i32, i32* @B38400
store i32 %39, i32* %8
br label %46
40: ; preds = %29
%41 = load i32, i32* @B57600
store i32 %41, i32* %8
br label %46
42: ; preds = %29
%43 = load i32, i32* @B115200
store i32 %43, i32* %8
br label %46
44: ; preds = %29
%45 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %125
46: ; preds = %42, %40, %38, %36, %34, %32
%47 = load i32, i32* %8
%48 = call i32 @cfsetispeed(%struct.termios* %6, i32 %47)
%49 = load i32, i32* %8
%50 = call i32 @cfsetospeed(%struct.termios* %6, i32 %49)
%51 = load i32, i32* @PARENB
%52 = xor i32 %51, -1
%53 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%54 = load i32, i32* %53
%55 = and i32 %54, %52
store i32 %55, i32* %53
%56 = load i32, i32* @CSTOPB
%57 = xor i32 %56, -1
%58 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%59 = load i32, i32* %58
%60 = and i32 %59, %57
store i32 %60, i32* %58
%61 = load i32, i32* @CSIZE
%62 = xor i32 %61, -1
%63 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%64 = load i32, i32* %63
%65 = and i32 %64, %62
store i32 %65, i32* %63
%66 = load i32, i32* @CS8
%67 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%68 = load i32, i32* %67
%69 = or i32 %68, %66
store i32 %69, i32* %67
%70 = load i32, i32* @CRTSCTS
%71 = xor i32 %70, -1
%72 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%73 = load i32, i32* %72
%74 = and i32 %73, %71
store i32 %74, i32* %72
%75 = load i32, i32* @CREAD
%76 = load i32, i32* @CLOCAL
%77 = or i32 %75, %76
%78 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%79 = load i32, i32* %78
%80 = or i32 %79, %77
store i32 %80, i32* %78
%81 = load i32, i32* @IXON
%82 = load i32, i32* @IXOFF
%83 = or i32 %81, %82
%84 = load i32, i32* @IXANY
%85 = or i32 %83, %84
%86 = xor i32 %85, -1
%87 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%88 = load i32, i32* %87
%89 = and i32 %88, %86
store i32 %89, i32* %87
%90 = load i32, i32* @ICANON
%91 = load i32, i32* @ECHO
%92 = or i32 %90, %91
%93 = load i32, i32* @ECHOE
%94 = or i32 %92, %93
%95 = load i32, i32* @ISIG
%96 = or i32 %94, %95
%97 = xor i32 %96, -1
%98 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%99 = load i32, i32* %98
%100 = and i32 %99, %97
store i32 %100, i32* %98
%101 = load i32, i32* @OPOST
%102 = xor i32 %101, -1
%103 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 4
%104 = load i32, i32* %103
%105 = and i32 %104, %102
store i32 %105, i32* %103
%106 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
%107 = load i64*, i64** %106
%108 = load i64, i64* @VMIN
%109 = getelementptr inbounds i64, i64* %107, i64 %108
store i64 0, i64* %109
%110 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
%111 = load i64*, i64** %110
%112 = load i64, i64* @VTIME
%113 = getelementptr inbounds i64, i64* %111, i64 %112
store i64 0, i64* %113
%114 = load i32, i32* %7
%115 = load i32, i32* @TCSANOW
%116 = call i64 @tcsetattr(i32 %114, i32 %115, %struct.termios* %6)
%117 = load i32, i32* %7
%118 = load i32, i32* @TCSAFLUSH
%119 = call i64 @tcsetattr(i32 %117, i32 %118, %struct.termios* %6)
%120 = icmp slt i64 %119, 0
br i1 %120, label %121, label %123
121: ; preds = %46
%122 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %125
123: ; preds = %46
%124 = load i32, i32* %7
store i32 %124, i32* %3
br label %125
125: ; preds = %123, %121, %44, %27, %21, %11
%126 = load i32, i32* %3
ret i32 %126
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32, i64*, i32 }
@.str = [19 x i8] c"serialport is null\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [34 x i8] c"serial_init: Unable to open port \00"
@.str.2 = [42 x i8] c"serial_init: Couldn't get term attributes\00"
@B4800 = global i32 0
@B9600 = global i32 0
@B19200 = global i32 0
@B38400 = global i32 0
@B57600 = global i32 0
@B115200 = global i32 0
@.str.3 = [19 x i8] c"baud not supported\00"
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@CRTSCTS = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ISIG = global i32 0
@OPOST = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@TCSAFLUSH = global i32 0
@.str.4 = [42 x i8] c"init_serial: Couldn't set term attributes\00"
define i32 @serial_init(i8* %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %90
8: ; preds = %2
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %0, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %90
16: ; preds = %8
%17 = call i64 @tcgetattr(i32 %12, %struct.termios* %3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %90
21: ; preds = %16
switch i32 %1, label %27 [
i32 4800, label %29
i32 9600, label %22
i32 19200, label %23
i32 38400, label %24
i32 57600, label %25
i32 115200, label %26
]
22: ; preds = %21
br label %29
23: ; preds = %21
br label %29
24: ; preds = %21
br label %29
25: ; preds = %21
br label %29
26: ; preds = %21
br label %29
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %90
29: ; preds = %21, %26, %25, %24, %23, %22
%30 = phi i32* [ @B115200, %26 ], [ @B57600, %25 ], [ @B38400, %24 ], [ @B19200, %23 ], [ @B9600, %22 ], [ @B4800, %21 ]
%31 = load i32, i32* %30
%32 = call i32 @cfsetispeed(%struct.termios* %3, i32 %31)
%33 = call i32 @cfsetospeed(%struct.termios* %3, i32 %31)
%34 = load i32, i32* @PARENB
%35 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%36 = load i32, i32* %35
%37 = load i32, i32* @CSTOPB
%38 = or i32 %34, %37
%39 = load i32, i32* @CSIZE
%40 = or i32 %38, %39
%41 = xor i32 %40, -1
%42 = and i32 %36, %41
%43 = load i32, i32* @CS8
%44 = or i32 %43, %42
%45 = load i32, i32* @CRTSCTS
%46 = xor i32 %45, -1
%47 = and i32 %44, %46
%48 = load i32, i32* @CREAD
%49 = load i32, i32* @CLOCAL
%50 = or i32 %48, %47
%51 = or i32 %50, %49
store i32 %51, i32* %35
%52 = load i32, i32* @IXON
%53 = load i32, i32* @IXOFF
%54 = or i32 %53, %52
%55 = load i32, i32* @IXANY
%56 = or i32 %54, %55
%57 = xor i32 %56, -1
%58 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%59 = load i32, i32* %58
%60 = and i32 %59, %57
store i32 %60, i32* %58
%61 = load i32, i32* @ICANON
%62 = load i32, i32* @ECHO
%63 = or i32 %62, %61
%64 = load i32, i32* @ECHOE
%65 = or i32 %63, %64
%66 = load i32, i32* @ISIG
%67 = or i32 %65, %66
%68 = xor i32 %67, -1
%69 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%70 = load i32, i32* %69
%71 = and i32 %70, %68
store i32 %71, i32* %69
%72 = load i32, i32* @OPOST
%73 = xor i32 %72, -1
%74 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 4
%75 = load i32, i32* %74
%76 = and i32 %75, %73
store i32 %76, i32* %74
%77 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%78 = load i64*, i64** %77
%79 = load i64, i64* @VMIN
%80 = getelementptr inbounds i64, i64* %78, i64 %79
store i64 0, i64* %80
%81 = load i64, i64* @VTIME
%82 = getelementptr inbounds i64, i64* %78, i64 %81
store i64 0, i64* %82
%83 = load i32, i32* @TCSANOW
%84 = call i64 @tcsetattr(i32 %12, i32 %83, %struct.termios* %3)
%85 = load i32, i32* @TCSAFLUSH
%86 = call i64 @tcsetattr(i32 %12, i32 %85, %struct.termios* %3)
%87 = icmp slt i64 %86, 0
br i1 %87, label %88, label %90
88: ; preds = %29
%89 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
br label %90
90: ; preds = %27, %88, %29, %19, %14, %6
%91 = phi i32 [ -1, %6 ], [ -1, %14 ], [ -1, %19 ], [ -1, %27 ], [ -1, %88 ], [ %12, %29 ]
ret i32 %91
}