#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 atoi(char *);
int /*<<< orphan*/ close(int);
int commandToInt(char *);
int createClientConnection(int, char *);
char *getInput(int /*<<< orphan*/, int);
int kbhit();
int /*<<< orphan*/ nonblock(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ readSock(int);
int /*<<< orphan*/ sendCommand(int, char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strtok(char *, char *);
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  int port = atoi(argv[2]);
  int sock = createClientConnection(port, argv[1]);
  int i = 0;
  nonblock(0);
  while (1) {
    readSock(sock);
    usleep(1);
    i = kbhit();
    if (i != 0) {
      char *req = getInput(stdin, 10);
      char pch[strlen(req)];
      strcpy(pch, req);
      strtok(pch, " ");
      if (pch != NULL) {
        switch (commandToInt(pch)) {
        case 1:
        case 2:
        case 3:
        case 4:
          sendCommand(sock, req);
          break;
        case 5:
          sendCommand(sock, req);
          close(sock);
          return 0;
        default:
          printf("[ERROR]Invalid command.\n");
        }
      }
      i = 0;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   10c0 <createClientConnection@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
xor    %edi,%edi
call   1050 <nonblock@plt>
mov    -0x18(%rbp),%edi
call   1030 <readSock@plt>
mov    $0x1,%edi
call   1100 <usleep@plt>
mov    $0x0,%al
call   1090 <kbhit@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1350 <main+0x170>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
mov    $0xa,%esi
call   10a0 <getInput@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rsi
call   1060 <strcpy@plt>
mov    -0x48(%rbp),%rdi
lea    0xd5a(%rip),%rsi
call   10d0 <strtok@plt>
mov    -0x48(%rbp),%rdi
call   10e0 <commandToInt@plt>
mov    %eax,%ecx
mov    %ecx,%eax
add    $0xffffffff,%eax
mov    %ecx,-0x40(%rbp)
sub    $0x4,%eax
jb     12dd <main+0xfd>
jmp    12cc <main+0xec>
mov    -0x40(%rbp),%eax
sub    $0x5,%eax
je     12ee <main+0x10e>
jmp    1315 <main+0x135>
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1040 <sendCommand@plt>
jmp    1321 <main+0x141>
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1040 <sendCommand@plt>
mov    -0x18(%rbp),%edi
call   10b0 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    132f <main+0x14f>
lea    0xce6(%rip),%rdi
call   1080 <printf@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x3c(%rbp),%eax
test   %eax,%eax
je     134b <main+0x16b>
jmp    1346 <main+0x166>
jmp    1355 <main+0x175>
jmp    1350 <main+0x170>
jmp    1227 <main+0x47>
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
call   10f0 <atoi@plt>
mov    0x8(%rbx),%rsi
mov    %eax,%edi
call   10c0 <createClientConnection@plt>
mov    %eax,%r12d
xor    %edi,%edi
call   1050 <nonblock@plt>
lea    0xdea(%rip),%r14
jmp    122e <main+0x4e>
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %r13,%rsi
call   1040 <sendCommand@plt>
mov    %r15,%rsp
mov    %r12d,%edi
call   1030 <readSock@plt>
mov    $0x1,%edi
call   1100 <usleep@plt>
xor    %eax,%eax
call   1090 <kbhit@plt>
test   %eax,%eax
je     122e <main+0x4e>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
mov    $0xa,%esi
call   10a0 <getInput@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1080 <strlen@plt>
mov    %rsp,%r15
mov    %eax,%eax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10d0 <strtok@plt>
mov    %rbx,%rdi
call   10e0 <commandToInt@plt>
lea    -0x1(%rax),%ecx
cmp    $0x4,%ecx
jb     1220 <main+0x40>
cmp    $0x5,%eax
je     12c2 <main+0xe2>
lea    0xd4d(%rip),%rdi
call   1070 <puts@plt>
mov    %r15,%rsp
jmp    122e <main+0x4e>
mov    %r12d,%edi
mov    %r13,%rsi
call   1040 <sendCommand@plt>
mov    %r12d,%edi
call   10b0 <close@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [2 x i8] c" \00"
@.str.1 = [25 x i8] c"[ERROR]Invalid command.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 2
%15 = load i8*, i8** %14
%16 = call i32 @atoi(i8* %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @createClientConnection(i32 %17, i8* %20)
store i32 %21, i32* %7
store i32 0, i32* %8
%22 = call i32 @nonblock(i32 0)
br label %23
23: ; preds = %2, %59
%24 = load i32, i32* %7
%25 = call i32 @readSock(i32 %24)
%26 = call i32 @usleep(i32 1)
%27 = call i32 (...) @kbhit()
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %59
30: ; preds = %23
%31 = load i32, i32* @stdin
%32 = call i8* @getInput(i32 %31, i32 10)
store i8* %32, i8** %9
%33 = load i8*, i8** %9
%34 = call i32 @strlen(i8* %33)
%35 = zext i32 %34 to i64
%36 = call i8* @llvm.stacksave()
store i8* %36, i8** %10
%37 = alloca i8, i64 %35
store i64 %35, i64* %11
%38 = load i8*, i8** %9
%39 = call i32 @strcpy(i8* %37, i8* %38)
%40 = call i32 @strtok(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%41 = call i32 @commandToInt(i8* %37)
switch i32 %41, label %52 [
i32 1, label %42
i32 2, label %42
i32 3, label %42
i32 4, label %42
i32 5, label %46
]
42: ; preds = %30, %30, %30, %30
%43 = load i32, i32* %7
%44 = load i8*, i8** %9
%45 = call i32 @sendCommand(i32 %43, i8* %44)
br label %54
46: ; preds = %30
%47 = load i32, i32* %7
%48 = load i8*, i8** %9
%49 = call i32 @sendCommand(i32 %47, i8* %48)
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
store i32 0, i32* %3
store i32 1, i32* %12
br label %55
52: ; preds = %30
%53 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %54
54: ; preds = %52, %42
store i32 0, i32* %8
store i32 0, i32* %12
br label %55
55: ; preds = %54, %46
%56 = load i8*, i8** %10
%57 = load i32, i32* %12
switch i32 %57, label %62 [
i32 0, label %58
i32 1, label %60
]
58: ; preds = %55
br label %59
59: ; preds = %58, %23
br label %23
60: ; preds = %55
%61 = load i32, i32* %3
ret i32 %61
62: ; preds = %55
unreachable
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [2 x i8] c" \00"
@str = [24 x i8] c"[ERROR]Invalid command.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 2
%4 = load i8*, i8** %3
%5 = tail call i32 @atoi(i8* %4)
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @createClientConnection(i32 %5, i8* %7)
%9 = tail call i32 @nonblock(i32 0)
br label %10
10: ; preds = %33, %2
%11 = call i32 @readSock(i32 %8)
%12 = call i32 @usleep(i32 1)
%13 = call i32 (...) @kbhit()
%14 = icmp eq i32 %13, 0
br i1 %14, label %33, label %15
15: ; preds = %10
%16 = load i32, i32* @stdin
%17 = call i8* @getInput(i32 %16, i32 10)
%18 = call i32 @strlen(i8* %17)
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
%21 = alloca i8, i64 %19
%22 = call i32 @strcpy(i8* %21, i8* %17)
%23 = call i32 @strtok(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @commandToInt(i8* %21)
switch i32 %24, label %30 [
i32 1, label %25
i32 2, label %25
i32 3, label %25
i32 4, label %25
i32 5, label %27
]
25: ; preds = %15, %15, %15, %15
%26 = call i32 @sendCommand(i32 %8, i8* %17)
br label %32
27: ; preds = %15
%28 = call i32 @sendCommand(i32 %8, i8* %17)
%29 = call i32 @close(i32 %8)
ret i32 0
30: ; preds = %15
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %32
32: ; preds = %25, %30
br label %33
33: ; preds = %32, %10
br label %10
}