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

/* Variables and functions */
int /*<<< orphan*/ 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_NDELAY;
int O_NOCTTY;
int O_RDWR;
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 open(char const *, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int serialport_init(const char *serialport) {
  struct termios toptions;
  int fd;
  fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    perror("init_serialport: Unable to open port ");
    return -1;
  }
  if (tcgetattr(fd, &toptions) < 0) {
    perror("init_serialport: Couldn't get term attributes");
    return -1;
  }
  speed_t brate = B9600;
  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] = 20;
  if (tcsetattr(fd, TCSANOW, &toptions) < 0) {
    perror("init_serialport: Couldn't set term attributes");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<serialport_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2de8(%rip),%rax
or     (%rax),%esi
mov    0x2dd7(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11b5 <serialport_init+0x55>
lea    0xe5c(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <serialport_init+0x1e0>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1040 <tcgetattr@plt>
cmp    $0x0,%rax
jge    11e3 <serialport_init+0x83>
lea    0xe54(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <serialport_init+0x1e0>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%esi
lea    -0x30(%rbp),%rdi
call   1070 <cfsetispeed@plt>
mov    -0x38(%rbp),%esi
lea    -0x30(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d18(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d4e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2ced(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d33(%rip),%rax
mov    (%rax),%eax
mov    0x2cc2(%rip),%rcx
or     (%rcx),%eax
or     -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    0x2d5a(%rip),%rcx
or     (%rcx),%eax
mov    0x2ce1(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2caf(%rip),%rax
mov    (%rax),%eax
mov    0x2d0e(%rip),%rcx
or     (%rcx),%eax
mov    0x2cfd(%rip),%rcx
or     (%rcx),%eax
mov    0x2cec(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d0a(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2ca4(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2c5f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x14,(%rax,%rcx,4)
mov    -0x34(%rbp),%edi
mov    0x2cb3(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
jge    133a <serialport_init+0x1da>
lea    0xd2b(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <serialport_init+0x1e0>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serialport_init>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    0x2df2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2ddf(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     12b5 <serialport_init+0x155>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1040 <tcgetattr@plt>
test   %rax,%rax
js     12be <serialport_init+0x15e>
mov    0x2df3(%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    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    0x2d62(%rip),%rcx
or     (%rcx),%eax
mov    0x2da1(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    (%rsp),%eax
mov    0x2da3(%rip),%rcx
or     (%rcx),%eax
mov    0x2d4a(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
and    %eax,%ecx
mov    0x2d95(%rip),%rax
mov    0x2d26(%rip),%rdx
or     (%rax),%ecx
or     (%rdx),%ecx
mov    %ecx,(%rsp)
mov    0x2db8(%rip),%rax
mov    0x2dc1(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d46(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x4(%rsp)
mov    0x2d17(%rip),%rax
mov    0x2d78(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d65(%rip),%rax
or     (%rax),%ecx
mov    0x2d54(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2d11(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2cd0(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x14,(%rax,%rcx,4)
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
test   %rax,%rax
js     12c7 <serialport_init+0x167>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd44(%rip),%rdi
jmp    12ce <serialport_init+0x16e>
lea    0xd61(%rip),%rdi
jmp    12ce <serialport_init+0x16e>
lea    0xd86(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    12aa <serialport_init+0x14a>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32, i32*, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [38 x i8] c"init_serialport: Unable to open port \00"
@.str.1 = [46 x i8] c"init_serialport: Couldn't get term attributes\00"
@B9600 = global i32 0
@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
@.str.2 = [46 x i8] c"init_serialport: Couldn't set term attributes\00"
define i32 @serialport_init(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.termios
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %8, %9
%11 = load i32, i32* @O_NDELAY
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %7, i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %101
18: ; preds = %1
%19 = load i32, i32* %5
%20 = call i64 @tcgetattr(i32 %19, %struct.termios* %4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %101
24: ; preds = %18
%25 = load i32, i32* @B9600
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = call i32 @cfsetispeed(%struct.termios* %4, i32 %26)
%28 = load i32, i32* %6
%29 = call i32 @cfsetospeed(%struct.termios* %4, i32 %28)
%30 = load i32, i32* @PARENB
%31 = xor i32 %30, -1
%32 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%33 = load i32, i32* %32
%34 = and i32 %33, %31
store i32 %34, i32* %32
%35 = load i32, i32* @CSTOPB
%36 = xor i32 %35, -1
%37 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%38 = load i32, i32* %37
%39 = and i32 %38, %36
store i32 %39, i32* %37
%40 = load i32, i32* @CSIZE
%41 = xor i32 %40, -1
%42 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%43 = load i32, i32* %42
%44 = and i32 %43, %41
store i32 %44, i32* %42
%45 = load i32, i32* @CS8
%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 = load i32, i32* @CRTSCTS
%50 = xor i32 %49, -1
%51 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%52 = load i32, i32* %51
%53 = and i32 %52, %50
store i32 %53, i32* %51
%54 = load i32, i32* @CREAD
%55 = load i32, i32* @CLOCAL
%56 = or i32 %54, %55
%57 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%58 = load i32, i32* %57
%59 = or i32 %58, %56
store i32 %59, i32* %57
%60 = load i32, i32* @IXON
%61 = load i32, i32* @IXOFF
%62 = or i32 %60, %61
%63 = load i32, i32* @IXANY
%64 = or i32 %62, %63
%65 = xor i32 %64, -1
%66 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%67 = load i32, i32* %66
%68 = and i32 %67, %65
store i32 %68, i32* %66
%69 = load i32, i32* @ICANON
%70 = load i32, i32* @ECHO
%71 = or i32 %69, %70
%72 = load i32, i32* @ECHOE
%73 = or i32 %71, %72
%74 = load i32, i32* @ISIG
%75 = or i32 %73, %74
%76 = xor i32 %75, -1
%77 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 2
%78 = load i32, i32* %77
%79 = and i32 %78, %76
store i32 %79, i32* %77
%80 = load i32, i32* @OPOST
%81 = xor i32 %80, -1
%82 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 4
%83 = load i32, i32* %82
%84 = and i32 %83, %81
store i32 %84, i32* %82
%85 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 3
%86 = load i32*, i32** %85
%87 = load i64, i64* @VMIN
%88 = getelementptr inbounds i32, i32* %86, i64 %87
store i32 0, i32* %88
%89 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 3
%90 = load i32*, i32** %89
%91 = load i64, i64* @VTIME
%92 = getelementptr inbounds i32, i32* %90, i64 %91
store i32 20, i32* %92
%93 = load i32, i32* %5
%94 = load i32, i32* @TCSANOW
%95 = call i64 @tcsetattr(i32 %93, i32 %94, %struct.termios* %4)
%96 = icmp slt i64 %95, 0
br i1 %96, label %97, label %99
97: ; preds = %24
%98 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %101
99: ; preds = %24
%100 = load i32, i32* %5
store i32 %100, i32* %2
br label %101
101: ; preds = %99, %97, %22, %16
%102 = load i32, i32* %2
ret i32 %102
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32, i32*, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [38 x i8] c"init_serialport: Unable to open port \00"
@.str.1 = [46 x i8] c"init_serialport: Couldn't get term attributes\00"
@B9600 = global i32 0
@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
@.str.2 = [46 x i8] c"init_serialport: Couldn't set term attributes\00"
define i32 @serialport_init(i8* nocapture %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NOCTTY
%6 = or i32 %5, %4
%7 = load i32, i32* @O_NDELAY
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
br label %76
13: ; preds = %1
%14 = call i64 @tcgetattr(i32 %9, %struct.termios* %2)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %76
18: ; preds = %13
%19 = load i32, i32* @B9600
%20 = call i32 @cfsetispeed(%struct.termios* %2, i32 %19)
%21 = call i32 @cfsetospeed(%struct.termios* %2, i32 %19)
%22 = load i32, i32* @PARENB
%23 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @CSTOPB
%26 = or i32 %22, %25
%27 = load i32, i32* @CSIZE
%28 = or i32 %26, %27
%29 = xor i32 %28, -1
%30 = and i32 %24, %29
%31 = load i32, i32* @CS8
%32 = or i32 %31, %30
%33 = load i32, i32* @CRTSCTS
%34 = xor i32 %33, -1
%35 = and i32 %32, %34
%36 = load i32, i32* @CREAD
%37 = load i32, i32* @CLOCAL
%38 = or i32 %36, %35
%39 = or i32 %38, %37
store i32 %39, i32* %23
%40 = load i32, i32* @IXON
%41 = load i32, i32* @IXOFF
%42 = or i32 %41, %40
%43 = load i32, i32* @IXANY
%44 = or i32 %42, %43
%45 = xor i32 %44, -1
%46 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%47 = load i32, i32* %46
%48 = and i32 %47, %45
store i32 %48, i32* %46
%49 = load i32, i32* @ICANON
%50 = load i32, i32* @ECHO
%51 = or i32 %50, %49
%52 = load i32, i32* @ECHOE
%53 = or i32 %51, %52
%54 = load i32, i32* @ISIG
%55 = or i32 %53, %54
%56 = xor i32 %55, -1
%57 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%58 = load i32, i32* %57
%59 = and i32 %58, %56
store i32 %59, i32* %57
%60 = load i32, i32* @OPOST
%61 = xor i32 %60, -1
%62 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 4
%63 = load i32, i32* %62
%64 = and i32 %63, %61
store i32 %64, i32* %62
%65 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
%66 = load i32*, i32** %65
%67 = load i64, i64* @VMIN
%68 = getelementptr inbounds i32, i32* %66, i64 %67
store i32 0, i32* %68
%69 = load i64, i64* @VTIME
%70 = getelementptr inbounds i32, i32* %66, i64 %69
store i32 20, i32* %70
%71 = load i32, i32* @TCSANOW
%72 = call i64 @tcsetattr(i32 %9, i32 %71, %struct.termios* %2)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %18
%75 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
br label %76
76: ; preds = %74, %18, %16, %11
%77 = phi i32 [ -1, %11 ], [ -1, %16 ], [ -1, %74 ], [ %9, %18 ]
ret i32 %77
}