#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;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int CLOCAL;
int CREAD;
int CRTSCTS;
int CS8;
int CSIZE;
int CSTOPB;
int IGNBRK;
int IGNCR;
int IGNPAR;
int IXANY;
int IXOFF;
int IXON;
int PARENB;
int PARODD;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int errno;
int /*<<< orphan*/ printf(char *, int);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int configureSerial(int portHandle, int baudRate) {
  struct termios portConfig = {0};
  if (tcgetattr(portHandle, &portConfig) != 0) {
    printf("Get Attributes failed.\n Error: %d", errno);
    return -1;
  }
  portConfig.c_oflag = 0;
  portConfig.c_lflag = 0;
  portConfig.c_cflag |= (portConfig.c_cflag & ~CSIZE) | CS8;
  portConfig.c_cflag |= CREAD | CLOCAL;
  portConfig.c_cflag &= ~(PARENB | PARODD);
  portConfig.c_cflag &= ~(CSTOPB);
  portConfig.c_cflag &= ~(CRTSCTS);
  portConfig.c_iflag |= IGNBRK | IGNPAR | IGNCR;
  portConfig.c_iflag &= ~(IXON | IXOFF | IXANY);
  cfsetospeed(&portConfig, baudRate);
  cfsetispeed(&portConfig, baudRate);
  portConfig.c_cc[VMIN] = 255;
  portConfig.c_cc[VTIME] = 5;
  if (tcsetattr(portHandle, TCSANOW, &portConfig) != 0) {
    printf("Failed to set port attributes. Error: %d\n", errno);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<configureSerial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0x0,%rax
je     11b5 <configureSerial+0x55>
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    0xe5c(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130a <configureSerial+0x1aa>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x38(%rbp),%eax
mov    0x2dd1(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    0x2dd3(%rip),%rcx
or     (%rcx),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    0x2d5b(%rip),%rcx
or     (%rcx),%eax
or     -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    0x2db3(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d39(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d2f(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
mov    0x2d24(%rip),%rcx
or     (%rcx),%eax
mov    0x2d8b(%rip),%rcx
or     (%rcx),%eax
or     -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    0x2d7b(%rip),%rcx
or     (%rcx),%eax
mov    0x2d1a(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0xc(%rbp),%esi
lea    -0x38(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    -0xc(%rbp),%esi
lea    -0x38(%rbp),%rdi
call   1080 <cfsetispeed@plt>
mov    -0x30(%rbp),%rax
mov    0x2d04(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xff,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
mov    0x2cbf(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    -0x8(%rbp),%edi
mov    0x2cfb(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
je     1303 <configureSerial+0x1a3>
mov    0x2c9f(%rip),%rax
mov    (%rax),%esi
lea    0xd30(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130a <configureSerial+0x1aa>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<configureSerial>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movq   $0x0,0x20(%rsp)
mov    %rsp,%rsi
call   1050 <tcgetattr@plt>
test   %rax,%rax
je     118a <configureSerial+0x3a>
lea    0xe8b(%rip),%rdi
jmp    127a <configureSerial+0x12a>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
or     (%rsp),%eax
mov    0x2e13(%rip),%rcx
or     (%rcx),%eax
mov    0x2daa(%rip),%rcx
or     (%rcx),%eax
mov    0x2e19(%rip),%rcx
mov    0x2e02(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2d97(%rip),%rcx
or     (%rcx),%edx
mov    0x2d96(%rip),%rcx
or     (%rcx),%edx
mov    0x2dad(%rip),%rcx
mov    0x2d96(%rip),%rsi
mov    (%rsi),%esi
or     (%rcx),%esi
not    %edx
and    %eax,%edx
mov    0x2def(%rip),%rax
or     (%rax),%esi
mov    %edx,(%rsp)
or     0x4(%rsp),%esi
mov    0x2dd7(%rip),%rax
mov    0x2de0(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d85(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %esi,%ecx
mov    %ecx,0x4(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <cfsetospeed@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1070 <cfsetispeed@plt>
mov    0x8(%rsp),%rax
mov    0x2d68(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xff,(%rax,%rcx,4)
mov    0x2d2f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
test   %rax,%rax
je     1296 <configureSerial+0x146>
lea    0xdb8(%rip),%rdi
mov    0x2d0f(%rip),%rax
mov    (%rax),%esi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
jmp    128d <configureSerial+0x13d>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i64, i64, i32 }
@.str = [34 x i8] c"Get Attributes failed.\0A Error: %d\00"
@errno = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@PARENB = global i32 0
@PARODD = global i32 0
@CSTOPB = global i32 0
@CRTSCTS = global i32 0
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@IGNCR = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.1 = [42 x i8] c"Failed to set port attributes. Error: %d\0A\00"
define i32 @configureSerial(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.termios
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = bitcast %struct.termios* %6 to i8*
%8 = load i32, i32* %4
%9 = call i64 @tcgetattr(i32 %8, %struct.termios* %6)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 -1, i32* %3
br label %87
14: ; preds = %2
%15 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 4
store i64 0, i64* %15
%16 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
store i64 0, i64* %16
%17 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* @CSIZE
%20 = xor i32 %19, -1
%21 = and i32 %18, %20
%22 = load i32, i32* @CS8
%23 = or i32 %21, %22
%24 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = or i32 %25, %23
store i32 %26, i32* %24
%27 = load i32, i32* @CREAD
%28 = load i32, i32* @CLOCAL
%29 = or i32 %27, %28
%30 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%31 = load i32, i32* %30
%32 = or i32 %31, %29
store i32 %32, i32* %30
%33 = load i32, i32* @PARENB
%34 = load i32, i32* @PARODD
%35 = or i32 %33, %34
%36 = xor i32 %35, -1
%37 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%38 = load i32, i32* %37
%39 = and i32 %38, %36
store i32 %39, i32* %37
%40 = load i32, i32* @CSTOPB
%41 = xor i32 %40, -1
%42 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%43 = load i32, i32* %42
%44 = and i32 %43, %41
store i32 %44, i32* %42
%45 = load i32, i32* @CRTSCTS
%46 = xor i32 %45, -1
%47 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%48 = load i32, i32* %47
%49 = and i32 %48, %46
store i32 %49, i32* %47
%50 = load i32, i32* @IGNBRK
%51 = load i32, i32* @IGNPAR
%52 = or i32 %50, %51
%53 = load i32, i32* @IGNCR
%54 = or i32 %52, %53
%55 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%56 = load i32, i32* %55
%57 = or i32 %56, %54
store i32 %57, i32* %55
%58 = load i32, i32* @IXON
%59 = load i32, i32* @IXOFF
%60 = or i32 %58, %59
%61 = load i32, i32* @IXANY
%62 = or i32 %60, %61
%63 = xor i32 %62, -1
%64 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%65 = load i32, i32* %64
%66 = and i32 %65, %63
store i32 %66, i32* %64
%67 = load i32, i32* %5
%68 = call i32 @cfsetospeed(%struct.termios* %6, i32 %67)
%69 = load i32, i32* %5
%70 = call i32 @cfsetispeed(%struct.termios* %6, i32 %69)
%71 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%72 = load i32*, i32** %71
%73 = load i64, i64* @VMIN
%74 = getelementptr inbounds i32, i32* %72, i64 %73
store i32 255, i32* %74
%75 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%76 = load i32*, i32** %75
%77 = load i64, i64* @VTIME
%78 = getelementptr inbounds i32, i32* %76, i64 %77
store i32 5, i32* %78
%79 = load i32, i32* %4
%80 = load i32, i32* @TCSANOW
%81 = call i64 @tcsetattr(i32 %79, i32 %80, %struct.termios* %6)
%82 = icmp ne i64 %81, 0
br i1 %82, label %83, label %86
83: ; preds = %14
%84 = load i32, i32* @errno
%85 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %84)
store i32 -1, i32* %3
br label %87
86: ; preds = %14
store i32 0, i32* %3
br label %87
87: ; preds = %86, %83, %11
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32*, i64, i64, i32 }
@.str = [34 x i8] c"Get Attributes failed.\0A Error: %d\00"
@errno = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@PARENB = global i32 0
@PARODD = global i32 0
@CSTOPB = global i32 0
@CRTSCTS = global i32 0
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@IGNCR = global i32 0
@IXON = global i32 0
@IXOFF = global i32 0
@IXANY = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.1 = [42 x i8] c"Failed to set port attributes. Error: %d\0A\00"
define i32 @configureSerial(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = call i64 @tcgetattr(i32 %0, %struct.termios* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %53
7: ; preds = %2
%8 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%9 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%10 = bitcast i64* %8 to i8*
%11 = load i32, i32* %9
%12 = load i32, i32* @CS8
%13 = or i32 %12, %11
%14 = load i32, i32* @CREAD
%15 = load i32, i32* @CLOCAL
%16 = or i32 %14, %13
%17 = or i32 %16, %15
%18 = load i32, i32* @PARENB
%19 = load i32, i32* @PARODD
%20 = or i32 %19, %18
%21 = load i32, i32* @CSTOPB
%22 = or i32 %20, %21
%23 = load i32, i32* @CRTSCTS
%24 = or i32 %22, %23
%25 = xor i32 %24, -1
%26 = and i32 %17, %25
store i32 %26, i32* %9
%27 = load i32, i32* @IGNBRK
%28 = load i32, i32* @IGNPAR
%29 = or i32 %28, %27
%30 = load i32, i32* @IGNCR
%31 = or i32 %29, %30
%32 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%33 = load i32, i32* %32
%34 = or i32 %31, %33
%35 = load i32, i32* @IXON
%36 = load i32, i32* @IXOFF
%37 = or i32 %36, %35
%38 = load i32, i32* @IXANY
%39 = or i32 %37, %38
%40 = xor i32 %39, -1
%41 = and i32 %34, %40
store i32 %41, i32* %32
%42 = call i32 @cfsetospeed(%struct.termios* %3, i32 %1)
%43 = call i32 @cfsetispeed(%struct.termios* %3, i32 %1)
%44 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
%45 = load i32*, i32** %44
%46 = load i64, i64* @VMIN
%47 = getelementptr inbounds i32, i32* %45, i64 %46
store i32 255, i32* %47
%48 = load i64, i64* @VTIME
%49 = getelementptr inbounds i32, i32* %45, i64 %48
store i32 5, i32* %49
%50 = load i32, i32* @TCSANOW
%51 = call i64 @tcsetattr(i32 %0, i32 %50, %struct.termios* %3)
%52 = icmp eq i64 %51, 0
br i1 %52, label %57, label %53
53: ; preds = %7, %2
%54 = phi i8* [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), %7 ]
%55 = load i32, i32* @errno
%56 = call i32 @printf(i8* %54, i32 %55)
br label %57
57: ; preds = %53, %7
%58 = phi i32 [ 0, %7 ], [ -1, %53 ]
ret i32 %58
}