#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 */
typedef int /*<<< orphan*/ int32_t;

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int _select_baudrate(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ log_log(char *, int /*<<< orphan*/, char *, char *,
                           char const *, ...);
int open(char const *, int);
scalar_t__ serial_port_setup(int, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int serial_port_open(const char *portname, int32_t baudrate) {
  int fd;
  fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd < 0) {
    log_log("ERROR", 0,
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/shawnchain/HomeMaster/refs/heads/master/"
            "iracc/serial_port.c",
            "*** open() \"%s\": %s", portname, strerror(errno));
    return -1;
  }
  int speed = _select_baudrate(baudrate);
  if (speed < 0) {
    log_log("ERROR", 0,
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/shawnchain/HomeMaster/refs/heads/master/"
            "iracc/serial_port.c",
            "*** unsupported baudrate %d", baudrate);
    return -1;
  }
  if (serial_port_setup(fd, speed) < 0) {
    close(fd);
    fd = -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<serial_port_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    0x2e4d(%rip),%rax
or     (%rax),%esi
mov    0x2e54(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11e7 <serial_port_open+0x87>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x28(%rbp),%r8
mov    %eax,%r9d
lea    0xe3c(%rip),%rdi
xor    %esi,%esi
lea    0xe39(%rip),%rdx
lea    0xec8(%rip),%rcx
mov    $0x0,%al
call   1070 <log_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <serial_port_open+0xf4>
mov    -0x14(%rbp),%edi
call   1030 <_select_baudrate@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    122a <serial_port_open+0xca>
movslq -0x14(%rbp),%r8
lea    0xdf9(%rip),%rdi
xor    %esi,%esi
lea    0xdf6(%rip),%rdx
lea    0xe99(%rip),%rcx
mov    $0x0,%al
call   1070 <log_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <serial_port_open+0xf4>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1060 <serial_port_setup@plt>
cmp    $0x0,%rax
jge    124e <serial_port_open+0xee>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_port_open>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e7d(%rip),%rcx
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e5e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1050 <open@plt>
test   %eax,%eax
js     11b3 <serial_port_open+0x53>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <_select_baudrate@plt>
test   %eax,%eax
js     11e7 <serial_port_open+0x87>
mov    %ebx,%edi
mov    %eax,%esi
call   1060 <serial_port_setup@plt>
test   %rax,%rax
jns    120d <serial_port_open+0xad>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1208 <serial_port_open+0xa8>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe38(%rip),%rdi
lea    0xe37(%rip),%rdx
lea    0xec6(%rip),%rcx
xor    %esi,%esi
mov    %r14,%r8
mov    %eax,%r9d
xor    %eax,%eax
call   1070 <log_log@plt>
jmp    1208 <serial_port_open+0xa8>
movslq %ebp,%r8
lea    0xe0f(%rip),%rdi
lea    0xe0e(%rip),%rdx
lea    0xeb1(%rip),%rcx
xor    %esi,%esi
xor    %eax,%eax
call   1070 <log_log@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str = [6 x i8] c"ERROR\00"
@.str.1 = [150 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/shawnchain/HomeMaster/refs/heads/master/iracc/serial_port.c\00"
@.str.2 = [20 x i8] c"*** open() \22%s\22: %s\00"
@errno = global i32 0
@.str.3 = [28 x i8] c"*** unsupported baudrate %d\00"
define i32 @serial_port_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = load i32, i32* @O_SYNC
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %8, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @log_log(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 0, i8* getelementptr inbounds ([150 x i8], [150 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %18, i32 %20)
store i32 -1, i32* %3
br label %42
22: ; preds = %2
%23 = load i32, i32* %5
%24 = call i32 @_select_baudrate(i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = inttoptr i64 %29 to i8*
%31 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @log_log(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 0, i8* getelementptr inbounds ([150 x i8], [150 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %30)
store i32 -1, i32* %3
br label %42
32: ; preds = %22
%33 = load i32, i32* %6
%34 = load i32, i32* %7
%35 = call i64 @serial_port_setup(i32 %33, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %6
br label %40
40: ; preds = %37, %32
%41 = load i32, i32* %6
store i32 %41, i32* %3
br label %42
42: ; preds = %40, %27, %17
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str = [6 x i8] c"ERROR\00"
@.str.1 = [150 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/shawnchain/HomeMaster/refs/heads/master/iracc/serial_port.c\00"
@.str.2 = [20 x i8] c"*** open() \22%s\22: %s\00"
@errno = global i32 0
@.str.3 = [28 x i8] c"*** unsupported baudrate %d\00"
define i32 @serial_port_open(i8* %0, i32 %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = load i32, i32* @O_SYNC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = tail call i32 @strerror(i32 %11)
%13 = tail call i32 (i8*, i32, i8*, i8*, i8*, ...) @log_log(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 0, i8* getelementptr inbounds ([150 x i8], [150 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %12)
br label %26
14: ; preds = %2
%15 = tail call i32 @_select_baudrate(i32 %1)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = sext i32 %1 to i64
%19 = inttoptr i64 %18 to i8*
%20 = tail call i32 (i8*, i32, i8*, i8*, i8*, ...) @log_log(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 0, i8* getelementptr inbounds ([150 x i8], [150 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %19)
br label %26
21: ; preds = %14
%22 = tail call i64 @serial_port_setup(i32 %8, i32 %15)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @close(i32 %8)
br label %26
26: ; preds = %17, %24, %21, %10
%27 = phi i32 [ -1, %10 ], [ -1, %17 ], [ -1, %24 ], [ %8, %21 ]
ret i32 %27
}