#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 */

/* Variables and functions */
unsigned int B115200;
int /*<<< orphan*/ EINVAL;
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
unsigned int _parse_baudrate(char *);
int /*<<< orphan*/ _set_blocking(int, int);
int /*<<< orphan*/ _set_serial_attribs(int, unsigned int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ stderr;
int _open_serial(int argc, char **argv) {
  unsigned baudrate = (B115200);
  int fd;
  if (argc > 1) {
    baudrate = _parse_baudrate(argv[1]);
  }
  if (baudrate == 0) {
    fputs("Invalid baudrate\n", stderr);
    errno = EINVAL;
    return -1;
  }
  fd = open(argv[0], O_RDWR | O_NOCTTY | O_SYNC);
  if (fd < 0) {
    return fd;
  }
  _set_serial_attribs(fd, baudrate, 0);
  _set_blocking(fd, 1);
  return fd;
}


x86 assembly (clang, -O0):
<_open_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1185 <_open_serial+0x35>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <_parse_baudrate@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11c2 <_open_serial+0x72>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1050 <fputs@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1221 <_open_serial+0xd1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2df7(%rip),%rax
or     (%rax),%esi
mov    0x2dfe(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1201 <_open_serial+0xb1>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1221 <_open_serial+0xd1>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   1030 <_set_serial_attribs@plt>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1040 <_set_blocking@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_open_serial>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
cmp    $0x2,%edi
jl     11ad <_open_serial+0x5d>
mov    0x8(%r14),%rdi
call   1070 <_parse_baudrate@plt>
mov    %eax,%ebx
test   %ebx,%ebx
je     11ba <_open_serial+0x6a>
mov    0x2e76(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    (%r14),%rdi
mov    0x2e59(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11e6 <_open_serial+0x96>
mov    %ebp,%edi
mov    %ebx,%esi
xor    %edx,%edx
call   1030 <_set_serial_attribs@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <_set_blocking@plt>
jmp    11e6 <_open_serial+0x96>
mov    0x2dfc(%rip),%rax
mov    (%rax),%ebx
test   %ebx,%ebx
jne    116b <_open_serial+0x1b>
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    0xe36(%rip),%rdi
call   1050 <fputs@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    0x2de1(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@B115200 = global i32 0
@.str = [18 x i8] c"Invalid baudrate\0A\00"
@stderr = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
define i32 @_open_serial(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @B115200
store i32 %8, i32* %6
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @_parse_baudrate(i8* %14)
store i32 %15, i32* %6
br label %16
16: ; preds = %11, %2
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = call i32 @fputs(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = load i32, i32* @EINVAL
store i32 %22, i32* @errno
store i32 -1, i32* %3
br label %44
23: ; preds = %16
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 0
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_RDWR
%28 = load i32, i32* @O_NOCTTY
%29 = or i32 %27, %28
%30 = load i32, i32* @O_SYNC
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %26, i32 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %23
%36 = load i32, i32* %7
store i32 %36, i32* %3
br label %44
37: ; preds = %23
%38 = load i32, i32* %7
%39 = load i32, i32* %6
%40 = call i32 @_set_serial_attribs(i32 %38, i32 %39, i32 0)
%41 = load i32, i32* %7
%42 = call i32 @_set_blocking(i32 %41, i32 1)
%43 = load i32, i32* %7
store i32 %43, i32* %3
br label %44
44: ; preds = %37, %35, %19
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@B115200 = global i32 0
@.str = [18 x i8] c"Invalid baudrate\0A\00"
@stderr = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
define i32 @_open_serial(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @B115200
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @_parse_baudrate(i8* %7)
br label %9
9: ; preds = %5, %2
%10 = phi i32 [ %8, %5 ], [ %3, %2 ]
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 @fputs(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @EINVAL
store i32 %15, i32* @errno
br label %28
16: ; preds = %9
%17 = load i8*, i8** %1
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_NOCTTY
%20 = or i32 %19, %18
%21 = load i32, i32* @O_SYNC
%22 = or i32 %20, %21
%23 = tail call i32 @open(i8* %17, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %16
%26 = tail call i32 @_set_serial_attribs(i32 %23, i32 %10, i32 0)
%27 = tail call i32 @_set_blocking(i32 %23, i32 1)
br label %28
28: ; preds = %16, %25, %12
%29 = phi i32 [ -1, %12 ], [ %23, %25 ], [ %23, %16 ]
ret i32 %29
}