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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int make_server_socket(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ process_rq(char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "usage: ./webserv portnum\n");
    exit(EXIT_FAILURE);
  }
  int sock_id = make_server_socket(atoi(argv[1]));
  int sock_fd;
  char request[BUFSIZ];
  FILE *sock_fp;
  while (true) {
    sock_fd = accept(sock_id, NULL, NULL);
    sock_fp = fdopen(sock_fd, "r");
    fgets(request, BUFSIZ, sock_fp);
    printf("got a call: request = %s\n", request);
    char buf[BUFSIZ];
    while (fgets(buf, BUFSIZ, sock_fp) != NULL && strcmp(buf, "\r\n") != 0)
      ;
    process_rq(request, sock_fd);
    fclose(sock_fp);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11f3 <main+0x43>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1070 <make_server_socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
jmp    123b <main+0x8b>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
lea    0xdc3(%rip),%rsi
call   10a0 <fdopen@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,-0x30(%rbp)
mov    0x2d6d(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    (%rax),%esi
mov    -0x30(%rbp),%rdx
call   1060 <fgets@plt>
mov    -0x48(%rbp),%rsi
lea    0xd97(%rip),%rdi
call   1050 <printf@plt>
mov    -0x58(%rbp),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x50(%rbp),%rdi
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rdx
call   1060 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x59(%rbp)
je     12f5 <main+0x145>
mov    -0x50(%rbp),%rdi
lea    0xd50(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%al
test   $0x1,%al
jne    1305 <main+0x155>
jmp    130a <main+0x15a>
jmp    12b3 <main+0x103>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1040 <process_rq@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
jmp    1236 <main+0x86>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    12bd <main+0x10d>
mov    0x8(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1070 <make_server_socket@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2df4(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    0xe36(%rip),%rbx
mov    %r14,-0x38(%rbp)
jmp    122b <main+0x7b>
cs nopw 0x0(%rax,%rax,1)
mov    -0x38(%rbp),%r14
mov    %r14,%rdi
mov    %r15d,%esi
call   1040 <process_rq@plt>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    -0x40(%rbp),%rsp
mov    -0x2c(%rbp),%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
mov    %eax,%r15d
mov    %eax,%edi
lea    0xdd7(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,%r12
mov    0x0(%r13),%esi
mov    %r14,%rdi
mov    %rax,%rdx
call   1060 <fgets@plt>
lea    0xdbb(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %rsp,-0x40(%rbp)
mov    0x0(%r13),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%r13),%esi
mov    %r14,%rdi
mov    %r12,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1210 <main+0x60>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    1290 <main+0xe0>
jmp    1210 <main+0x60>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
lea    0xd33(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"usage: ./webserv portnum\0A\00"
@EXIT_FAILURE = global i32 0
@BUFSIZ = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [26 x i8] c"got a call: request = %s\0A\00"
@.str.3 = [3 x i8] c"\0D\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
%25 = call i32 @make_server_socket(i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* @BUFSIZ
%27 = zext i32 %26 to i64
%28 = call i8* @llvm.stacksave()
store i8* %28, i8** %8
%29 = alloca i8, i64 %27
store i64 %27, i64* %9
br label %30
30: ; preds = %55, %20
br label %31
31: ; preds = %30
%32 = load i32, i32* %6
%33 = call i32 @accept(i32 %32, i32* null, i32* null)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = call i32* @fdopen(i32 %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %35, i32** %10
%36 = load i32, i32* @BUFSIZ
%37 = load i32*, i32** %10
%38 = call i32* @fgets(i8* %29, i32 %36, i32* %37)
%39 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%40 = load i32, i32* @BUFSIZ
%41 = zext i32 %40 to i64
%42 = call i8* @llvm.stacksave()
store i8* %42, i8** %11
%43 = alloca i8, i64 %41
store i64 %41, i64* %12
br label %44
44: ; preds = %54, %31
%45 = load i32, i32* @BUFSIZ
%46 = load i32*, i32** %10
%47 = call i32* @fgets(i8* %43, i32 %45, i32* %46)
%48 = icmp ne i32* %47, null
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = call i64 @strcmp(i8* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%51 = icmp ne i64 %50, 0
br label %52
52: ; preds = %49, %44
%53 = phi i1 [ false, %44 ], [ %51, %49 ]
br i1 %53, label %54, label %55
54: ; preds = %52
br label %44
55: ; preds = %52
%56 = load i32, i32* %7
%57 = call i32 @process_rq(i8* %29, i32 %56)
%58 = load i32*, i32** %10
%59 = call i32 @fclose(i32* %58)
%60 = load i8*, i8** %11
br label %30
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"usage: ./webserv portnum\0A\00"
@EXIT_FAILURE = global i32 0
@BUFSIZ = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [26 x i8] c"got a call: request = %s\0A\00"
@.str.3 = [3 x i8] c"\0D\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = tail call i32 @make_server_socket(i32 %12)
%14 = load i32, i32* @BUFSIZ
%15 = zext i32 %14 to i64
%16 = alloca i8, i64 %15
br label %17
17: ; preds = %34, %9
%18 = call i32 @accept(i32 %13, i32* null, i32* null)
%19 = call i32* @fdopen(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @BUFSIZ
%21 = call i32* @fgets(i8* %16, i32 %20, i32* %19)
%22 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%23 = load i32, i32* @BUFSIZ
%24 = zext i32 %23 to i64
%25 = call i8* @llvm.stacksave()
%26 = alloca i8, i64 %24
br label %27
27: ; preds = %31, %17
%28 = load i32, i32* @BUFSIZ
%29 = call i32* @fgets(i8* %26, i32 %28, i32* %19)
%30 = icmp eq i32* %29, null
br i1 %30, label %34, label %31
31: ; preds = %27
%32 = call i64 @strcmp(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %27
34: ; preds = %27, %31
%35 = call i32 @process_rq(i8* %16, i32 %18)
%36 = call i32 @fclose(i32* %19)
br label %17
}