#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*/ socklen_t;
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ client;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ handle_connection(int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ set_sock_opt(int, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int server_sockfd;
  int client_sockfd;
  struct sockaddr_in server;
  struct sockaddr_in client;
  size_t sockaddr_in_len = sizeof(struct sockaddr_in);
  (void)argc;
  (void)argv;
  server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (server_sockfd == -1) {
    fprintf(stderr, "Can't open server socket: %s\n", strerror(errno));
    return EXIT_FAILURE;
  }
  set_sock_opt(server_sockfd, SO_REUSEADDR, true);
  memset(&server, 0, sizeof(server));
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(8081);
  if (-1 == bind(server_sockfd, (struct sockaddr *)&server, sizeof(server))) {
    fprintf(stderr, "Bind failed on endpoint socket: %s\n", strerror(errno));
    return EXIT_FAILURE;
  }
  if (-1 == listen(server_sockfd, 3)) {
    fprintf(stderr, "Listen failed on endpoint socket: %s\n", strerror(errno));
    return EXIT_FAILURE;
  }
  puts("Waiting for incoming connections…");
  while (true) {
    memset(&client, 0x00, sizeof(client));
    client_sockfd = accept(server_sockfd, (struct sockaddr *)&client,
                           (socklen_t *)&sockaddr_in_len);
    if (client_sockfd == -1) {
      fprintf(stderr, "Accept failed on server socket: %s\n", strerror(errno));
      return EXIT_FAILURE;
    }
    puts("Connection accepted");
    handle_connection(client_sockfd);
    puts("Ready for more");
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0xc,-0x40(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1231 <main+0x81>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xde5(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13cd <main+0x21d>
mov    -0x14(%rbp),%edi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1050 <set_sock_opt@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x1f91,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12d9 <main+0x129>
mov    0x2d2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xd5b(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2ce9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13cd <main+0x21d>
mov    -0x14(%rbp),%edi
mov    $0x3,%esi
call   1090 <listen@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1332 <main+0x182>
mov    0x2cd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xd26(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2c90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13cd <main+0x21d>
lea    0xd2f(%rip),%rdi
call   1030 <puts@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    13a8 <main+0x1f8>
mov    0x2c5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2c42(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x50(%rbp),%edi
mov    %rax,%rdx
lea    0xcfa(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2c1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13cd <main+0x21d>
lea    0xd01(%rip),%rdi
call   1030 <puts@plt>
mov    -0x18(%rbp),%edi
call   1080 <handle_connection@plt>
lea    0xd01(%rip),%rdi
call   1030 <puts@plt>
jmp    133e <main+0x18e>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movq   $0xc,(%rsp)
mov    0x2e1b(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     131b <main+0x16b>
mov    %eax,%r13d
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
mov    $0x1,%edx
call   1050 <set_sock_opt@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    $0x1f91,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     133b <main+0x18b>
mov    %r13d,%edi
mov    $0x3,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
je     135b <main+0x1ab>
lea    0xdfa(%rip),%rdi
call   1030 <puts@plt>
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    %rsp,%rdx
mov    %r13d,%edi
mov    %rbx,%rsi
call   10b0 <accept@plt>
cmp    $0xffffffff,%eax
je     12fb <main+0x14b>
mov    %eax,%ebp
lea    0xe0d(%rip),%r14
lea    0xe1a(%rip),%r15
lea    0x18(%rsp),%rbx
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1080 <handle_connection@plt>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    12c0 <main+0x110>
mov    0x2cce(%rip),%rax
mov    (%rax),%ebx
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd73(%rip),%rsi
jmp    1379 <main+0x1c9>
mov    0x2cae(%rip),%rax
mov    (%rax),%ebx
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xcc7(%rip),%rsi
jmp    1379 <main+0x1c9>
mov    0x2c8e(%rip),%rax
mov    (%rax),%ebx
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xcc5(%rip),%rsi
jmp    1379 <main+0x1c9>
mov    0x2c6e(%rip),%rax
mov    (%rax),%ebx
mov    0x2c55(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xcc9(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x2c2e(%rip),%rax
mov    (%rax),%eax
add    $0x28,%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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"Can't open server socket: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [36 x i8] c"Bind failed on endpoint socket: %s\0A\00"
@.str.2 = [38 x i8] c"Listen failed on endpoint socket: %s\0A\00"
@.str.3 = [36 x i8] c"Waiting for incoming connections\E2\80\A6\00"
@.str.4 = [36 x i8] c"Accept failed on server socket: %s\0A\00"
@.str.5 = [20 x i8] c"Connection accepted\00"
@.str.6 = [15 x i8] c"Ready for more\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 %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 12, i64* %10
%11 = load i32, i32* %4
%12 = load i8**, i8*** %5
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
store i32 %23, i32* %3
br label %77
24: ; preds = %2
%25 = load i32, i32* %6
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 @set_sock_opt(i32 %25, i32 %26, i32 1)
%28 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = call i32 @htons(i32 8081)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %34, i32* %35
%36 = load i32, i32* %6
%37 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%38 = call i32 @bind(i32 %36, %struct.sockaddr* %37, i32 12)
%39 = icmp eq i32 -1, %38
br i1 %39, label %40, label %46
40: ; preds = %24
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %43)
%45 = load i32, i32* @EXIT_FAILURE
store i32 %45, i32* %3
br label %77
46: ; preds = %24
%47 = load i32, i32* %6
%48 = call i32 @listen(i32 %47, i32 3)
%49 = icmp eq i32 -1, %48
br i1 %49, label %50, label %56
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %53)
%55 = load i32, i32* @EXIT_FAILURE
store i32 %55, i32* %3
br label %77
56: ; preds = %46
%57 = call i32 @puts(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %58
58: ; preds = %56, %72
%59 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%60 = load i32, i32* %6
%61 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%62 = bitcast i64* %10 to i32*
%63 = call i32 @accept(i32 %60, %struct.sockaddr* %61, i32* %62)
store i32 %63, i32* %7
%64 = load i32, i32* %7
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %72
66: ; preds = %58
%67 = load i32, i32* @stderr
%68 = load i32, i32* @errno
%69 = call i8* @strerror(i32 %68)
%70 = call i32 @fprintf(i32 %67, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %69)
%71 = load i32, i32* @EXIT_FAILURE
store i32 %71, i32* %3
br label %77
72: ; preds = %58
%73 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%74 = load i32, i32* %7
%75 = call i32 @handle_connection(i32 %74)
%76 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %58
77: ; preds = %66, %50, %40, %18
%78 = load i32, i32* %3
ret i32 %78
}

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
@stderr = global i32 0
@.str = [30 x i8] c"Can't open server socket: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [36 x i8] c"Bind failed on endpoint socket: %s\0A\00"
@.str.2 = [38 x i8] c"Listen failed on endpoint socket: %s\0A\00"
@.str.3 = [36 x i8] c"Waiting for incoming connections\E2\80\A6\00"
@.str.4 = [36 x i8] c"Accept failed on server socket: %s\0A\00"
@.str.5 = [20 x i8] c"Connection accepted\00"
@.str.6 = [15 x i8] c"Ready for more\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i64
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i64* %5 to i8*
store i64 12, i64* %5
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %64
18: ; preds = %2
%19 = load i32, i32* @SO_REUSEADDR
%20 = tail call i32 @set_sock_opt(i32 %11, i32 %19, i32 1)
%21 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %24, i32* %25
%26 = call i32 @htons(i32 8081)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i32 @bind(i32 %11, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %36
31: ; preds = %18
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %34)
br label %64
36: ; preds = %18
%37 = call i32 @listen(i32 %11, i32 3)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %42)
br label %64
44: ; preds = %36
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%47 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%48 = bitcast i64* %5 to i32*
%49 = call i32 @accept(i32 %11, %struct.sockaddr* %47, i32* %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %56
51: ; preds = %56, %44
%52 = load i32, i32* @stderr
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %54)
br label %64
56: ; preds = %44, %56
%57 = phi i32 [ %62, %56 ], [ %49, %44 ]
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%59 = call i32 @handle_connection(i32 %57)
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%61 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%62 = call i32 @accept(i32 %11, %struct.sockaddr* %47, i32* %48)
%63 = icmp eq i32 %62, -1
br i1 %63, label %51, label %56
64: ; preds = %51, %39, %31, %13
%65 = load i32, i32* @EXIT_FAILURE
ret i32 %65
}