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

/* Variables and functions */
int B115200;
int B1200;
int B9600;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ IGNCR;
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetspeed(struct termios *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int open(char const *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int InitSerial(const char *File, int BaudRate) {
  struct termios info;
  int baud;
  int fd;
  fd = open(File, O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (fd == -1)
    return -1;
  switch (BaudRate) {
  case 1200:
    baud = B1200;
    break;
  case 9600:
    baud = B9600;
    break;
  case 115200:
    baud = B115200;
    break;
  default:
    fprintf(stderr, "ERROR: Invalid baud rate to InitSerial (%i)\n", BaudRate);
    exit(1);
  }
  info.c_lflag = 0;
  info.c_cflag = baud | CS8 | CLOCAL | CREAD;
  info.c_iflag = IGNCR;
  info.c_oflag = 0;
  cfsetspeed(&info, baud);
  info.c_cc[VTIME] = 0;
  info.c_cc[VMIN] = 1;
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &info);
  return fd;
}


x86 assembly (clang, -O0):
<InitSerial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
or     (%rax),%esi
mov    0x2e54(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    11ac <InitSerial+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <InitSerial+0x17c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4c(%rbp)
sub    $0x4b0,%eax
je     11e8 <InitSerial+0x88>
jmp    11c2 <InitSerial+0x62>
mov    -0x4c(%rbp),%eax
sub    $0x2580,%eax
je     11f9 <InitSerial+0x99>
jmp    11d5 <InitSerial+0x75>
mov    -0x4c(%rbp),%eax
sub    $0x1c200,%eax
je     120a <InitSerial+0xaa>
jmp    121b <InitSerial+0xbb>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
jmp    123d <InitSerial+0xdd>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
jmp    123d <InitSerial+0xdd>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
jmp    123d <InitSerial+0xdd>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xdd2(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x44(%rbp),%eax
mov    0x2d69(%rip),%rcx
or     (%rcx),%eax
mov    0x2d10(%rip),%rcx
or     (%rcx),%eax
mov    0x2d5f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x44(%rbp),%esi
lea    -0x40(%rbp),%rdi
call   1040 <cfsetspeed@plt>
mov    -0x38(%rbp),%rax
mov    0x2ce7(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x38(%rbp),%rax
mov    0x2d0a(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x48(%rbp),%edi
mov    0x2ce6(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x48(%rbp),%edi
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<InitSerial>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    0x2e7b(%rip),%rcx
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %esi,%ebp
mov    0x2e5f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1260 <InitSerial+0x100>
cmp    $0x4b0,%ebp
je     11ba <InitSerial+0x5a>
cmp    $0x1c200,%ebp
je     11c3 <InitSerial+0x63>
cmp    $0x2580,%ebp
jne    1269 <InitSerial+0x109>
mov    0x2e10(%rip),%rax
jmp    11ca <InitSerial+0x6a>
mov    0x2dc7(%rip),%rax
jmp    11ca <InitSerial+0x6a>
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
movq   $0x0,0x20(%rsp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
or     %esi,%eax
mov    0x2d81(%rip),%rcx
or     (%rcx),%eax
mov    0x2dd0(%rip),%rcx
or     (%rcx),%eax
mov    %eax,(%rsp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1040 <cfsetspeed@plt>
mov    0x8(%rsp),%rax
mov    0x2d56(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d7d(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <tcflush@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1060 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%rip),%rsi
mov    %ebp,%edx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i64, i32, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@B1200 = global i32 0
@B9600 = global i32 0
@B115200 = global i32 0
@stderr = global i32 0
@.str = [45 x i8] c"ERROR: Invalid baud rate to InitSerial (%i)\0A\00"
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNCR = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @InitSerial(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.termios
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_NOCTTY
%12 = or i32 %10, %11
%13 = load i32, i32* @O_NONBLOCK
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %9, i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 -1, i32* %3
br label %62
19: ; preds = %2
%20 = load i32, i32* %5
switch i32 %20, label %27 [
i32 1200, label %21
i32 9600, label %23
i32 115200, label %25
]
21: ; preds = %19
%22 = load i32, i32* @B1200
store i32 %22, i32* %7
br label %32
23: ; preds = %19
%24 = load i32, i32* @B9600
store i32 %24, i32* %7
br label %32
25: ; preds = %19
%26 = load i32, i32* @B115200
store i32 %26, i32* %7
br label %32
27: ; preds = %19
%28 = load i32, i32* @stderr
%29 = load i32, i32* %5
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25, %23, %21
%33 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 4
store i64 0, i64* %33
%34 = load i32, i32* %7
%35 = load i32, i32* @CS8
%36 = or i32 %34, %35
%37 = load i32, i32* @CLOCAL
%38 = or i32 %36, %37
%39 = load i32, i32* @CREAD
%40 = or i32 %38, %39
%41 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* @IGNCR
%43 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
store i32 %42, i32* %43
%44 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
store i64 0, i64* %44
%45 = load i32, i32* %7
%46 = call i32 @cfsetspeed(%struct.termios* %6, i32 %45)
%47 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%48 = load i32*, i32** %47
%49 = load i64, i64* @VTIME
%50 = getelementptr inbounds i32, i32* %48, i64 %49
store i32 0, i32* %50
%51 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%52 = load i32*, i32** %51
%53 = load i64, i64* @VMIN
%54 = getelementptr inbounds i32, i32* %52, i64 %53
store i32 1, i32* %54
%55 = load i32, i32* %8
%56 = load i32, i32* @TCIFLUSH
%57 = call i32 @tcflush(i32 %55, i32 %56)
%58 = load i32, i32* %8
%59 = load i32, i32* @TCSANOW
%60 = call i32 @tcsetattr(i32 %58, i32 %59, %struct.termios* %6)
%61 = load i32, i32* %8
store i32 %61, i32* %3
br label %62
62: ; preds = %32, %18
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i64, i32, i64 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@B1200 = global i32 0
@B9600 = global i32 0
@B115200 = global i32 0
@stderr = global i32 0
@.str = [45 x i8] c"ERROR: Invalid baud rate to InitSerial (%i)\0A\00"
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNCR = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
define i32 @InitSerial(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %44, label %12
12: ; preds = %2
switch i32 %1, label %15 [
i32 1200, label %19
i32 9600, label %13
i32 115200, label %14
]
13: ; preds = %12
br label %19
14: ; preds = %12
br label %19
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %1)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %12, %14, %13
%20 = phi i32* [ @B115200, %14 ], [ @B9600, %13 ], [ @B1200, %12 ]
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 4
store i64 0, i64* %22
%23 = load i32, i32* @CS8
%24 = or i32 %23, %21
%25 = load i32, i32* @CLOCAL
%26 = or i32 %24, %25
%27 = load i32, i32* @CREAD
%28 = or i32 %26, %27
%29 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @IGNCR
%31 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
store i64 0, i64* %32
%33 = call i32 @cfsetspeed(%struct.termios* %3, i32 %21)
%34 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%35 = load i32*, i32** %34
%36 = load i64, i64* @VTIME
%37 = getelementptr inbounds i32, i32* %35, i64 %36
store i32 0, i32* %37
%38 = load i64, i64* @VMIN
%39 = getelementptr inbounds i32, i32* %35, i64 %38
store i32 1, i32* %39
%40 = load i32, i32* @TCIFLUSH
%41 = call i32 @tcflush(i32 %10, i32 %40)
%42 = load i32, i32* @TCSANOW
%43 = call i32 @tcsetattr(i32 %10, i32 %42, %struct.termios* %3)
br label %44
44: ; preds = %2, %19
ret i32 %10
}