#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_zero;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ handle_connection(int, struct sockaddr_in *);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
int /*<<< orphan*/ printf(char *, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int sockfd, new_sockfd, yes = 1;
  struct sockaddr_in host_addr, client_addr;
  socklen_t sin_size;
  printf("Accepting web requests on port %d\n", 80);
  if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    fatal("in socket");
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
    fatal("setting socket option SO_REUSEADDR");
  host_addr.sin_family = AF_INET;
  host_addr.sin_port = htons(80);
  host_addr.sin_addr.s_addr = INADDR_ANY;
  memset(&(host_addr.sin_zero), '\0', 8);
  if (bind(sockfd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr)) ==
      -1)
    fatal("binding to socket");
  if (listen(sockfd, 20) == -1)
    fatal("listening on socket");
  while (1) {
    sin_size = sizeof(struct sockaddr_in);
    new_sockfd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_size);
    if (new_sockfd == -1)
      fatal("accepting connection");
    handle_connection(new_sockfd, &client_addr);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x10(%rbp)
lea    0xe43(%rip),%rdi
mov    $0x50,%esi
call   1050 <printf@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11f8 <main+0x58>
lea    0xe30(%rip),%rdi
call   10b0 <fatal@plt>
mov    -0x8(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1231 <main+0x91>
lea    0xe01(%rip),%rdi
call   10b0 <fatal@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    128c <main+0xec>
lea    0xdc9(%rip),%rdi
call   10b0 <fatal@plt>
mov    -0x8(%rbp),%edi
mov    $0x14,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12ae <main+0x10e>
lea    0xdb9(%rip),%rdi
call   10b0 <fatal@plt>
jmp    12b3 <main+0x113>
movl   $0x10,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    12e3 <main+0x143>
lea    0xd98(%rip),%rdi
call   10b0 <fatal@plt>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1070 <handle_connection@plt>
jmp    12b3 <main+0x113>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
movl   $0x1,0xc(%rsp)
lea    0xe45(%rip),%rdi
mov    $0x50,%esi
call   1050 <printf@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    11f2 <main+0x52>
lea    0xe36(%rip),%rdi
call   10b0 <fatal@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1228 <main+0x88>
lea    0xe0a(%rip),%rdi
call   10b0 <fatal@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1285 <main+0xe5>
lea    0xdd0(%rip),%rdi
call   10b0 <fatal@plt>
mov    %r14d,%edi
mov    $0x14,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12a3 <main+0x103>
lea    0xdc4(%rip),%rdi
call   10b0 <fatal@plt>
lea    0x20(%rsp),%rbx
lea    0x8(%rsp),%r12
lea    0xdc2(%rip),%r15
jmp    12ca <main+0x12a>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <handle_connection@plt>
movl   $0x10,0x8(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   10a0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    12c0 <main+0x120>
mov    %r15,%rdi
call   10b0 <fatal@plt>
jmp    12c0 <main+0x120>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [35 x i8] c"Accepting web requests on port %d\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"in socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [35 x i8] c"setting socket option SO_REUSEADDR\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.3 = [18 x i8] c"binding to socket\00"
@.str.4 = [20 x i8] c"listening on socket\00"
@.str.5 = [21 x i8] c"accepting connection\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %4
%8 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 80)
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @fatal(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = load i32, i32* %2
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, i32* %4, i32 4)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @fatal(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %15
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 3
store i32 %24, i32* %25
%26 = call i32 @htons(i32 80)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* @INADDR_ANY
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%32 = call i32 @memset(i32* %31, i8 signext 0, i32 8)
%33 = load i32, i32* %2
%34 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%35 = call i32 @bind(i32 %33, %struct.sockaddr* %34, i32 4)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %23
%38 = call i32 @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %23
%40 = load i32, i32* %2
%41 = call i32 @listen(i32 %40, i32 20)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %39
br label %46
46: ; preds = %45, %54
store i32 16, i32* %7
%47 = load i32, i32* %2
%48 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%49 = call i32 @accept(i32 %47, %struct.sockaddr* %48, i32* %7)
store i32 %49, i32* %3
%50 = load i32, i32* %3
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = call i32 @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %52, %46
%55 = load i32, i32* %3
%56 = call i32 @handle_connection(i32 %55, %struct.sockaddr_in* %6)
br label %46
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [35 x i8] c"Accepting web requests on port %d\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"in socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [35 x i8] c"setting socket option SO_REUSEADDR\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.3 = [18 x i8] c"binding to socket\00"
@.str.4 = [20 x i8] c"listening on socket\00"
@.str.5 = [21 x i8] c"accepting connection\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 80)
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = tail call i32 @fatal(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %12, i32 %17, i32 %18, i32* %1, i32 4)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @fatal(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 3
store i32 %24, i32* %25
%26 = call i32 @htons(i32 80)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* @INADDR_ANY
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%31 = call i32 @memset(i32* %30, i8 signext 0, i32 8)
%32 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%33 = call i32 @bind(i32 %12, %struct.sockaddr* %32, i32 4)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %23
%36 = call i32 @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %23
%38 = call i32 @listen(i32 %12, i32 20)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @fatal(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %44
44: ; preds = %49, %42
store i32 16, i32* %4
%45 = call i32 @accept(i32 %12, %struct.sockaddr* %43, i32* %4)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %49
49: ; preds = %47, %44
%50 = call i32 @handle_connection(i32 %45, %struct.sockaddr_in* %3)
br label %44
}