#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 BUFSIZ;
scalar_t__ bulit_in(char *, int);
int /*<<< orphan*/ do_404(char *, int);
int /*<<< orphan*/ do_cat(char *, int);
int /*<<< orphan*/ do_ls(char *, int);
scalar_t__ isadir(char *);
scalar_t__ not_exist(char *);
int /*<<< orphan*/ not_implemented(int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sanitize(char *);
int sscanf(char *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
void process_rq(char *rq, int fd) {
  printf("rq: %s\n", rq);
  char cmd[BUFSIZ], arg[BUFSIZ];
  if (sscanf(rq, "%s %s", cmd, arg) != 2)
    return;
  printf("cmd: %s\n", cmd);
  printf("arg: %s\n", arg);
  sanitize(arg);
  printf("sanitize arg: %s\n", arg);
  printf("sanitized version is %s\n", arg);
  if (strcmp(cmd, "GET") != 0) {
    not_implemented(fd);
  } else if (bulit_in(arg, fd)) {
  } else if (not_exist(arg)) {
    do_404(arg, fd);
  } else if (isadir(arg)) {
    do_ls(arg, fd);
  } else {
    do_cat(arg, fd);
  }
}


x86 assembly (clang, -O0):
<process_rq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe36(%rip),%rdi
call   1070 <printf@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x18(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x40(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x20(%rbp)
mov    (%rax),%eax
mov    %rax,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rcx
sub    %rsi,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   10b0 <sscanf@plt>
cmp    $0x2,%eax
je     123e <process_rq+0x8e>
movl   $0x1,-0x2c(%rbp)
jmp    1333 <process_rq+0x183>
mov    -0x40(%rbp),%rsi
lea    0xdc5(%rip),%rdi
call   1070 <printf@plt>
mov    -0x38(%rbp),%rsi
lea    0xdc7(%rip),%rdi
call   1070 <printf@plt>
mov    -0x38(%rbp),%rdi
call   10d0 <sanitize@plt>
mov    -0x38(%rbp),%rsi
lea    0xda5(%rip),%rdi
call   1070 <printf@plt>
mov    -0x38(%rbp),%rsi
lea    0xda7(%rip),%rdi
call   1070 <printf@plt>
mov    -0x40(%rbp),%rdi
lea    0xdb0(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     12ae <process_rq+0xfe>
mov    -0xc(%rbp),%edi
call   1030 <not_implemented@plt>
jmp    132c <process_rq+0x17c>
mov    -0x38(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   10c0 <bulit_in@plt>
cmp    $0x0,%rax
je     12c9 <process_rq+0x119>
jmp    1327 <process_rq+0x177>
mov    -0x38(%rbp),%rdi
call   1080 <not_exist@plt>
cmp    $0x0,%rax
je     12ed <process_rq+0x13d>
mov    -0x38(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <do_404@plt>
jmp    1322 <process_rq+0x172>
mov    -0x38(%rbp),%rdi
call   1060 <isadir@plt>
cmp    $0x0,%rax
je     1311 <process_rq+0x161>
mov    -0x38(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   10a0 <do_ls@plt>
jmp    131d <process_rq+0x16d>
mov    -0x38(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <do_cat@plt>
jmp    1322 <process_rq+0x172>
jmp    1327 <process_rq+0x177>
jmp    132c <process_rq+0x17c>
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_rq>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%r12
lea    0xe38(%rip),%rdi
mov    %r12,%rsi
call   1070 <printf@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
lea    0xe0e(%rip),%rsi
mov    %r12,%rdi
mov    %rbx,%rdx
mov    %r15,%rcx
call   10b0 <sscanf@plt>
cmp    $0x2,%eax
jne    12bc <process_rq+0x10c>
lea    0xdf6(%rip),%rdi
mov    %rbx,%rsi
call   1070 <printf@plt>
lea    0xdf9(%rip),%rdi
mov    %r15,%rsi
call   1070 <printf@plt>
mov    %r15,%rdi
call   10d0 <sanitize@plt>
lea    0xdd9(%rip),%rdi
mov    %r15,%rsi
call   1070 <printf@plt>
lea    0xddc(%rip),%rdi
mov    %r15,%rsi
call   1070 <printf@plt>
lea    0xde6(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1273 <process_rq+0xc3>
mov    %r14d,%edi
call   1030 <not_implemented@plt>
jmp    12bc <process_rq+0x10c>
mov    %r15,%rdi
mov    %r14d,%esi
call   10c0 <bulit_in@plt>
test   %rax,%rax
jne    12bc <process_rq+0x10c>
mov    %r15,%rdi
call   1080 <not_exist@plt>
test   %rax,%rax
je     129d <process_rq+0xed>
mov    %r15,%rdi
mov    %r14d,%esi
call   1040 <do_404@plt>
jmp    12bc <process_rq+0x10c>
mov    %r15,%rdi
call   1060 <isadir@plt>
mov    %r15,%rdi
mov    %r14d,%esi
test   %rax,%rax
je     12b7 <process_rq+0x107>
call   10a0 <do_ls@plt>
jmp    12bc <process_rq+0x10c>
call   1050 <do_cat@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"rq: %s\0A\00"
@BUFSIZ = global i32 0
@.str.1 = [6 x i8] c"%s %s\00"
@.str.2 = [9 x i8] c"cmd: %s\0A\00"
@.str.3 = [9 x i8] c"arg: %s\0A\00"
@.str.4 = [18 x i8] c"sanitize arg: %s\0A\00"
@.str.5 = [25 x i8] c"sanitized version is %s\0A\00"
@.str.6 = [4 x i8] c"GET\00"
define void @process_rq(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i8*, i8** %3
%10 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @BUFSIZ
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
%15 = load i32, i32* @BUFSIZ
%16 = zext i32 %15 to i64
%17 = alloca i8, i64 %16
store i64 %16, i64* %7
%18 = load i8*, i8** %3
%19 = call i32 @sscanf(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %17)
%20 = icmp ne i32 %19, 2
br i1 %20, label %21, label %22
21: ; preds = %2
store i32 1, i32* %8
br label %57
22: ; preds = %2
%23 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%24 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%25 = call i32 @sanitize(i8* %17)
%26 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %17)
%27 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %17)
%28 = call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %22
%31 = load i32, i32* %4
%32 = call i32 @not_implemented(i32 %31)
br label %56
33: ; preds = %22
%34 = load i32, i32* %4
%35 = call i64 @bulit_in(i8* %17, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33
br label %55
38: ; preds = %33
%39 = call i64 @not_exist(i8* %17)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32, i32* %4
%43 = call i32 @do_404(i8* %17, i32 %42)
br label %54
44: ; preds = %38
%45 = call i64 @isadir(i8* %17)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* %4
%49 = call i32 @do_ls(i8* %17, i32 %48)
br label %53
50: ; preds = %44
%51 = load i32, i32* %4
%52 = call i32 @do_cat(i8* %17, i32 %51)
br label %53
53: ; preds = %50, %47
br label %54
54: ; preds = %53, %41
br label %55
55: ; preds = %54, %37
br label %56
56: ; preds = %55, %30
store i32 0, i32* %8
br label %57
57: ; preds = %56, %21
%58 = load i8*, i8** %5
%59 = load i32, i32* %8
switch i32 %59, label %61 [
i32 0, label %60
i32 1, label %60
]
60: ; preds = %57, %57
ret void
61: ; preds = %57
unreachable
}

LLVM IR (-O2):
@.str = [8 x i8] c"rq: %s\0A\00"
@BUFSIZ = global i32 0
@.str.1 = [6 x i8] c"%s %s\00"
@.str.2 = [9 x i8] c"cmd: %s\0A\00"
@.str.3 = [9 x i8] c"arg: %s\0A\00"
@.str.4 = [18 x i8] c"sanitize arg: %s\0A\00"
@.str.5 = [25 x i8] c"sanitized version is %s\0A\00"
@.str.6 = [4 x i8] c"GET\00"
define void @process_rq(i8* %0, i32 %1) {
%3 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = alloca i8, i64 %5
%8 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %6, i8* %7)
%9 = icmp eq i32 %8, 2
br i1 %9, label %10, label %35
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%12 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%13 = call i32 @sanitize(i8* %7)
%14 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %7)
%15 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %7)
%16 = call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %10
%19 = call i32 @not_implemented(i32 %1)
br label %35
20: ; preds = %10
%21 = call i64 @bulit_in(i8* %7, i32 %1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %35
23: ; preds = %20
%24 = call i64 @not_exist(i8* %7)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 @do_404(i8* %7, i32 %1)
br label %35
28: ; preds = %23
%29 = call i64 @isadir(i8* %7)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @do_ls(i8* %7, i32 %1)
br label %35
33: ; preds = %28
%34 = call i32 @do_cat(i8* %7, i32 %1)
br label %35
35: ; preds = %18, %26, %33, %31, %20, %2
ret void
}