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

/* Variables and functions */
int /*<<< orphan*/ B115200;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ IGNBRK;
int /*<<< orphan*/ IGNPAR;
int NCCS;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCMSET;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetspeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int *);
int open(char const *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_serial_device(const char *serial_device_path) {
  int device_file = open(serial_device_path, O_RDWR | O_NOCTTY, 0);
  if (device_file < 0) {
    printf("Can't Open Serial Controller\n");
    return -1;
  }
  struct termios options;
  int bits;
  bits = 0;
  ioctl(device_file, TIOCMSET, &bits);
  tcgetattr(device_file, &options);
  options.c_iflag = IGNPAR;
  options.c_iflag |= IGNBRK;
  options.c_cflag |= CS8 | CREAD | CLOCAL;
  options.c_oflag = 0;
  options.c_lflag = 0;
  int i;
  for (i = 0; i < NCCS; i++) {
    options.c_cc[i] = 0;
  }
  options.c_cc[VMIN] = 0;
  options.c_cc[VTIME] = 1;
  cfsetspeed(&options, B115200);
  if (tcsetattr(device_file, TCSANOW, &options) < 0) {
    printf("Can't Set The Serial Device Parameters\n");
  } else {
    printf("Successfully Set the Serial Device Parameters\n");
  }
  tcflush(device_file, TCIOFLUSH);
  return device_file;
}


x86 assembly (clang, -O0):
<open_serial_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    0x2e18(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11be <open_serial_device+0x4e>
lea    0xe53(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <open_serial_device+0x187>
movl   $0x0,-0x44(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    -0x44(%rbp),%rdx
call   1060 <ioctl@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1070 <tcgetattr@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    0x2db7(%rip),%rcx
or     (%rcx),%eax
mov    0x2d56(%rip),%rcx
or     (%rcx),%eax
or     -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    0x2dad(%rip),%rcx
cmp    (%rcx),%eax
jge    1268 <open_serial_device+0xf8>
mov    -0x38(%rbp),%rax
movslq -0x48(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1239 <open_serial_device+0xc9>
mov    -0x38(%rbp),%rax
mov    0x2d45(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x38(%rbp),%rax
mov    0x2d00(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2cf7(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdi
call   1050 <cfsetspeed@plt>
mov    -0x14(%rbp),%edi
mov    0x2d22(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%rax
jge    12d4 <open_serial_device+0x164>
lea    0xd54(%rip),%rdi
call   1040 <printf@plt>
jmp    12e0 <open_serial_device+0x170>
lea    0xd6b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2cb6(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_serial_device>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    0x2e62(%rip),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edx,%edx
call   1090 <open@plt>
test   %eax,%eax
js     12aa <open_serial_device+0x13a>
mov    %eax,%ebx
movl   $0x0,0xc(%rsp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1060 <ioctl@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1070 <tcgetattr@plt>
mov    0x2db6(%rip),%rax
mov    0x2dc7(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2de4(%rip),%rax
mov    0x2de5(%rip),%rdx
mov    (%rdx),%edx
or     (%rax),%edx
mov    0x2d82(%rip),%rax
or     (%rax),%edx
mov    %ecx,0x30(%rsp)
or     %edx,0x10(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x20(%rsp)
mov    0x2de9(%rip),%rcx
cmpl   $0x0,(%rcx)
mov    0x18(%rsp),%rax
jle    1233 <open_serial_device+0xc3>
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movl   $0x0,(%rax,%rdx,4)
add    $0x1,%rdx
movslq (%rcx),%rsi
cmp    %rsi,%rdx
jl     1220 <open_serial_device+0xb0>
mov    0x2d7e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d3d(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <cfsetspeed@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1080 <tcsetattr@plt>
test   %rax,%rax
lea    0xda6(%rip),%rax
lea    0xd71(%rip),%rdi
cmovs  %rax,%rdi
call   1030 <puts@plt>
mov    0x2d01(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
jmp    12bb <open_serial_device+0x14b>
lea    0xda4(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i64, i64, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [30 x i8] c"Can't Open Serial Controller\0A\00"
@TIOCMSET = global i32 0
@IGNPAR = global i32 0
@IGNBRK = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@NCCS = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@B115200 = global i32 0
@TCSANOW = global i32 0
@.str.1 = [40 x i8] c"Can't Set The Serial Device Parameters\0A\00"
@.str.2 = [47 x i8] c"Successfully Set the Serial Device Parameters\0A\00"
@TCIOFLUSH = global i32 0
define i32 @open_serial_device(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.termios
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %8, i32 %11, i32 0)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %76
17: ; preds = %1
store i32 0, i32* %6
%18 = load i32, i32* %4
%19 = load i32, i32* @TIOCMSET
%20 = call i32 @ioctl(i32 %18, i32 %19, i32* %6)
%21 = load i32, i32* %4
%22 = call i32 @tcgetattr(i32 %21, %struct.termios* %5)
%23 = load i32, i32* @IGNPAR
%24 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 4
store i32 %23, i32* %24
%25 = load i32, i32* @IGNBRK
%26 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 4
%27 = load i32, i32* %26
%28 = or i32 %27, %25
store i32 %28, i32* %26
%29 = load i32, i32* @CS8
%30 = load i32, i32* @CREAD
%31 = or i32 %29, %30
%32 = load i32, i32* @CLOCAL
%33 = or i32 %31, %32
%34 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%35 = load i32, i32* %34
%36 = or i32 %35, %33
store i32 %36, i32* %34
%37 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 3
store i64 0, i64* %37
%38 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 2
store i64 0, i64* %38
store i32 0, i32* %7
br label %39
39: ; preds = %49, %17
%40 = load i32, i32* %7
%41 = load i32, i32* @NCCS
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %52
43: ; preds = %39
%44 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%45 = load i32*, i32** %44
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
store i32 0, i32* %48
br label %49
49: ; preds = %43
%50 = load i32, i32* %7
%51 = add nsw i32 %50, 1
store i32 %51, i32* %7
br label %39
52: ; preds = %39
%53 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%54 = load i32*, i32** %53
%55 = load i64, i64* @VMIN
%56 = getelementptr inbounds i32, i32* %54, i64 %55
store i32 0, i32* %56
%57 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 1
%58 = load i32*, i32** %57
%59 = load i64, i64* @VTIME
%60 = getelementptr inbounds i32, i32* %58, i64 %59
store i32 1, i32* %60
%61 = load i32, i32* @B115200
%62 = call i32 @cfsetspeed(%struct.termios* %5, i32 %61)
%63 = load i32, i32* %4
%64 = load i32, i32* @TCSANOW
%65 = call i64 @tcsetattr(i32 %63, i32 %64, %struct.termios* %5)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %52
%68 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %71
69: ; preds = %52
%70 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
br label %71
71: ; preds = %69, %67
%72 = load i32, i32* %4
%73 = load i32, i32* @TCIOFLUSH
%74 = call i32 @tcflush(i32 %72, i32 %73)
%75 = load i32, i32* %4
store i32 %75, i32* %2
br label %76
76: ; preds = %71, %15
%77 = load i32, i32* %2
ret i32 %77
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i64, i64, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TIOCMSET = global i32 0
@IGNPAR = global i32 0
@IGNBRK = global i32 0
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@NCCS = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@B115200 = global i32 0
@TCSANOW = global i32 0
@TCIOFLUSH = global i32 0
@str = [46 x i8] c"Successfully Set the Serial Device Parameters\00"
@str.3 = [39 x i8] c"Can't Set The Serial Device Parameters\00"
@str.4 = [29 x i8] c"Can't Open Serial Controller\00"
define i32 @open_serial_device(i8* nocapture %0) {
%2 = alloca %struct.termios
%3 = alloca i32
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NOCTTY
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.4, i64 0, i64 0))
br label %56
11: ; preds = %1
%12 = bitcast %struct.termios* %2 to i8*
%13 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%14 = load i32, i32* @TIOCMSET
%15 = call i32 @ioctl(i32 %7, i32 %14, i32* %3)
%16 = call i32 @tcgetattr(i32 %7, %struct.termios* %2)
%17 = load i32, i32* @IGNPAR
%18 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 4
%19 = load i32, i32* @IGNBRK
%20 = or i32 %19, %17
store i32 %20, i32* %18
%21 = load i32, i32* @CS8
%22 = load i32, i32* @CREAD
%23 = or i32 %22, %21
%24 = load i32, i32* @CLOCAL
%25 = or i32 %23, %24
%26 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%27 = load i32, i32* %26
%28 = or i32 %25, %27
store i32 %28, i32* %26
%29 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
%30 = bitcast i64* %29 to i8*
%31 = load i32, i32* @NCCS
%32 = icmp sgt i32 %31, 0
%33 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%34 = load i32*, i32** %33
br i1 %32, label %35, label %42
35: ; preds = %11, %35
%36 = phi i64 [ %38, %35 ], [ 0, %11 ]
%37 = getelementptr inbounds i32, i32* %34, i64 %36
store i32 0, i32* %37
%38 = add nuw nsw i64 %36, 1
%39 = load i32, i32* @NCCS
%40 = sext i32 %39 to i64
%41 = icmp slt i64 %38, %40
br i1 %41, label %35, label %42
42: ; preds = %35, %11
%43 = load i64, i64* @VMIN
%44 = getelementptr inbounds i32, i32* %34, i64 %43
store i32 0, i32* %44
%45 = load i64, i64* @VTIME
%46 = getelementptr inbounds i32, i32* %34, i64 %45
store i32 1, i32* %46
%47 = load i32, i32* @B115200
%48 = call i32 @cfsetspeed(%struct.termios* %2, i32 %47)
%49 = load i32, i32* @TCSANOW
%50 = call i64 @tcsetattr(i32 %7, i32 %49, %struct.termios* %2)
%51 = icmp slt i64 %50, 0
%52 = select i1 %51, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @str.3, i64 0, i64 0), i8* getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0)
%53 = call i32 @puts(i8* dereferenceable(1) %52)
%54 = load i32, i32* @TCIOFLUSH
%55 = call i32 @tcflush(i32 %7, i32 %54)
br label %56
56: ; preds = %42, %9
%57 = phi i32 [ -1, %9 ], [ %7, %42 ]
ret i32 %57
}