#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_cc;
};

/* Variables and functions */
int /*<<< orphan*/ B9600;
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int CSIZE;
int CSTOPB;
int IXANY;
int IXOFF;
int IXON;
int PARENB;
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*/ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int initSerialPort(int fd) {
  struct termios options;
  tcgetattr(fd, &options);
  cfsetispeed(&options, B9600);
  cfsetospeed(&options, B9600);
  options.c_cflag |= (CLOCAL | CREAD);
  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;
  options.c_iflag &= ~(IXON | IXOFF | IXANY);
  options.c_cflag |= CRTSCTS;
  options.c_cc[VMIN] = 1;
  options.c_cc[VTIME] = 1;
  if (tcsetattr(fd, TCSANOW, &options) < 0) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<initSerialPort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <tcgetattr@plt>
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1060 <cfsetispeed@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    0x2e35(%rip),%rcx
or     (%rcx),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    0x2e00(%rip),%rcx
or     (%rcx),%eax
mov    0x2da7(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d9a(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    0x2d65(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x8(%rbp),%edi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1260 <initSerialPort+0x120>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <initSerialPort+0x127>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initSerialPort>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    %rsp,%r14
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
mov    0x2e6c(%rip),%rbx
mov    (%rbx),%esi
mov    %r14,%rdi
call   1060 <cfsetispeed@plt>
mov    (%rbx),%esi
mov    %r14,%rdi
call   1030 <cfsetospeed@plt>
mov    0x2df9(%rip),%rax
mov    0x2e42(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     (%rsp),%ecx
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    0x2de3(%rip),%rdx
or     (%rdx),%eax
mov    0x2e0a(%rip),%rdx
or     (%rdx),%eax
not    %eax
and    %ecx,%eax
mov    0x2e0d(%rip),%rcx
or     (%rcx),%eax
mov    0x2e2c(%rip),%rcx
mov    0x2e2d(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2dd2(%rip),%rcx
or     (%rcx),%edx
not    %edx
and    %edx,0x4(%rsp)
mov    0x2dab(%rip),%rcx
or     (%rcx),%eax
mov    %eax,(%rsp)
mov    0x8(%rsp),%rax
mov    0x2dca(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d99(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
sar    $0x3f,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32* }
@B9600 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@CRTSCTS = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
define i32 @initSerialPort(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = call i32 @tcgetattr(i32 %0, %struct.termios* %2)
%5 = load i32, i32* @B9600
%6 = call i32 @cfsetispeed(%struct.termios* %2, i32 %5)
%7 = load i32, i32* @B9600
%8 = call i32 @cfsetospeed(%struct.termios* %2, i32 %7)
%9 = load i32, i32* @CLOCAL
%10 = load i32, i32* @CREAD
%11 = or i32 %10, %9
%12 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = or i32 %11, %13
%15 = load i32, i32* @PARENB
%16 = load i32, i32* @CSTOPB
%17 = or i32 %15, %16
%18 = load i32, i32* @CSIZE
%19 = or i32 %17, %18
%20 = xor i32 %19, -1
%21 = and i32 %14, %20
%22 = load i32, i32* @CS8
%23 = or i32 %22, %21
%24 = load i32, i32* @IXON
%25 = load i32, i32* @IXOFF
%26 = or i32 %25, %24
%27 = load i32, i32* @IXANY
%28 = or i32 %26, %27
%29 = xor i32 %28, -1
%30 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%31 = load i32, i32* %30
%32 = and i32 %31, %29
store i32 %32, i32* %30
%33 = load i32, i32* @CRTSCTS
%34 = or i32 %33, %23
store i32 %34, i32* %12
%35 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%36 = load i32*, i32** %35
%37 = load i64, i64* @VMIN
%38 = getelementptr inbounds i32, i32* %36, i64 %37
store i32 1, i32* %38
%39 = load i64, i64* @VTIME
%40 = getelementptr inbounds i32, i32* %36, i64 %39
store i32 1, i32* %40
%41 = load i32, i32* @TCSANOW
%42 = call i64 @tcsetattr(i32 %0, i32 %41, %struct.termios* %2)
%43 = ashr i64 %42, 63
%44 = trunc i64 %43 to i32
ret i32 %44
}