#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ cliaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  int listenfd, connfd, n;
  struct sockaddr_in servaddr, cliaddr;
  socklen_t clilen;
  char mesg[4096];
  char output[] = "HTTP/1.0 200 OK\r\nDate: Wed, 12 Mar 2014 00:14:10 "
                  "GMT\r\n\r\n<html><h1>Hello!</h1></html>\r\n";
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(32000);
  bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  listen(listenfd, 1024);
  for (int i = 0; i < 100; i++) {
    clilen = sizeof(cliaddr);
    connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
    printf("connected (%d)\n", connfd);
    if (connfd > 0) {
      n = recv(connfd, mesg, 4096, 0);
      mesg[n] = 0;
      printf("=====\n%s=====\n", mesg);
      send(connfd, output, strlen(output), 0);
      close(connfd);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10c0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x10a0(%rbp),%rdi
lea    0xe09(%rip),%rsi
mov    $0x57,%edx
call   10b0 <memcpy@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    $0x7d00,%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
mov    -0x14(%rbp),%edi
mov    $0x400,%esi
call   10c0 <listen@plt>
movl   $0x0,-0x10a4(%rbp)
cmpl   $0x64,-0x10a4(%rbp)
jge    1355 <main+0x185>
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jle    133c <main+0x16c>
mov    -0x18(%rbp),%edi
lea    -0x1040(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x1040(%rbp,%rax,1)
lea    -0x1040(%rbp),%rsi
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x10b4(%rbp)
lea    -0x10a0(%rbp),%rax
mov    %rax,-0x10b0(%rbp)
lea    -0x10a0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x10b4(%rbp),%edi
mov    -0x10b0(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    -0x18(%rbp),%edi
call   10a0 <close@plt>
jmp    1341 <main+0x171>
mov    -0x10a4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10a4(%rbp)
jmp    127f <main+0xaf>
mov    -0x4(%rbp),%eax
add    $0x10c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1098,%rsp
movaps 0xe68(%rip),%xmm0
movaps %xmm0,0x70(%rsp)
movaps 0xe4c(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0xe30(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xe14(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xdf8(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movabs $0xa0d3e6c6d7468,%rax
mov    %rax,0x7f(%rsp)
mov    0x2dc5(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x7d00,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
mov    %ebx,%edi
mov    $0x400,%esi
call   10b0 <listen@plt>
mov    $0x64,%r15d
lea    0xdbc(%rip),%r12
lea    0x90(%rsp),%r13
lea    0x30(%rsp),%rbp
jmp    12b6 <main+0xf6>
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%r15d
je     1334 <main+0x174>
movl   $0xc,0xc(%rsp)
mov    %ebx,%edi
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   10d0 <accept@plt>
mov    %eax,%r14d
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
test   %r14d,%r14d
jle    12b0 <main+0xf0>
mov    %r14d,%edi
mov    %r13,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cltq
movb   $0x0,0x90(%rsp,%rax,1)
lea    0xd61(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %r14d,%edi
call   10a0 <close@plt>
jmp    12b0 <main+0xf0>
xor    %eax,%eax
add    $0x1098,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.output = [87 x i8] c"HTTP/1.0 200 OK\0D\0ADate: Wed, 12 Mar 2014 00:14:10 GMT\0D\0A\0D\0A<html><h1>Hello!</h1></html>\0D\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [16 x i8] c"connected (%d)\0A\00"
@.str.1 = [15 x i8] c"=====\0A%s=====\0A\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 %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca [4096 x i8]
%13 = alloca [87 x i8]
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = bitcast [87 x i8]* %13 to i8*
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %6
%19 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = call i32 @htons(i32 32000)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %6
%29 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%30 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = load i32, i32* %6
%32 = call i32 @listen(i32 %31, i32 1024)
store i32 0, i32* %14
br label %33
33: ; preds = %61, %2
%34 = load i32, i32* %14
%35 = icmp slt i32 %34, 100
br i1 %35, label %36, label %64
36: ; preds = %33
store i32 12, i32* %11
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%39 = call i32 @accept(i32 %37, %struct.sockaddr* %38, i32* %11)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %7
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %60
44: ; preds = %36
%45 = load i32, i32* %7
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%47 = call i32 @recv(i32 %45, i8* %46, i32 4096, i32 0)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 %49
store i8 0, i8* %50
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %51)
%53 = load i32, i32* %7
%54 = getelementptr inbounds [87 x i8], [87 x i8]* %13, i64 0, i64 0
%55 = getelementptr inbounds [87 x i8], [87 x i8]* %13, i64 0, i64 0
%56 = call i32 @strlen(i8* %55)
%57 = call i32 @send(i32 %53, i8* %54, i32 %56, i32 0)
%58 = load i32, i32* %7
%59 = call i32 @close(i32 %58)
br label %60
60: ; preds = %44, %36
br label %61
61: ; preds = %60
%62 = load i32, i32* %14
%63 = add nsw i32 %62, 1
store i32 %63, i32* %14
br label %33
64: ; preds = %33
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.output = [87 x i8] c"HTTP/1.0 200 OK\0D\0ADate: Wed, 12 Mar 2014 00:14:10 GMT\0D\0A\0D\0A<html><h1>Hello!</h1></html>\0D\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [16 x i8] c"connected (%d)\0A\00"
@.str.1 = [15 x i8] c"=====\0A%s=====\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [4096 x i8]
%7 = alloca [87 x i8]
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%12 = getelementptr inbounds [87 x i8], [87 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %20, i32* %21
%22 = call i32 @htons(i32 32000)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @bind(i32 %15, %struct.sockaddr* %24, i32 12)
%26 = call i32 @listen(i32 %15, i32 1024)
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %29
28: ; preds = %42
ret i32 0
29: ; preds = %2, %42
%30 = phi i32 [ 0, %2 ], [ %43, %42 ]
store i32 12, i32* %5
%31 = call i32 @accept(i32 %15, %struct.sockaddr* %27, i32* %5)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = icmp sgt i32 %31, 0
br i1 %33, label %34, label %42
34: ; preds = %29
%35 = call i32 @recv(i32 %31, i8* %11, i32 4096, i32 0)
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 %36
store i8 0, i8* %37
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%39 = call i32 @strlen(i8* %12)
%40 = call i32 @send(i32 %31, i8* %12, i32 %39, i32 0)
%41 = call i32 @close(i32 %31)
br label %42
42: ; preds = %29, %34
%43 = add nuw nsw i32 %30, 1
%44 = icmp eq i32 %43, 100
br i1 %44, label %28, label %29
}