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

/* Variables and functions */
int /*<<< orphan*/ NAP_requestfile(char *, char *);
int /*<<< orphan*/ NAP_search(char *);
int /*<<< orphan*/ NAP_sendcommand(int /*<<< orphan*/, char *);
char *argstring(char **);
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ freeargs(char **);
int getargs(char *, char ***);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcasecmp(char *, char *);
int strlen(char *);
void KB_getcommand(void) {
  char **argv = NULL;
  int argc;
  char buff[1024];
  fgets(buff, sizeof(buff), stdin);
  buff[strlen(buff) - 1] = 0;
  argc = getargs(buff, &argv);
  if (argc > 0) {
    if (!strcasecmp(argv[0], "raw")) {
      if (argc == 1) {
        fprintf(stdout, "raw <command id> <data>\n");
      } else {
        char *data;
        if (argc == 2)
          NAP_sendcommand(atoi(argv[1]), "");
        else {
          data = argstring(&argv[2]);
          NAP_sendcommand(atoi(argv[1]), data);
          free(data);
        }
      }
    } else if (!strcasecmp(argv[0], "get")) {
      if (argc < 3) {
        fprintf(stdout, "get <name> <file>\n");
      } else {
        char *f;
        f = argstring(&argv[2]);
        NAP_requestfile(argv[1], f);
        free(f);
      }
    } else if (!strcasecmp(argv[0], "search")) {
      if (argc == 1) {
        fprintf(stdout, "search <file>");
      } else {
        char *s;
        s = argstring(&argv[1]);
        NAP_search(s);
        free(s);
      }
    }
  }
  freeargs(argv);
}


x86 assembly (clang, -O0):
<KB_getcommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movq   $0x0,-0x8(%rbp)
lea    -0x410(%rbp),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1070 <fgets@plt>
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   10d0 <getargs@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    13cb <KB_getcommand+0x20b>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
lea    0xdcf(%rip),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%eax
jne    12d2 <KB_getcommand+0x112>
cmpl   $0x1,-0xc(%rbp)
jne    1263 <KB_getcommand+0xa3>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    0xdab(%rip),%rsi
call   1080 <fprintf@plt>
jmp    12cd <KB_getcommand+0x10d>
cmpl   $0x2,-0xc(%rbp)
jne    128d <KB_getcommand+0xcd>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
lea    0xd99(%rip),%rsi
call   10b0 <NAP_sendcommand@plt>
jmp    12c8 <KB_getcommand+0x108>
mov    -0x8(%rbp),%rdi
add    $0x10,%rdi
call   1060 <argstring@plt>
mov    %rax,-0x418(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
mov    -0x418(%rbp),%rsi
call   10b0 <NAP_sendcommand@plt>
mov    -0x418(%rbp),%rdi
call   1030 <free@plt>
jmp    12cd <KB_getcommand+0x10d>
jmp    13c6 <KB_getcommand+0x206>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
lea    0xd3d(%rip),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%eax
jne    134b <KB_getcommand+0x18b>
cmpl   $0x3,-0xc(%rbp)
jge    1312 <KB_getcommand+0x152>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
lea    0xd19(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1346 <KB_getcommand+0x186>
mov    -0x8(%rbp),%rdi
add    $0x10,%rdi
call   1060 <argstring@plt>
mov    %rax,-0x420(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x420(%rbp),%rsi
call   1090 <NAP_requestfile@plt>
mov    -0x420(%rbp),%rdi
call   1030 <free@plt>
jmp    13c1 <KB_getcommand+0x201>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
lea    0xcdb(%rip),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%eax
jne    13bc <KB_getcommand+0x1fc>
cmpl   $0x1,-0xc(%rbp)
jne    138b <KB_getcommand+0x1cb>
mov    0x2c60(%rip),%rax
mov    (%rax),%edi
lea    0xcba(%rip),%rsi
call   1080 <fprintf@plt>
jmp    13b7 <KB_getcommand+0x1f7>
mov    -0x8(%rbp),%rdi
add    $0x8,%rdi
call   1060 <argstring@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rdi
call   10a0 <NAP_search@plt>
mov    -0x428(%rbp),%rdi
call   1030 <free@plt>
jmp    13bc <KB_getcommand+0x1fc>
jmp    13c1 <KB_getcommand+0x201>
jmp    13c6 <KB_getcommand+0x206>
jmp    13cb <KB_getcommand+0x20b>
mov    -0x8(%rbp),%rdi
call   10e0 <freeargs@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<KB_getcommand>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
movq   $0x0,0x8(%rsp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1070 <fgets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10d0 <getargs@plt>
test   %eax,%eax
jle    1343 <KB_getcommand+0x183>
mov    %eax,%ebp
mov    0x8(%rsp),%r14
mov    (%r14),%rbx
lea    0xddc(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcasecmp@plt>
test   %eax,%eax
je     127d <KB_getcommand+0xbd>
lea    0xde6(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcasecmp@plt>
test   %eax,%eax
je     12a1 <KB_getcommand+0xe1>
lea    0xdea(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcasecmp@plt>
test   %eax,%eax
jne    1343 <KB_getcommand+0x183>
cmp    $0x1,%ebp
jne    1324 <KB_getcommand+0x164>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    0xdc8(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1343 <KB_getcommand+0x183>
cmp    $0x2,%ebp
je     12c0 <KB_getcommand+0x100>
cmp    $0x1,%ebp
jne    12d9 <KB_getcommand+0x119>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1343 <KB_getcommand+0x183>
cmp    $0x2,%ebp
ja     1302 <KB_getcommand+0x142>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
lea    0xd6b(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1343 <KB_getcommand+0x183>
mov    0x8(%r14),%rdi
call   10c0 <atoi@plt>
lea    0xd4c(%rip),%rsi
mov    %eax,%edi
call   10b0 <NAP_sendcommand@plt>
jmp    1343 <KB_getcommand+0x183>
add    $0x10,%r14
mov    %r14,%rdi
call   1060 <argstring@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
mov    %rbx,%rsi
call   10b0 <NAP_sendcommand@plt>
jmp    133b <KB_getcommand+0x17b>
add    $0x10,%r14
mov    %r14,%rdi
call   1060 <argstring@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%rax
mov    0x8(%rax),%rdi
mov    %rbx,%rsi
call   1090 <NAP_requestfile@plt>
jmp    133b <KB_getcommand+0x17b>
add    $0x8,%r14
mov    %r14,%rdi
call   1060 <argstring@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <NAP_search@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   10e0 <freeargs@plt>
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [4 x i8] c"raw\00"
@stdout = global i32 0
@.str.1 = [25 x i8] c"raw <command id> <data>\0A\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [4 x i8] c"get\00"
@.str.4 = [19 x i8] c"get <name> <file>\0A\00"
@.str.5 = [7 x i8] c"search\00"
@.str.6 = [14 x i8] c"search <file>\00"
define void @KB_getcommand() {
%1 = alloca i8**
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8** null, i8*** %1
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %7, i32 1024, i32 %8)
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%11 = call i32 @strlen(i8* %10)
%12 = sub nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %13
store i8 0, i8* %14
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%16 = call i32 @getargs(i8* %15, i8*** %1)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %102
19: ; preds = %0
%20 = load i8**, i8*** %1
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 @strcasecmp(i8* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%24 = icmp ne i32 %23, 0
br i1 %24, label %54, label %25
25: ; preds = %19
%26 = load i32, i32* %2
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32, i32* @stdout
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %53
31: ; preds = %25
%32 = load i32, i32* %2
%33 = icmp eq i32 %32, 2
br i1 %33, label %34, label %40
34: ; preds = %31
%35 = load i8**, i8*** %1
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i32 @atoi(i8* %37)
%39 = call i32 @NAP_sendcommand(i32 %38, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %52
40: ; preds = %31
%41 = load i8**, i8*** %1
%42 = getelementptr inbounds i8*, i8** %41, i64 2
%43 = call i8* @argstring(i8** %42)
store i8* %43, i8** %4
%44 = load i8**, i8*** %1
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i32 @atoi(i8* %46)
%48 = load i8*, i8** %4
%49 = call i32 @NAP_sendcommand(i32 %47, i8* %48)
%50 = load i8*, i8** %4
%51 = call i32 @free(i8* %50)
br label %52
52: ; preds = %40, %34
br label %53
53: ; preds = %52, %28
br label %101
54: ; preds = %19
%55 = load i8**, i8*** %1
%56 = getelementptr inbounds i8*, i8** %55, i64 0
%57 = load i8*, i8** %56
%58 = call i32 @strcasecmp(i8* %57, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%59 = icmp ne i32 %58, 0
br i1 %59, label %78, label %60
60: ; preds = %54
%61 = load i32, i32* %2
%62 = icmp slt i32 %61, 3
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i32, i32* @stdout
%65 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %77
66: ; preds = %60
%67 = load i8**, i8*** %1
%68 = getelementptr inbounds i8*, i8** %67, i64 2
%69 = call i8* @argstring(i8** %68)
store i8* %69, i8** %5
%70 = load i8**, i8*** %1
%71 = getelementptr inbounds i8*, i8** %70, i64 1
%72 = load i8*, i8** %71
%73 = load i8*, i8** %5
%74 = call i32 @NAP_requestfile(i8* %72, i8* %73)
%75 = load i8*, i8** %5
%76 = call i32 @free(i8* %75)
br label %77
77: ; preds = %66, %63
br label %100
78: ; preds = %54
%79 = load i8**, i8*** %1
%80 = getelementptr inbounds i8*, i8** %79, i64 0
%81 = load i8*, i8** %80
%82 = call i32 @strcasecmp(i8* %81, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%83 = icmp ne i32 %82, 0
br i1 %83, label %99, label %84
84: ; preds = %78
%85 = load i32, i32* %2
%86 = icmp eq i32 %85, 1
br i1 %86, label %87, label %90
87: ; preds = %84
%88 = load i32, i32* @stdout
%89 = call i32 @fprintf(i32 %88, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %98
90: ; preds = %84
%91 = load i8**, i8*** %1
%92 = getelementptr inbounds i8*, i8** %91, i64 1
%93 = call i8* @argstring(i8** %92)
store i8* %93, i8** %6
%94 = load i8*, i8** %6
%95 = call i32 @NAP_search(i8* %94)
%96 = load i8*, i8** %6
%97 = call i32 @free(i8* %96)
br label %98
98: ; preds = %90, %87
br label %99
99: ; preds = %98, %78
br label %100
100: ; preds = %99, %77
br label %101
101: ; preds = %100, %53
br label %102
102: ; preds = %101, %0
%103 = load i8**, i8*** %1
%104 = call i32 @freeargs(i8** %103)
ret void
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [4 x i8] c"raw\00"
@stdout = global i32 0
@.str.1 = [25 x i8] c"raw <command id> <data>\0A\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [4 x i8] c"get\00"
@.str.4 = [19 x i8] c"get <name> <file>\0A\00"
@.str.5 = [7 x i8] c"search\00"
@.str.6 = [14 x i8] c"search <file>\00"
define void @KB_getcommand() {
%1 = alloca i8**
%2 = alloca [1024 x i8]
%3 = bitcast i8*** %1 to i8*
store i8** null, i8*** %1
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @stdin
%6 = call i32 @fgets(i8* %4, i32 1024, i32 %5)
%7 = call i32 @strlen(i8* %4)
%8 = add nsw i32 %7, -1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %9
store i8 0, i8* %10
%11 = call i32 @getargs(i8* %4, i8*** %1)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %65
13: ; preds = %0
%14 = load i8**, i8*** %1
%15 = load i8*, i8** %14
%16 = call i32 @strcasecmp(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %36
18: ; preds = %13
switch i32 %11, label %27 [
i32 1, label %19
i32 2, label %22
]
19: ; preds = %18
%20 = load i32, i32* @stdout
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %65
22: ; preds = %18
%23 = getelementptr inbounds i8*, i8** %14, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
%26 = call i32 @NAP_sendcommand(i32 %25, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %65
27: ; preds = %18
%28 = getelementptr inbounds i8*, i8** %14, i64 2
%29 = call i8* @argstring(i8** %28)
%30 = load i8**, i8*** %1
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
%34 = call i32 @NAP_sendcommand(i32 %33, i8* %29)
%35 = call i32 @free(i8* %29)
br label %65
36: ; preds = %13
%37 = call i32 @strcasecmp(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %52
39: ; preds = %36
%40 = icmp ult i32 %11, 3
br i1 %40, label %41, label %44
41: ; preds = %39
%42 = load i32, i32* @stdout
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %65
44: ; preds = %39
%45 = getelementptr inbounds i8*, i8** %14, i64 2
%46 = call i8* @argstring(i8** %45)
%47 = load i8**, i8*** %1
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = call i32 @NAP_requestfile(i8* %49, i8* %46)
%51 = call i32 @free(i8* %46)
br label %65
52: ; preds = %36
%53 = call i32 @strcasecmp(i8* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %65
55: ; preds = %52
%56 = icmp eq i32 %11, 1
br i1 %56, label %57, label %60
57: ; preds = %55
%58 = load i32, i32* @stdout
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %65
60: ; preds = %55
%61 = getelementptr inbounds i8*, i8** %14, i64 1
%62 = call i8* @argstring(i8** %61)
%63 = call i32 @NAP_search(i8* %62)
%64 = call i32 @free(i8* %62)
br label %65
65: ; preds = %22, %27, %19, %52, %60, %57, %41, %44, %0
%66 = load i8**, i8*** %1
%67 = call i32 @freeargs(i8** %66)
ret void
}