#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 /*<<< orphan*/ CloseAdrPort();
scalar_t__ OpenAdrPort(char *);
int /*<<< orphan*/ atoi(char *);
int close_gatectl();
int init_gatectl(int /*<<< orphan*/);
int open_gate(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  int nret = 0;
  if (argc < 2 || argc > 4) {
    printf("adrserial needs 3 parameters for the serial port\n");
    printf("  ie. use 'gatectld port command pin' 'gatectld 0' to connect to "
           "/dev/ttyS0\n");
    return 0;
  }
  if (OpenAdrPort(argv[1]) < 0)
    return 0;
  if (argc == 2)
    return init_gatectl(atoi(argv[1]));
  if (strcmp(argv[2], "init") == 0)
    nret = init_gatectl(atoi(argv[1]));
  else if (strcmp(argv[2], "open") == 0)
    nret = open_gate(atoi(argv[3]));
  else if (strcmp(argv[2], "exit") == 0)
    nret = close_gatectl();
  CloseAdrPort();
  return nret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11b1 <main+0x31>
cmpl   $0x4,-0x8(%rbp)
jle    11d5 <main+0x55>
lea    0xe48(%rip),%rdi
call   1050 <printf@plt>
lea    0xe6e(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d1 <main+0x151>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <OpenAdrPort@plt>
cmp    $0x0,%rax
jge    11f8 <main+0x78>
movl   $0x0,-0x4(%rbp)
jmp    12d1 <main+0x151>
cmpl   $0x2,-0x8(%rbp)
jne    121e <main+0x9e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1080 <init_gatectl@plt>
mov    %eax,-0x4(%rbp)
jmp    12d1 <main+0x151>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe52(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1258 <main+0xd8>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1080 <init_gatectl@plt>
mov    %eax,-0x14(%rbp)
jmp    12c4 <main+0x144>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe1d(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1292 <main+0x112>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1030 <open_gate@plt>
mov    %eax,-0x14(%rbp)
jmp    12bf <main+0x13f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xde8(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12ba <main+0x13a>
mov    $0x0,%al
call   1090 <close_gatectl@plt>
mov    %eax,-0x14(%rbp)
jmp    12bf <main+0x13f>
jmp    12c4 <main+0x144>
mov    $0x0,%al
call   1040 <CloseAdrPort@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    -0x5(%rbx),%eax
cmp    $0xfffffffc,%eax
ja     11ad <main+0x2d>
lea    0xe7a(%rip),%rdi
call   1040 <puts@plt>
lea    0xe9f(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
jmp    1253 <main+0xd3>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
call   1070 <OpenAdrPort@plt>
test   %rax,%rax
js     11da <main+0x5a>
cmp    $0x2,%ebx
jne    11de <main+0x5e>
mov    0x8(%r14),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1080 <init_gatectl@plt>
xor    %ebx,%ebx
jmp    1253 <main+0xd3>
mov    0x10(%r14),%rdi
lea    0xe17(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1228 <main+0xa8>
mov    0x10(%r14),%rdi
lea    0xe07(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     123a <main+0xba>
mov    0x10(%r14),%rdi
lea    0xdf7(%rip),%rsi
call   1060 <strcmp@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    124c <main+0xcc>
xor    %eax,%eax
call   1090 <close_gatectl@plt>
jmp    124a <main+0xca>
mov    0x8(%r14),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1080 <init_gatectl@plt>
jmp    124a <main+0xca>
mov    0x18(%r14),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1030 <open_gate@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <CloseAdrPort@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [50 x i8] c"adrserial needs 3 parameters for the serial port\0A\00"
@.str.1 = [77 x i8] c" ie. use 'gatectld port command pin' 'gatectld 0' to connect to /dev/ttyS0\0A\00"
@.str.2 = [5 x i8] c"init\00"
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [5 x i8] c"exit\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 2
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 4
br i1 %11, label %12, label %15
12: ; preds = %9, %2
%13 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @printf(i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %68
15: ; preds = %9
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i64 @OpenAdrPort(i8* %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
store i32 0, i32* %3
br label %68
22: ; preds = %15
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, 2
br i1 %24, label %25, label %31
25: ; preds = %22
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
%30 = call i32 @init_gatectl(i32 %29)
store i32 %30, i32* %3
br label %68
31: ; preds = %22
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i64 @strcmp(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %31
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i32 @atoi(i8* %40)
%42 = call i32 @init_gatectl(i32 %41)
store i32 %42, i32* %6
br label %65
43: ; preds = %31
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 2
%46 = load i8*, i8** %45
%47 = call i64 @strcmp(i8* %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %43
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 3
%52 = load i8*, i8** %51
%53 = call i32 @atoi(i8* %52)
%54 = call i32 @open_gate(i32 %53)
store i32 %54, i32* %6
br label %64
55: ; preds = %43
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 2
%58 = load i8*, i8** %57
%59 = call i64 @strcmp(i8* %58, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %55
%62 = call i32 (...) @close_gatectl()
store i32 %62, i32* %6
br label %63
63: ; preds = %61, %55
br label %64
64: ; preds = %63, %49
br label %65
65: ; preds = %64, %37
%66 = call i32 (...) @CloseAdrPort()
%67 = load i32, i32* %6
store i32 %67, i32* %3
br label %68
68: ; preds = %65, %25, %21, %12
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@.str.2 = [5 x i8] c"init\00"
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [5 x i8] c"exit\00"
@str = [49 x i8] c"adrserial needs 3 parameters for the serial port\00"
@str.5 = [76 x i8] c" ie. use 'gatectld port command pin' 'gatectld 0' to connect to /dev/ttyS0\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add i32 %0, -5
%4 = icmp ult i32 %3, -3
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([76 x i8], [76 x i8]* @str.5, i64 0, i64 0))
br label %46
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @OpenAdrPort(i8* %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %46, label %13
13: ; preds = %8
%14 = icmp eq i32 %0, 2
br i1 %14, label %15, label %19
15: ; preds = %13
%16 = load i8*, i8** %9
%17 = tail call i32 @atoi(i8* %16)
%18 = tail call i32 @init_gatectl(i32 %17)
br label %46
19: ; preds = %13
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = tail call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i8*, i8** %9
%26 = tail call i32 @atoi(i8* %25)
%27 = tail call i32 @init_gatectl(i32 %26)
br label %43
28: ; preds = %19
%29 = load i8*, i8** %20
%30 = tail call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = getelementptr inbounds i8*, i8** %1, i64 3
%34 = load i8*, i8** %33
%35 = tail call i32 @atoi(i8* %34)
%36 = tail call i32 @open_gate(i32 %35)
br label %43
37: ; preds = %28
%38 = load i8*, i8** %20
%39 = tail call i64 @strcmp(i8* %38, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = tail call i32 (...) @close_gatectl()
br label %43
43: ; preds = %32, %41, %37, %24
%44 = phi i32 [ %27, %24 ], [ %36, %32 ], [ %42, %41 ], [ 0, %37 ]
%45 = tail call i32 (...) @CloseAdrPort()
br label %46
46: ; preds = %8, %43, %15, %5
%47 = phi i32 [ 0, %5 ], [ %18, %15 ], [ %44, %43 ], [ 0, %8 ]
ret i32 %47
}