#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 */
scalar_t__ auroraCheck(int);
int /*<<< orphan*/ comClose(int);
int comFindPort(char const *);
int comGetNoPorts();
int /*<<< orphan*/ comOpen(int, int);
int /*<<< orphan*/ printf(char *, char const *);
int auroraConnect(const char *port, int baudrate) {
  int p;
  if (port) {
    p = comFindPort(port);
    if (p == -1) {
      printf("Port not available: %s !\n", port);
      return -1;
    }
    if (!comOpen(p, baudrate)) {
      printf("Unable to open port %s !\n", port);
      return -1;
    }
    if (auroraCheck(p))
      return p;
    else
      comClose(p);
  } else {
    int noPorts = comGetNoPorts();
    for (p = 0; p < noPorts; p++) {
      if (!comOpen(p, baudrate))
        continue;
      if (auroraCheck(p))
        return p;
      else
        comClose(p);
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<auroraConnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1206 <auroraConnect+0xa6>
mov    -0x10(%rbp),%rdi
call   1080 <comFindPort@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ac <auroraConnect+0x4c>
mov    -0x10(%rbp),%rsi
lea    0xe65(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <auroraConnect+0x120>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1070 <comOpen@plt>
cmp    $0x0,%eax
jne    11dc <auroraConnect+0x7c>
mov    -0x10(%rbp),%rsi
lea    0xe4f(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <auroraConnect+0x120>
mov    -0x18(%rbp),%edi
call   1060 <auroraCheck@plt>
cmp    $0x0,%rax
je     11f9 <auroraConnect+0x99>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1280 <auroraConnect+0x120>
mov    -0x18(%rbp),%edi
call   1030 <comClose@plt>
jmp    1279 <auroraConnect+0x119>
mov    $0x0,%al
call   1050 <comGetNoPorts@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1274 <auroraConnect+0x114>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1070 <comOpen@plt>
cmp    $0x0,%eax
jne    123c <auroraConnect+0xdc>
jmp    1266 <auroraConnect+0x106>
mov    -0x18(%rbp),%edi
call   1060 <auroraCheck@plt>
cmp    $0x0,%rax
je     1259 <auroraConnect+0xf9>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1280 <auroraConnect+0x120>
mov    -0x18(%rbp),%edi
call   1030 <comClose@plt>
jmp    1266 <auroraConnect+0x106>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1217 <auroraConnect+0xb7>
jmp    1279 <auroraConnect+0x119>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<auroraConnect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
test   %rdi,%rdi
je     11ae <auroraConnect+0x4e>
mov    %rdi,%rbx
call   1080 <comFindPort@plt>
cmp    $0xffffffff,%eax
je     11fa <auroraConnect+0x9a>
mov    %eax,%r14d
mov    %eax,%edi
mov    %r15d,%esi
call   1070 <comOpen@plt>
test   %eax,%eax
je     1203 <auroraConnect+0xa3>
mov    %r14d,%edi
call   1060 <auroraCheck@plt>
test   %rax,%rax
jne    121d <auroraConnect+0xbd>
mov    %r14d,%edi
call   1030 <comClose@plt>
mov    $0xffffffff,%r14d
jmp    121d <auroraConnect+0xbd>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <comGetNoPorts@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
jle    121d <auroraConnect+0xbd>
mov    %eax,%ebp
jmp    11d7 <auroraConnect+0x77>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%ebx
cmp    %ebx,%ebp
je     121d <auroraConnect+0xbd>
mov    %ebx,%edi
mov    %r15d,%esi
call   1070 <comOpen@plt>
test   %eax,%eax
je     11d0 <auroraConnect+0x70>
mov    %ebx,%edi
call   1060 <auroraCheck@plt>
test   %rax,%rax
jne    121a <auroraConnect+0xba>
mov    %ebx,%edi
call   1030 <comClose@plt>
jmp    11d0 <auroraConnect+0x70>
lea    0xdff(%rip),%rdi
jmp    120a <auroraConnect+0xaa>
lea    0xe10(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%r14d
jmp    121d <auroraConnect+0xbd>
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Port not available: %s !\0A\00"
@.str.1 = [26 x i8] c"Unable to open port %s !\0A\00"
define i32 @auroraConnect(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 = icmp ne i8* %8, null
br i1 %9, label %10, label %36
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = call i32 @comFindPort(i8* %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i8*, i8** %4
%17 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %63
18: ; preds = %10
%19 = load i32, i32* %6
%20 = load i32, i32* %5
%21 = call i32 @comOpen(i32 %19, i32 %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %18
%24 = load i8*, i8** %4
%25 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %24)
store i32 -1, i32* %3
br label %63
26: ; preds = %18
%27 = load i32, i32* %6
%28 = call i64 @auroraCheck(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i32, i32* %6
store i32 %31, i32* %3
br label %63
32: ; preds = %26
%33 = load i32, i32* %6
%34 = call i32 @comClose(i32 %33)
br label %35
35: ; preds = %32
br label %62
36: ; preds = %2
%37 = call i32 (...) @comGetNoPorts()
store i32 %37, i32* %7
store i32 0, i32* %6
br label %38
38: ; preds = %58, %36
%39 = load i32, i32* %6
%40 = load i32, i32* %7
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %61
42: ; preds = %38
%43 = load i32, i32* %6
%44 = load i32, i32* %5
%45 = call i32 @comOpen(i32 %43, i32 %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %48, label %47
47: ; preds = %42
br label %58
48: ; preds = %42
%49 = load i32, i32* %6
%50 = call i64 @auroraCheck(i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = load i32, i32* %6
store i32 %53, i32* %3
br label %63
54: ; preds = %48
%55 = load i32, i32* %6
%56 = call i32 @comClose(i32 %55)
br label %57
57: ; preds = %54
br label %58
58: ; preds = %57, %47
%59 = load i32, i32* %6
%60 = add nsw i32 %59, 1
store i32 %60, i32* %6
br label %38
61: ; preds = %38
br label %62
62: ; preds = %61, %35
store i32 -1, i32* %3
br label %63
63: ; preds = %62, %52, %30, %23, %15
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@.str = [26 x i8] c"Port not available: %s !\0A\00"
@.str.1 = [26 x i8] c"Unable to open port %s !\0A\00"
define i32 @auroraConnect(i8* %0, i32 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %19, label %4
4: ; preds = %2
%5 = tail call i32 @comFindPort(i8* %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %34
9: ; preds = %4
%10 = tail call i32 @comOpen(i32 %5, i32 %1)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %34
14: ; preds = %9
%15 = tail call i64 @auroraCheck(i32 %5)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %34
17: ; preds = %14
%18 = tail call i32 @comClose(i32 %5)
br label %34
19: ; preds = %2
%20 = tail call i32 (...) @comGetNoPorts()
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %34
22: ; preds = %19, %31
%23 = phi i32 [ %32, %31 ], [ 0, %19 ]
%24 = tail call i32 @comOpen(i32 %23, i32 %1)
%25 = icmp eq i32 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %22
%27 = tail call i64 @auroraCheck(i32 %23)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = tail call i32 @comClose(i32 %23)
br label %31
31: ; preds = %22, %29
%32 = add nuw nsw i32 %23, 1
%33 = icmp eq i32 %32, %20
br i1 %33, label %34, label %22
34: ; preds = %26, %31, %19, %17, %14, %12, %7
%35 = phi i32 [ -1, %7 ], [ -1, %12 ], [ %5, %14 ], [ -1, %17 ], [ -1, %19 ], [ %23, %26 ], [ -1, %31 ]
ret i32 %35
}