#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;
int /*<<< orphan*/ cannot_do(int);
int /*<<< orphan*/ do_404(char *, int);
int /*<<< orphan*/ do_cat(char *, int);
int /*<<< orphan*/ do_exec(char *, int);
int /*<<< orphan*/ do_ls(char *, int);
scalar_t__ ends_in_cgi(char *);
scalar_t__ fork();
scalar_t__ isadir(char *);
scalar_t__ not_exist(char *);
int /*<<< orphan*/ printf(char *, ...);
int sscanf(char *, char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
void process_rq(char *rq, int fd) {
  char cmd[BUFSIZ], arg[BUFSIZ];
  if (fork() != 0)
    return;
  printf("rq: %s\n", rq);
  strcpy(arg, "./");
  printf("arg: %s\n", arg);
  if (sscanf(rq, "%s %s", cmd, arg + 2) != 2)
    return;
  printf("arg: %s\n", arg);
  if (strcmp(cmd, "GET") != 0) {
    printf("cannot_do\n");
    cannot_do(fd);
  } else if (not_exist(arg)) {
    printf("do_404\n");
    do_404(arg, fd);
  } else if (isadir(arg)) {
    printf("do_ls\n");
    do_ls(arg, fd);
  } else if (ends_in_cgi(arg)) {
    printf("do_exec\n");
    do_exec(arg, fd);
  } else {
    printf("do_cat\n");
    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    0x2dfa(%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,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   10f0 <fork@plt>
cmp    $0x0,%rax
je     1246 <process_rq+0x76>
movl   $0x1,-0x2c(%rbp)
jmp    13b8 <process_rq+0x1e8>
mov    -0x8(%rbp),%rsi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x38(%rbp),%rdi
lea    0xda5(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x38(%rbp),%rsi
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%rdx
mov    -0x8(%rbp),%rdi
add    $0x2,%rcx
lea    0xd83(%rip),%rsi
call   10e0 <sscanf@plt>
cmp    $0x2,%eax
je     12ab <process_rq+0xdb>
movl   $0x1,-0x2c(%rbp)
jmp    13b8 <process_rq+0x1e8>
mov    -0x38(%rbp),%rsi
lea    0xd55(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x40(%rbp),%rdi
lea    0xd52(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
je     12f2 <process_rq+0x122>
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <cannot_do@plt>
jmp    13b1 <process_rq+0x1e1>
mov    -0x38(%rbp),%rdi
call   10b0 <not_exist@plt>
cmp    $0x0,%rax
je     1324 <process_rq+0x154>
lea    0xd1d(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <do_404@plt>
jmp    13ac <process_rq+0x1dc>
mov    -0x38(%rbp),%rdi
call   1080 <isadir@plt>
cmp    $0x0,%rax
je     1356 <process_rq+0x186>
lea    0xcf3(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   10d0 <do_ls@plt>
jmp    13a7 <process_rq+0x1d7>
mov    -0x38(%rbp),%rdi
call   10a0 <ends_in_cgi@plt>
cmp    $0x0,%rax
je     1388 <process_rq+0x1b8>
lea    0xcc8(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <do_exec@plt>
jmp    13a2 <process_rq+0x1d2>
lea    0xcb2(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1060 <do_cat@plt>
jmp    13a7 <process_rq+0x1d7>
jmp    13ac <process_rq+0x1dc>
jmp    13b1 <process_rq+0x1e1>
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
mov    0x2de8(%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
xor    %eax,%eax
call   1100 <fork@plt>
test   %rax,%rax
jne    133b <process_rq+0x15b>
lea    0xdd5(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   10a0 <printf@plt>
lea    0xdcc(%rip),%rsi
mov    %r15,%rdi
call   1030 <strcpy@plt>
lea    0xdc0(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   10a0 <printf@plt>
lea    0x2(%r15),%rcx
lea    0xdb4(%rip),%rsi
mov    %r12,%rdi
mov    %rbx,%rdx
call   10f0 <sscanf@plt>
cmp    $0x2,%eax
jne    133b <process_rq+0x15b>
lea    0xd90(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   10a0 <printf@plt>
lea    0xd8e(%rip),%rsi
mov    %rbx,%rdi
call   10d0 <strcmp@plt>
test   %rax,%rax
je     12b2 <process_rq+0xd2>
lea    0xd9a(%rip),%rdi
call   1050 <puts@plt>
mov    %r14d,%edi
call   1080 <cannot_do@plt>
jmp    133b <process_rq+0x15b>
mov    %r15,%rdi
call   10c0 <not_exist@plt>
test   %rax,%rax
je     12d8 <process_rq+0xf8>
lea    0xd6d(%rip),%rdi
call   1050 <puts@plt>
mov    %r15,%rdi
mov    %r14d,%esi
call   1040 <do_404@plt>
jmp    133b <process_rq+0x15b>
mov    %r15,%rdi
call   1090 <isadir@plt>
test   %rax,%rax
je     12fe <process_rq+0x11e>
lea    0xd41(%rip),%rdi
call   1050 <puts@plt>
mov    %r15,%rdi
mov    %r14d,%esi
call   10e0 <do_ls@plt>
jmp    133b <process_rq+0x15b>
mov    %r15,%rdi
call   10b0 <ends_in_cgi@plt>
test   %rax,%rax
je     1324 <process_rq+0x144>
lea    0xd13(%rip),%rdi
call   1050 <puts@plt>
mov    %r15,%rdi
mov    %r14d,%esi
call   1060 <do_exec@plt>
jmp    133b <process_rq+0x15b>
lea    0xcf3(%rip),%rdi
call   1050 <puts@plt>
mov    %r15,%rdi
mov    %r14d,%esi
call   1070 <do_cat@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [8 x i8] c"rq: %s\0A\00"
@.str.1 = [3 x i8] c"./\00"
@.str.2 = [9 x i8] c"arg: %s\0A\00"
@.str.3 = [6 x i8] c"%s %s\00"
@.str.4 = [4 x i8] c"GET\00"
@.str.5 = [11 x i8] c"cannot_do\0A\00"
@.str.6 = [8 x i8] c"do_404\0A\00"
@.str.7 = [7 x i8] c"do_ls\0A\00"
@.str.8 = [9 x i8] c"do_exec\0A\00"
@.str.9 = [8 x i8] c"do_cat\0A\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 i32, i32* @BUFSIZ
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
%13 = load i32, i32* @BUFSIZ
%14 = zext i32 %13 to i64
%15 = alloca i8, i64 %14
store i64 %14, i64* %7
%16 = call i64 (...) @fork()
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 1, i32* %8
br label %66
19: ; preds = %2
%20 = load i8*, i8** %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @strcpy(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %15, i64 2
%26 = call i32 @sscanf(i8* %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %12, i8* %25)
%27 = icmp ne i32 %26, 2
br i1 %27, label %28, label %29
28: ; preds = %19
store i32 1, i32* %8
br label %66
29: ; preds = %19
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%31 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%35 = load i32, i32* %4
%36 = call i32 @cannot_do(i32 %35)
br label %65
37: ; preds = %29
%38 = call i64 @not_exist(i8* %15)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%42 = load i32, i32* %4
%43 = call i32 @do_404(i8* %15, i32 %42)
br label %64
44: ; preds = %37
%45 = call i64 @isadir(i8* %15)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%49 = load i32, i32* %4
%50 = call i32 @do_ls(i8* %15, i32 %49)
br label %63
51: ; preds = %44
%52 = call i64 @ends_in_cgi(i8* %15)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %51
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
%56 = load i32, i32* %4
%57 = call i32 @do_exec(i8* %15, i32 %56)
br label %62
58: ; preds = %51
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
%60 = load i32, i32* %4
%61 = call i32 @do_cat(i8* %15, i32 %60)
br label %62
62: ; preds = %58, %54
br label %63
63: ; preds = %62, %47
br label %64
64: ; preds = %63, %40
br label %65
65: ; preds = %64, %33
store i32 0, i32* %8
br label %66
66: ; preds = %65, %28, %18
%67 = load i8*, i8** %5
%68 = load i32, i32* %8
switch i32 %68, label %70 [
i32 0, label %69
i32 1, label %69
]
69: ; preds = %66, %66
ret void
70: ; preds = %66
unreachable
}

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