#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 */
typedef int /*<<< orphan*/ tio;
struct termios {
  int c_cflag;
  int c_lflag;
  int *c_cc;
  scalar_t__ c_oflag;
  scalar_t__ c_iflag;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ B115200;
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int ECHO;
int ECHOE;
int ICANON;
int ISIG;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ initNetworkStat();
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_serial(char *port, int oflags) {
  struct termios tio;
  fd_set rdset;
  unsigned char c = 0;
  memset(&tio, 0, sizeof(tio));
  tio.c_iflag = 0;
  tio.c_oflag = 0;
  tio.c_cflag &= CRTSCTS | CS8 | CLOCAL | CREAD;
  tio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  tio.c_cc[VMIN] = 0;
  tio.c_cc[VTIME] = 10;
  int fd;
  fd = open(port, oflags);
  if (fd < 0) {
    return fd;
  }
  cfsetospeed(&tio, B115200);
  cfsetispeed(&tio, B115200);
  tcsetattr(fd, TCSANOW, &tio);
  initNetworkStat();
  return fd;
}


x86 assembly (clang, -O0):
<open_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movb   $0x0,-0x3d(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    0x2e1d(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd4(%rip),%rcx
or     (%rcx),%eax
mov    0x2e13(%rip),%rcx
or     (%rcx),%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    0x2e13(%rip),%rcx
or     (%rcx),%eax
mov    0x2e02(%rip),%rcx
or     (%rcx),%eax
mov    0x2df1(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2dc3(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
mov    0x2d96(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xa,(%rax,%rcx,4)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1238 <open_serial+0xd8>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    127e <open_serial+0x11e>
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdi
call   1080 <cfsetispeed@plt>
mov    -0x44(%rbp),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    $0x0,%al
call   1050 <initNetworkStat@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_serial>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e05(%rip),%rax
mov    0x2e36(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xorps  %xmm0,%xmm0
mov    0x2de8(%rip),%rax
or     (%rax),%ecx
movups %xmm0,0x10(%rsp)
mov    0x2e22(%rip),%rax
or     (%rax),%ecx
and    %ecx,(%rsp)
mov    0x2de6(%rip),%rax
mov    0x2e27(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e14(%rip),%rax
or     (%rax),%ecx
mov    0x2e03(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x4(%rsp)
mov    0x8(%rsp),%rax
mov    0x2dd7(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2dae(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xa,(%rax,%rcx,4)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1246 <open_serial+0xe6>
mov    0x2d95(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1030 <cfsetospeed@plt>
mov    (%r14),%esi
mov    %rbp,%rdi
call   1080 <cfsetispeed@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1060 <tcsetattr@plt>
xor    %eax,%eax
call   1050 <initNetworkStat@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@CRTSCTS = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ISIG = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@B115200 = global i32 0
@TCSANOW = global i32 0
define i32 @open_serial(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.termios
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8 0, i8* %8
%10 = call i32 @memset(%struct.termios* %6, i32 0, i32 32)
%11 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 4
store i64 0, i64* %11
%12 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
store i64 0, i64* %12
%13 = load i32, i32* @CRTSCTS
%14 = load i32, i32* @CS8
%15 = or i32 %13, %14
%16 = load i32, i32* @CLOCAL
%17 = or i32 %15, %16
%18 = load i32, i32* @CREAD
%19 = or i32 %17, %18
%20 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%21 = load i32, i32* %20
%22 = and i32 %21, %19
store i32 %22, i32* %20
%23 = load i32, i32* @ICANON
%24 = load i32, i32* @ECHO
%25 = or i32 %23, %24
%26 = load i32, i32* @ECHOE
%27 = or i32 %25, %26
%28 = load i32, i32* @ISIG
%29 = or i32 %27, %28
%30 = xor i32 %29, -1
%31 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%32 = load i32, i32* %31
%33 = and i32 %32, %30
store i32 %33, i32* %31
%34 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%35 = load i32*, i32** %34
%36 = load i64, i64* @VMIN
%37 = getelementptr inbounds i32, i32* %35, i64 %36
store i32 0, i32* %37
%38 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%39 = load i32*, i32** %38
%40 = load i64, i64* @VTIME
%41 = getelementptr inbounds i32, i32* %39, i64 %40
store i32 10, i32* %41
%42 = load i8*, i8** %4
%43 = load i32, i32* %5
%44 = call i32 @open(i8* %42, i32 %43)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %2
%48 = load i32, i32* %9
store i32 %48, i32* %3
br label %59
49: ; preds = %2
%50 = load i32, i32* @B115200
%51 = call i32 @cfsetospeed(%struct.termios* %6, i32 %50)
%52 = load i32, i32* @B115200
%53 = call i32 @cfsetispeed(%struct.termios* %6, i32 %52)
%54 = load i32, i32* %9
%55 = load i32, i32* @TCSANOW
%56 = call i32 @tcsetattr(i32 %54, i32 %55, %struct.termios* %6)
%57 = call i32 (...) @initNetworkStat()
%58 = load i32, i32* %9
store i32 %58, i32* %3
br label %59
59: ; preds = %49, %47
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@CRTSCTS = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ISIG = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@B115200 = global i32 0
@TCSANOW = global i32 0
define i32 @open_serial(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = call i32 @memset(%struct.termios* %3, i32 0, i32 32)
%6 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%7 = bitcast i64* %6 to i8*
%8 = load i32, i32* @CRTSCTS
%9 = load i32, i32* @CS8
%10 = or i32 %9, %8
%11 = load i32, i32* @CLOCAL
%12 = or i32 %10, %11
%13 = load i32, i32* @CREAD
%14 = or i32 %12, %13
%15 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = and i32 %16, %14
store i32 %17, i32* %15
%18 = load i32, i32* @ICANON
%19 = load i32, i32* @ECHO
%20 = or i32 %19, %18
%21 = load i32, i32* @ECHOE
%22 = or i32 %20, %21
%23 = load i32, i32* @ISIG
%24 = or i32 %22, %23
%25 = xor i32 %24, -1
%26 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%27 = load i32, i32* %26
%28 = and i32 %27, %25
store i32 %28, i32* %26
%29 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%30 = load i32*, i32** %29
%31 = load i64, i64* @VMIN
%32 = getelementptr inbounds i32, i32* %30, i64 %31
store i32 0, i32* %32
%33 = load i64, i64* @VTIME
%34 = getelementptr inbounds i32, i32* %30, i64 %33
store i32 10, i32* %34
%35 = call i32 @open(i8* %0, i32 %1)
%36 = icmp slt i32 %35, 0
br i1 %36, label %45, label %37
37: ; preds = %2
%38 = load i32, i32* @B115200
%39 = call i32 @cfsetospeed(%struct.termios* %3, i32 %38)
%40 = load i32, i32* @B115200
%41 = call i32 @cfsetispeed(%struct.termios* %3, i32 %40)
%42 = load i32, i32* @TCSANOW
%43 = call i32 @tcsetattr(i32 %35, i32 %42, %struct.termios* %3)
%44 = call i32 (...) @initNetworkStat()
br label %45
45: ; preds = %2, %37
ret i32 %35
}