#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*/ servaddr;
typedef int /*<<< orphan*/ opt;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int listenfd, connfd;
  char buffer[1];
  struct sockaddr_in servaddr;
  int opt = 1;
  int len = sizeof(opt);
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, len);
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(8888);
  bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  listen(listenfd, SOMAXCONN);
  connfd = accept(listenfd, NULL, NULL);
  for (;;) {
    read(connfd, buffer, 1);
    write(connfd, buffer, 1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x2c(%rbp)
movl   $0x4,-0x30(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    -0x30(%rbp),%r8d
lea    -0x2c(%rbp),%rcx
call   1030 <setsockopt@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    -0x14(%rbp),%edi
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
call   1090 <listen@plt>
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    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    -0x18(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    1270 <main+0xd0>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
movl   $0x1,0xc(%rsp)
mov    0x2e28(%rip),%r15
mov    (%r15),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1090 <listen@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
mov    %eax,%ebx
lea    0xb(%rsp),%r14
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    1260 <main+0xc0>

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
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@SOMAXCONN = 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 [1 x i8]
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %10
store i32 4, i32* %11
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = load i32, i32* %11
%19 = call i32 @setsockopt(i32 %15, i32 %16, i32 %17, i32* %10, i32 %18)
%20 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = call i32 @htonl(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = call i32 @htons(i32 8888)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* %6
%30 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%31 = call i32 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = load i32, i32* %6
%33 = load i32, i32* @SOMAXCONN
%34 = call i32 @listen(i32 %32, i32 %33)
%35 = load i32, i32* %6
%36 = call i32 @accept(i32 %35, i32* null, i32* null)
store i32 %36, i32* %7
br label %37
37: ; preds = %37, %2
%38 = load i32, i32* %7
%39 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 1)
%41 = load i32, i32* %7
%42 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 0
%43 = call i32 @write(i32 %41, i8* %42, i32 1)
br label %37
}

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
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@SOMAXCONN = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %5 to i8*
store i32 1, i32* %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 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_REUSEADDR
%14 = call i32 @setsockopt(i32 %11, i32 %12, i32 %13, i32* %5, i32 4)
%15 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = call i32 @htons(i32 8888)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%24 = call i32 @bind(i32 %11, %struct.sockaddr* %23, i32 12)
%25 = load i32, i32* @SOMAXCONN
%26 = call i32 @listen(i32 %11, i32 %25)
%27 = call i32 @accept(i32 %11, i32* null, i32* null)
br label %28
28: ; preds = %28, %2
%29 = call i32 @read(i32 %27, i8* %6, i32 1)
%30 = call i32 @write(i32 %27, i8* %6, i32 1)
br label %28
}