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

/* Variables and functions */
int /*<<< orphan*/ B1152000;
int CLOCAL;
int CREAD;
int CS8;
int O_NONBLOCK;
int O_RDWR;
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*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int serial_setup(char *name) {
  struct termios tio;
  int fd = -1;
  fd = open(name, O_RDWR | O_NONBLOCK);
  if (fd < 0) {
    perror("Unable to open communication with companion chip");
    return -1;
  }
  memset(&tio, 0, sizeof(tio));
  tio.c_cflag = CS8 | CREAD | CLOCAL;
  tio.c_cc[VMIN] = 1;
  tio.c_cc[VTIME] = 5;
  cfsetospeed(&tio, B1152000);
  cfsetispeed(&tio, B1152000);
  tcsetattr(fd, TCSANOW, &tio);
  return fd;
}


x86 assembly (clang, -O0):
<serial_setup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e59(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b3 <serial_setup+0x53>
lea    0xe5e(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <serial_setup+0xea>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    0x2dfd(%rip),%rcx
or     (%rcx),%eax
mov    0x2dbc(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dd4(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2da7(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdi
call   1070 <cfsetispeed@plt>
mov    -0x24(%rbp),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_setup>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e78(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     121d <serial_setup+0xbd>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e25(%rip),%rax
mov    0x2e26(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2de3(%rip),%rax
or     (%rax),%ecx
mov    %ecx,(%rsp)
mov    0x8(%rsp),%rax
mov    0x2dfa(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2dd1(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2dc8(%rip),%r15
mov    (%r15),%esi
mov    %r14,%rdi
call   1030 <cfsetospeed@plt>
mov    (%r15),%esi
mov    %r14,%rdi
call   1070 <cfsetispeed@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xddc(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1211 <serial_setup+0xb1>

LLVM IR (-O0):
%struct.termios = type { i32, i32* }
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str = [49 x i8] c"Unable to open communication with companion chip\00"
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@B1152000 = global i32 0
@TCSANOW = global i32 0
define i32 @serial_setup(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.termios
%5 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %5
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %7, %8
%10 = call i32 @open(i8* %6, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %39
15: ; preds = %1
%16 = call i32 @memset(%struct.termios* %4, i32 0, i32 16)
%17 = load i32, i32* @CS8
%18 = load i32, i32* @CREAD
%19 = or i32 %17, %18
%20 = load i32, i32* @CLOCAL
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%24 = load i32*, i32** %23
%25 = load i64, i64* @VMIN
%26 = getelementptr inbounds i32, i32* %24, i64 %25
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%28 = load i32*, i32** %27
%29 = load i64, i64* @VTIME
%30 = getelementptr inbounds i32, i32* %28, i64 %29
store i32 5, i32* %30
%31 = load i32, i32* @B1152000
%32 = call i32 @cfsetospeed(%struct.termios* %4, i32 %31)
%33 = load i32, i32* @B1152000
%34 = call i32 @cfsetispeed(%struct.termios* %4, i32 %33)
%35 = load i32, i32* %5
%36 = load i32, i32* @TCSANOW
%37 = call i32 @tcsetattr(i32 %35, i32 %36, %struct.termios* %4)
%38 = load i32, i32* %5
store i32 %38, i32* %2
br label %39
39: ; preds = %15, %13
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.termios = type { i32, i32* }
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str = [49 x i8] c"Unable to open communication with companion chip\00"
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@B1152000 = global i32 0
@TCSANOW = global i32 0
define i32 @serial_setup(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_NONBLOCK
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %31
11: ; preds = %1
%12 = call i32 @memset(%struct.termios* %2, i32 0, i32 16)
%13 = load i32, i32* @CS8
%14 = load i32, i32* @CREAD
%15 = or i32 %14, %13
%16 = load i32, i32* @CLOCAL
%17 = or i32 %15, %16
%18 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%20 = load i32*, i32** %19
%21 = load i64, i64* @VMIN
%22 = getelementptr inbounds i32, i32* %20, i64 %21
store i32 1, i32* %22
%23 = load i64, i64* @VTIME
%24 = getelementptr inbounds i32, i32* %20, i64 %23
store i32 5, i32* %24
%25 = load i32, i32* @B1152000
%26 = call i32 @cfsetospeed(%struct.termios* %2, i32 %25)
%27 = load i32, i32* @B1152000
%28 = call i32 @cfsetispeed(%struct.termios* %2, i32 %27)
%29 = load i32, i32* @TCSANOW
%30 = call i32 @tcsetattr(i32 %7, i32 %29, %struct.termios* %2)
br label %31
31: ; preds = %11, %9
%32 = phi i32 [ -1, %9 ], [ %7, %11 ]
ret i32 %32
}