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

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(unsigned char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ help();
int /*<<< orphan*/ irecv(int);
int /*<<< orphan*/ isend(int, unsigned char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strncmp(unsigned char *, char *, int);
void send_and_recv_loop(int serial_handle) {
  fd_set fds;
  while (1) {
    unsigned char command[100];
    unsigned char *c;
    int err;
    printf("\nsmart>");
    fflush(stdout);
    fgets(command, 100, stdin);
    c = command;
    while (*c == ' ' || *c == '\t')
      c++;
    if (!strncmp(c, "quit", 4))
      break;
    else if (!strncmp(c, "send", 4)) {
      isend(serial_handle, c);
      irecv(serial_handle);
    } else if (!strncmp(c, "recv", 4))
      irecv(serial_handle);
    else if (!strncmp(c, "help", 4))
      help();
    else
      printf("Invalid command\n");
  }
}


x86 assembly (clang, -O0):
<send_and_recv_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe7b(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
lea    -0x70(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1060 <fgets@plt>
lea    -0x70(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
movzbl (%rax),%ecx
mov    $0x1,%al
cmp    $0x20,%ecx
mov    %al,-0x7d(%rbp)
je     11dc <send_and_recv_loop+0x6c>
mov    -0x78(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x9,%eax
sete   %al
mov    %al,-0x7d(%rbp)
mov    -0x7d(%rbp),%al
test   $0x1,%al
jne    11ec <send_and_recv_loop+0x7c>
jmp    11fd <send_and_recv_loop+0x8d>
mov    -0x78(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x78(%rbp)
jmp    11b7 <send_and_recv_loop+0x47>
mov    -0x78(%rbp),%rdi
lea    0xe00(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1220 <send_and_recv_loop+0xb0>
jmp    12cc <send_and_recv_loop+0x15c>
mov    -0x78(%rbp),%rdi
lea    0xde2(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1257 <send_and_recv_loop+0xe7>
mov    -0x4(%rbp),%edi
mov    -0x78(%rbp),%rsi
call   1070 <isend@plt>
mov    -0x4(%rbp),%edi
call   1040 <irecv@plt>
jmp    12c2 <send_and_recv_loop+0x152>
mov    -0x78(%rbp),%rdi
lea    0xdb0(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1282 <send_and_recv_loop+0x112>
mov    -0x4(%rbp),%edi
call   1040 <irecv@plt>
jmp    12bd <send_and_recv_loop+0x14d>
mov    -0x78(%rbp),%rdi
lea    0xd8a(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    12ac <send_and_recv_loop+0x13c>
mov    $0x0,%al
call   1090 <help@plt>
jmp    12b8 <send_and_recv_loop+0x148>
lea    0xd69(%rip),%rdi
call   1050 <printf@plt>
jmp    12bd <send_and_recv_loop+0x14d>
jmp    12c2 <send_and_recv_loop+0x152>
jmp    12c7 <send_and_recv_loop+0x157>
jmp    117e <send_and_recv_loop+0xe>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_and_recv_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %edi,%ebp
mov    %rsp,%r13
mov    0x2e4e(%rip),%r14
lea    0xe67(%rip),%r15
lea    0xe65(%rip),%r12
jmp    11c1 <send_and_recv_loop+0x41>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
call   1080 <isend@plt>
mov    %ebp,%edi
call   1050 <irecv@plt>
lea    0xe38(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    (%r14),%edx
mov    %r13,%rdi
mov    $0x64,%esi
call   1070 <fgets@plt>
mov    %r13,%rbx
jmp    11f4 <send_and_recv_loop+0x74>
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x20,%al
je     11f0 <send_and_recv_loop+0x70>
cmp    $0x9,%al
je     11f0 <send_and_recv_loop+0x70>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     1278 <send_and_recv_loop+0xf8>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     11b0 <send_and_recv_loop+0x30>
mov    %rbx,%rdi
lea    0xde1(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     11ba <send_and_recv_loop+0x3a>
mov    %rbx,%rdi
lea    0xdca(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     126c <send_and_recv_loop+0xec>
lea    0xdba(%rip),%rdi
call   1040 <puts@plt>
jmp    11c1 <send_and_recv_loop+0x41>
xor    %eax,%eax
call   10a0 <help@plt>
jmp    11c1 <send_and_recv_loop+0x41>
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"\0Asmart>\00"
@stdout = global i32 0
@stdin = global i32 0
@.str.1 = [5 x i8] c"quit\00"
@.str.2 = [5 x i8] c"send\00"
@.str.3 = [5 x i8] c"recv\00"
@.str.4 = [5 x i8] c"help\00"
@.str.5 = [17 x i8] c"Invalid command\0A\00"
define void @send_and_recv_loop(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %2
br label %7
7: ; preds = %1, %63
%8 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @stdout
%10 = call i32 @fflush(i32 %9)
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%12 = load i32, i32* @stdin
%13 = call i32 @fgets(i8* %11, i32 100, i32 %12)
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
store i8* %14, i8** %5
br label %15
15: ; preds = %27, %7
%16 = load i8*, i8** %5
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = icmp eq i32 %18, 32
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = load i8*, i8** %5
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = icmp eq i32 %23, 9
br label %25
25: ; preds = %20, %15
%26 = phi i1 [ true, %15 ], [ %24, %20 ]
br i1 %26, label %27, label %30
27: ; preds = %25
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %5
br label %15
30: ; preds = %25
%31 = load i8*, i8** %5
%32 = call i32 @strncmp(i8* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%33 = icmp ne i32 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %30
br label %64
35: ; preds = %30
%36 = load i8*, i8** %5
%37 = call i32 @strncmp(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%38 = icmp ne i32 %37, 0
br i1 %38, label %45, label %39
39: ; preds = %35
%40 = load i32, i32* %2
%41 = load i8*, i8** %5
%42 = call i32 @isend(i32 %40, i8* %41)
%43 = load i32, i32* %2
%44 = call i32 @irecv(i32 %43)
br label %62
45: ; preds = %35
%46 = load i8*, i8** %5
%47 = call i32 @strncmp(i8* %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%48 = icmp ne i32 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %45
%50 = load i32, i32* %2
%51 = call i32 @irecv(i32 %50)
br label %61
52: ; preds = %45
%53 = load i8*, i8** %5
%54 = call i32 @strncmp(i8* %53, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%55 = icmp ne i32 %54, 0
br i1 %55, label %58, label %56
56: ; preds = %52
%57 = call i32 (...) @help()
br label %60
58: ; preds = %52
%59 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %60
60: ; preds = %58, %56
br label %61
61: ; preds = %60, %49
br label %62
62: ; preds = %61, %39
br label %63
63: ; preds = %62
br label %7
64: ; preds = %34
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"\0Asmart>\00"
@stdout = global i32 0
@stdin = global i32 0
@.str.1 = [5 x i8] c"quit\00"
@.str.2 = [5 x i8] c"send\00"
@.str.3 = [5 x i8] c"recv\00"
@.str.4 = [5 x i8] c"help\00"
@str = [16 x i8] c"Invalid command\00"
define void @send_and_recv_loop(i32 %0) {
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %36, %1
%5 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @stdout
%7 = call i32 @fflush(i32 %6)
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %3, i32 100, i32 %8)
br label %10
10: ; preds = %13, %4
%11 = phi i8* [ %3, %4 ], [ %14, %13 ]
%12 = load i8, i8* %11
switch i8 %12, label %15 [
i8 32, label %13
i8 9, label %13
]
13: ; preds = %10, %10
%14 = getelementptr inbounds i8, i8* %11, i64 1
br label %10
15: ; preds = %10
%16 = call i32 @strncmp(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %37, label %18
18: ; preds = %15
%19 = call i32 @strncmp(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = call i32 @isend(i32 %0, i8* %11)
%23 = call i32 @irecv(i32 %0)
br label %36
24: ; preds = %18
%25 = call i32 @strncmp(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @irecv(i32 %0)
br label %36
29: ; preds = %24
%30 = call i32 @strncmp(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 (...) @help()
br label %36
34: ; preds = %29
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %36
36: ; preds = %27, %34, %32, %21
br label %4
37: ; preds = %15
ret void
}