#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 /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ process(int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int sock;
  struct sockaddr_in sin;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = INADDR_ANY;
  sin.sin_port = htons(80);
  bind(sock, (struct sockaddr *)&sin, sizeof(sin));
  listen(sock, 5);
  printf("HTTP server listening on port %d\n", 80);
  while (1) {
    int s;
    FILE *f;
    s = accept(sock, NULL, NULL);
    if (s < 0)
      break;
    f = fdopen(s, "a+");
    process(f);
    fclose(f);
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
lea    0xdeb(%rip),%rdi
mov    $0x50,%esi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10b0 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1240 <main+0xa0>
jmp    126a <main+0xca>
mov    -0x1c(%rbp),%edi
lea    0xdd8(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <process@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
jmp    121f <main+0x7f>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e39(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %ebp,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
lea    0xdfb(%rip),%rdi
mov    $0x50,%esi
call   1050 <printf@plt>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
test   %eax,%eax
js     125c <main+0xbc>
lea    0xdfd(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,%edi
mov    %r14,%rsi
call   1090 <fdopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <process@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
test   %eax,%eax
jns    1230 <main+0x90>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [34 x i8] c"HTTP server listening on port %d\0A\00"
@.str.1 = [3 x i8] c"a+\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32*
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %2
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* @INADDR_ANY
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = call i32 @htons(i32 80)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* %2
%17 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%18 = call i32 @bind(i32 %16, %struct.sockaddr* %17, i32 12)
%19 = load i32, i32* %2
%20 = call i32 @listen(i32 %19, i32 5)
%21 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 80)
br label %22
22: ; preds = %0, %28
%23 = load i32, i32* %2
%24 = call i32 @accept(i32 %23, i32* null, i32* null)
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %22
br label %35
28: ; preds = %22
%29 = load i32, i32* %4
%30 = call i32* @fdopen(i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %30, i32** %5
%31 = load i32*, i32** %5
%32 = call i32 @process(i32* %31)
%33 = load i32*, i32** %5
%34 = call i32 @fclose(i32* %33)
br label %22
35: ; preds = %27
%36 = load i32, i32* %2
%37 = call i32 @close(i32 %36)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [34 x i8] c"HTTP server listening on port %d\0A\00"
@.str.1 = [3 x i8] c"a+\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @INADDR_ANY
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %8, i32* %9
%10 = tail call i32 @htons(i32 80)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %10, i32* %11
%12 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%13 = call i32 @bind(i32 %5, %struct.sockaddr* %12, i32 12)
%14 = call i32 @listen(i32 %5, i32 5)
%15 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 80)
%16 = call i32 @accept(i32 %5, i32* null, i32* null)
%17 = icmp slt i32 %16, 0
br i1 %17, label %25, label %18
18: ; preds = %0, %18
%19 = phi i32 [ %23, %18 ], [ %16, %0 ]
%20 = call i32* @fdopen(i32 %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @process(i32* %20)
%22 = call i32 @fclose(i32* %20)
%23 = call i32 @accept(i32 %5, i32* null, i32* null)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %18
25: ; preds = %18, %0
%26 = call i32 @close(i32 %5)
ret i32 0
}