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

/* Variables and functions */
int /*<<< orphan*/ B115200;
int /*<<< orphan*/ B9600;
int CREAD;
int CS8;
int CSTOPB;
int PARENB;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ errExit(char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
void startSerial(int fd, int flags) {
  struct termios tp;
  if (tcgetattr(fd, &tp) == -1)
    errExit("tcgetattr");
  if (cfsetispeed(&tp, (flags & 01)   ? B9600
                       : (flags & 02) ? B115200
                                      : B9600) == -1)
    errExit("cfsetispeed");
  tp.c_cflag = CS8 | CREAD;
  tp.c_cflag &= ~PARENB;
  if ((flags & 010))
    tp.c_cflag &= ~CSTOPB;
  else
    tp.c_cflag |= CSTOPB;
  tp.c_cc[VMIN] = 1;
  tp.c_cc[VTIME] = 0;
  tcflush(fd, TCIOFLUSH);
  if (tcsetattr(fd, TCSANOW, &tp) == -1)
    errExit("tcsetattr");
}


x86 assembly (clang, -O0):
<startSerial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    117f <startSerial+0x2f>
lea    0xe86(%rip),%rdi
call   1030 <errExit@plt>
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     119f <startSerial+0x4f>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11d1 <startSerial+0x81>
mov    -0x8(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11bf <startSerial+0x6f>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    11cb <startSerial+0x7b>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1070 <cfsetispeed@plt>
cmp    $0xffffffff,%eax
jne    11f2 <startSerial+0xa2>
lea    0xe1d(%rip),%rdi
call   1030 <errExit@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    0x2dce(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     123f <startSerial+0xef>
mov    0x2d61(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    124e <startSerial+0xfe>
mov    0x2d4a(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d67(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    0x2d32(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x4(%rbp),%edi
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x4(%rbp),%edi
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    12b3 <startSerial+0x163>
lea    0xd68(%rip),%rdi
call   1030 <errExit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<startSerial>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     1171 <startSerial+0x21>
test   $0x1,%bpl
je     1183 <startSerial+0x33>
jmp    1189 <startSerial+0x39>
lea    0xe88(%rip),%rdi
call   1030 <errExit@plt>
test   $0x1,%bpl
jne    1189 <startSerial+0x39>
test   $0x2,%bpl
jne    1192 <startSerial+0x42>
mov    0x2e48(%rip),%rax
jmp    1199 <startSerial+0x49>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
call   1070 <cfsetispeed@plt>
cmp    $0xffffffff,%eax
jne    11b6 <startSerial+0x66>
lea    0xe59(%rip),%rdi
call   1030 <errExit@plt>
mov    0x2e0b(%rip),%rax
mov    0x2e0c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ecx,%eax
mov    0x2db4(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,%edx
or     %eax,%edx
not    %ecx
and    %eax,%ecx
test   $0x8,%bpl
cmove  %edx,%ecx
mov    %ecx,0x8(%rsp)
mov    0x10(%rsp),%rax
mov    0x2dc3(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d92(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    124e <startSerial+0xfe>
lea    0xdcd(%rip),%rdi
call   1030 <errExit@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32* }
@.str = [10 x i8] c"tcgetattr\00"
@B9600 = global i32 0
@B115200 = global i32 0
@.str.1 = [12 x i8] c"cfsetispeed\00"
@CS8 = global i32 0
@CREAD = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCIOFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.2 = [10 x i8] c"tcsetattr\00"
define void @startSerial(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.termios
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
%7 = call i32 @tcgetattr(i32 %6, %struct.termios* %5)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @errExit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = load i32, i32* %4
%13 = and i32 %12, 1
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = load i32, i32* @B9600
br label %27
17: ; preds = %11
%18 = load i32, i32* %4
%19 = and i32 %18, 2
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load i32, i32* @B115200
br label %25
23: ; preds = %17
%24 = load i32, i32* @B9600
br label %25
25: ; preds = %23, %21
%26 = phi i32 [ %22, %21 ], [ %24, %23 ]
br label %27
27: ; preds = %25, %15
%28 = phi i32 [ %16, %15 ], [ %26, %25 ]
%29 = call i32 @cfsetispeed(%struct.termios* %5, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @errExit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
%34 = load i32, i32* @CS8
%35 = load i32, i32* @CREAD
%36 = or i32 %34, %35
%37 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
store i32 %36, i32* %37
%38 = load i32, i32* @PARENB
%39 = xor i32 %38, -1
%40 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%41 = load i32, i32* %40
%42 = and i32 %41, %39
store i32 %42, i32* %40
%43 = load i32, i32* %4
%44 = and i32 %43, 8
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %33
%47 = load i32, i32* @CSTOPB
%48 = xor i32 %47, -1
%49 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%50 = load i32, i32* %49
%51 = and i32 %50, %48
store i32 %51, i32* %49
br label %57
52: ; preds = %33
%53 = load i32, i32* @CSTOPB
%54 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%55 = load i32, i32* %54
%56 = or i32 %55, %53
store i32 %56, i32* %54
br label %57
57: ; preds = %52, %46
%58 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%59 = load i32*, i32** %58
%60 = load i64, i64* @VMIN
%61 = getelementptr inbounds i32, i32* %59, i64 %60
store i32 1, i32* %61
%62 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%63 = load i32*, i32** %62
%64 = load i64, i64* @VTIME
%65 = getelementptr inbounds i32, i32* %63, i64 %64
store i32 0, i32* %65
%66 = load i32, i32* %3
%67 = load i32, i32* @TCIOFLUSH
%68 = call i32 @tcflush(i32 %66, i32 %67)
%69 = load i32, i32* %3
%70 = load i32, i32* @TCSANOW
%71 = call i32 @tcsetattr(i32 %69, i32 %70, %struct.termios* %5)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %75
73: ; preds = %57
%74 = call i32 @errExit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %75
75: ; preds = %73, %57
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32, i32* }
@.str = [10 x i8] c"tcgetattr\00"
@B9600 = global i32 0
@B115200 = global i32 0
@.str.1 = [12 x i8] c"cfsetispeed\00"
@CS8 = global i32 0
@CREAD = global i32 0
@PARENB = global i32 0
@CSTOPB = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCIOFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.2 = [10 x i8] c"tcsetattr\00"
define void @startSerial(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = call i32 @tcgetattr(i32 %0, %struct.termios* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = call i32 @errExit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = and i32 %1, 1
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = load i32, i32* @B9600
br label %20
14: ; preds = %9
%15 = and i32 %1, 2
%16 = icmp eq i32 %15, 0
%17 = load i32, i32* @B115200
%18 = load i32, i32* @B9600
%19 = select i1 %16, i32 %18, i32 %17
br label %20
20: ; preds = %14, %12
%21 = phi i32 [ %13, %12 ], [ %19, %14 ]
%22 = call i32 @cfsetispeed(%struct.termios* %3, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @errExit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = load i32, i32* @CS8
%28 = load i32, i32* @CREAD
%29 = or i32 %28, %27
%30 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%31 = load i32, i32* @PARENB
%32 = xor i32 %31, -1
%33 = and i32 %29, %32
%34 = and i32 %1, 8
%35 = icmp eq i32 %34, 0
%36 = load i32, i32* @CSTOPB
%37 = or i32 %36, %33
%38 = xor i32 %36, -1
%39 = and i32 %33, %38
%40 = select i1 %35, i32 %37, i32 %39
store i32 %40, i32* %30
%41 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%42 = load i32*, i32** %41
%43 = load i64, i64* @VMIN
%44 = getelementptr inbounds i32, i32* %42, i64 %43
store i32 1, i32* %44
%45 = load i64, i64* @VTIME
%46 = getelementptr inbounds i32, i32* %42, i64 %45
store i32 0, i32* %46
%47 = load i32, i32* @TCIOFLUSH
%48 = call i32 @tcflush(i32 %0, i32 %47)
%49 = load i32, i32* @TCSANOW
%50 = call i32 @tcsetattr(i32 %0, i32 %49, %struct.termios* %3)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %26
%53 = call i32 @errExit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %54
54: ; preds = %52, %26
ret void
}