#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 accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int make_server_socket(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ process_rq(char *, int);
int /*<<< orphan*/ read_til_crnl(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int sock, fd;
  FILE *fpin;
  char request[1024];
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <portnum>\n", argv[0]);
    exit(1);
  }
  sock = make_server_socket(atoi(argv[1]));
  if (sock == -1)
    exit(2);
  while (1) {
    fd = accept(sock, NULL, NULL);
    fpin = fdopen(fd, "r");
    fgets(request, 1024, fpin);
    printf("got a call: request = %s", request);
    read_til_crnl(fpin);
    process_rq(request, fd);
    fclose(fpin);
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11f9 <main+0x49>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe16(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1080 <make_server_socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1224 <main+0x74>
mov    $0x2,%edi
call   10d0 <exit@plt>
jmp    1229 <main+0x79>
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    0xdd0(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x20(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
lea    -0x420(%rbp),%rsi
lea    0xda6(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <read_til_crnl@plt>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1050 <process_rq@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1229 <main+0x79>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
cmp    $0x2,%edi
jne    1252 <main+0xa2>
mov    0x8(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1080 <make_server_socket@plt>
cmp    $0xffffffff,%eax
je     1274 <main+0xc4>
mov    %eax,%r12d
lea    0xe28(%rip),%r14
mov    %rsp,%r13
lea    0xe20(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
call   10a0 <fdopen@plt>
mov    %rax,%rbx
mov    %r13,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <read_til_crnl@plt>
mov    %r13,%rdi
mov    %ebp,%esi
call   1050 <process_rq@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1200 <main+0x50>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd9b(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"Usage: %s <portnum>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"got a call: request = %s\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 [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
%24 = call i32 @make_server_socket(i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = call i32 @exit(i32 2)
unreachable
29: ; preds = %19
br label %30
30: ; preds = %29, %30
%31 = load i32, i32* %6
%32 = call i32 @accept(i32 %31, i32* null, i32* null)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = call i32* @fdopen(i32 %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %34, i32** %8
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%36 = load i32*, i32** %8
%37 = call i32 @fgets(i8* %35, i32 1024, i32* %36)
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%39 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = load i32*, i32** %8
%41 = call i32 @read_til_crnl(i32* %40)
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%43 = load i32, i32* %7
%44 = call i32 @process_rq(i8* %42, i32 %43)
%45 = load i32*, i32** %8
%46 = call i32 @fclose(i32* %45)
br label %30
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"Usage: %s <portnum>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"got a call: request = %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = tail call i32 @make_server_socket(i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = tail call i32 @exit(i32 2)
unreachable
19: ; preds = %11, %19
%20 = call i32 @accept(i32 %15, i32* null, i32* null)
%21 = call i32* @fdopen(i32 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @fgets(i8* %4, i32 1024, i32* %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%24 = call i32 @read_til_crnl(i32* %21)
%25 = call i32 @process_rq(i8* %4, i32 %20)
%26 = call i32 @fclose(i32* %21)
br label %19
}