#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 */
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int _parse_baudrate(char *, unsigned int *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int);
int /*<<< orphan*/ set_blocking(int, int);
int /*<<< orphan*/ set_serial_attribs(int, unsigned int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int _open_serial_connection(char *name, char *baudrate_arg) {
  unsigned baudrate = 0;
  if (_parse_baudrate(baudrate_arg, &baudrate) == -1) {
    fprintf(stderr, "Invalid baudrate specified: %s\n", baudrate_arg);
    return 1;
  }
  int serial_fd = open(name, O_RDWR | O_NOCTTY | O_SYNC);
  if (serial_fd < 0) {
    fprintf(stderr, "Error opening serial device %s\n", name);
    return -1;
  }
  set_serial_attribs(serial_fd, baudrate, 0);
  set_blocking(serial_fd, 1);
  return serial_fd;
}


x86 assembly (clang, -O0):
<_open_serial_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1070 <_parse_baudrate@plt>
cmp    $0xffffffff,%eax
jne    11a2 <_open_serial_connection+0x52>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe6f(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1218 <_open_serial_connection+0xc8>
mov    -0x10(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11f8 <_open_serial_connection+0xa8>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe39(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <_open_serial_connection+0xc8>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
xor    %edx,%edx
call   1030 <set_serial_attribs@plt>
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1040 <set_blocking@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_open_serial_connection>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %rdi,%rbx
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <_parse_baudrate@plt>
cmp    $0xffffffff,%eax
je     11b7 <_open_serial_connection+0x67>
mov    0x2e6e(%rip),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e54(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11d6 <_open_serial_connection+0x86>
mov    %eax,%ebp
mov    0x4(%rsp),%esi
mov    %eax,%edi
xor    %edx,%edx
call   1030 <set_serial_attribs@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <set_blocking@plt>
jmp    11f3 <_open_serial_connection+0xa3>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
mov    %rbp,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%ebp
jmp    11f3 <_open_serial_connection+0xa3>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"Invalid baudrate specified: %s\0A\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.1 = [32 x i8] c"Error opening serial device %s\0A\00"
define i32 @_open_serial_connection(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
%8 = load i8*, i8** %5
%9 = call i32 @_parse_baudrate(i8* %8, i32* %6)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %5
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 1, i32* %3
br label %36
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_NOCTTY
%19 = or i32 %17, %18
%20 = load i32, i32* @O_SYNC
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %16, i32 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %15
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %4
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %27)
store i32 -1, i32* %3
br label %36
29: ; preds = %15
%30 = load i32, i32* %7
%31 = load i32, i32* %6
%32 = call i32 @set_serial_attribs(i32 %30, i32 %31, i32 0)
%33 = load i32, i32* %7
%34 = call i32 @set_blocking(i32 %33, i32 1)
%35 = load i32, i32* %7
store i32 %35, i32* %3
br label %36
36: ; preds = %29, %25, %11
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"Invalid baudrate specified: %s\0A\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.1 = [32 x i8] c"Error opening serial device %s\0A\00"
define i32 @_open_serial_connection(i8* %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = call i32 @_parse_baudrate(i8* %1, i32* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %25
10: ; preds = %2
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %12, %11
%14 = load i32, i32* @O_SYNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %0, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %25
21: ; preds = %10
%22 = load i32, i32* %3
%23 = call i32 @set_serial_attribs(i32 %16, i32 %22, i32 0)
%24 = call i32 @set_blocking(i32 %16, i32 1)
br label %25
25: ; preds = %18, %21, %7
%26 = phi i32 [ 1, %7 ], [ -1, %18 ], [ %16, %21 ]
ret i32 %26
}