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

/* Variables and functions */
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 *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ die_errno(int /*<<< orphan*/);
int /*<<< orphan*/ die_errno_msg(int /*<<< orphan*/, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ handle_client(int);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  struct sockaddr_in server;
  server.sin_family = PF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(8888);
  (void)argc;
  (void)argv;
  int server_fd = socket(PF_INET, SOCK_STREAM, 0);
  if (server_fd < 0)
    die_errno(errno);
  int on = 1;
  if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))
    die_errno(errno);
  if (bind(server_fd, (struct sockaddr *)&server, sizeof(server)))
    die_errno_msg(errno, "cannot open port");
  if (listen(server_fd, 0))
    die_errno(errno);
  for (;;) {
    struct sockaddr_in client;
    int client_fd;
    socklen_t sl = sizeof(client);
    client_fd = accept(server_fd, (struct sockaddr *)&client, &sl);
    if (client_fd < 0) {
      die_errno(errno);
    }
    char *client_ip = inet_ntoa(client.sin_addr);
    printf("Connection received from: %s (%d)\n", client_ip, client_fd);
    handle_client(client_fd);
  }
  return 0;
}


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)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x22b8,%edi
call   1060 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    121f <main+0x6f>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1090 <die_errno@plt>
movl   $0x1,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
je     1262 <main+0xb2>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   1090 <die_errno@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
je     1292 <main+0xe2>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
lea    0xd73(%rip),%rsi
call   10c0 <die_errno_msg@plt>
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
je     12b4 <main+0x104>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
call   1090 <die_errno@plt>
jmp    12b9 <main+0x109>
movl   $0xc,-0x40(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12eb <main+0x13b>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   1090 <die_errno@plt>
mov    -0x38(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rsi
mov    -0x3c(%rbp),%edx
lea    0xd0c(%rip),%rdi
call   1070 <printf@plt>
mov    -0x3c(%rbp),%edi
call   1030 <handle_client@plt>
jmp    12b9 <main+0x109>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2dfb(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    $0x22b8,%edi
call   1060 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%rbx),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    120d <main+0x5d>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   1090 <die_errno@plt>
movl   $0x1,0x4(%rsp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
je     124d <main+0x9d>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   1090 <die_errno@plt>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
je     1279 <main+0xc9>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
lea    0xd8c(%rip),%rsi
call   10c0 <die_errno_msg@plt>
mov    %r14d,%edi
xor    %esi,%esi
call   1080 <listen@plt>
test   %rax,%rax
je     1296 <main+0xe6>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   1090 <die_errno@plt>
lea    0x18(%rsp),%r15
mov    %rsp,%r12
mov    0x2d23(%rip),%r13
lea    0xd65(%rip),%rbx
jmp    12cd <main+0x11d>
xchg   %ax,%ax
mov    0x18(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1070 <printf@plt>
mov    %ebp,%edi
call   1030 <handle_client@plt>
movl   $0xc,(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12b0 <main+0x100>
mov    0x0(%r13),%edi
call   1090 <die_errno@plt>
jmp    12b0 <main+0x100>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [17 x i8] c"cannot open port\00"
@.str.1 = [35 x i8] c"Connection received from: %s (%d)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @PF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_ANY
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = call i32 @htons(i32 8888)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* %4
%21 = load i8**, i8*** %5
%22 = load i32, i32* @PF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %2
%28 = load i32, i32* @errno
%29 = call i32 @die_errno(i32 %28)
br label %30
30: ; preds = %27, %2
store i32 1, i32* %8
%31 = load i32, i32* %7
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_REUSEADDR
%34 = call i64 @setsockopt(i32 %31, i32 %32, i32 %33, i32* %8, i32 4)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = load i32, i32* @errno
%38 = call i32 @die_errno(i32 %37)
br label %39
39: ; preds = %36, %30
%40 = load i32, i32* %7
%41 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%42 = call i64 @bind(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i32, i32* @errno
%46 = call i32 @die_errno_msg(i32 %45, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %47
47: ; preds = %44, %39
%48 = load i32, i32* %7
%49 = call i64 @listen(i32 %48, i32 0)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i32, i32* @errno
%53 = call i32 @die_errno(i32 %52)
br label %54
54: ; preds = %51, %47
br label %55
55: ; preds = %64, %54
store i32 12, i32* %11
%56 = load i32, i32* %7
%57 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%58 = call i32 @accept(i32 %56, %struct.sockaddr* %57, i32* %11)
store i32 %58, i32* %10
%59 = load i32, i32* %10
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %55
%62 = load i32, i32* @errno
%63 = call i32 @die_errno(i32 %62)
br label %64
64: ; preds = %61, %55
%65 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%66 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call i8* @inet_ntoa(i32 %67)
store i8* %68, i8** %12
%69 = load i8*, i8** %12
%70 = load i32, i32* %10
%71 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %69, i32 %70)
%72 = load i32, i32* %10
%73 = call i32 @handle_client(i32 %72)
br label %55
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [17 x i8] c"cannot open port\00"
@.str.1 = [35 x i8] c"Connection received from: %s (%d)\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = load i32, i32* @PF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* @INADDR_ANY
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %10, i32* %11
%12 = tail call i32 @htons(i32 8888)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @PF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i32, i32* @errno
%20 = tail call i32 @die_errno(i32 %19)
br label %21
21: ; preds = %18, %2
%22 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_REUSEADDR
%25 = call i64 @setsockopt(i32 %16, i32 %23, i32 %24, i32* %4, i32 4)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %21
%28 = load i32, i32* @errno
%29 = call i32 @die_errno(i32 %28)
br label %30
30: ; preds = %27, %21
%31 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%32 = call i64 @bind(i32 %16, %struct.sockaddr* %31, i32 12)
%33 = icmp eq i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = load i32, i32* @errno
%36 = call i32 @die_errno_msg(i32 %35, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %37
37: ; preds = %34, %30
%38 = call i64 @listen(i32 %16, i32 0)
%39 = icmp eq i64 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = load i32, i32* @errno
%42 = call i32 @die_errno(i32 %41)
br label %43
43: ; preds = %40, %37
%44 = bitcast %struct.sockaddr_in* %5 to i8*
%45 = bitcast i32* %6 to i8*
%46 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
br label %48
48: ; preds = %54, %43
store i32 12, i32* %6
%49 = call i32 @accept(i32 %16, %struct.sockaddr* %46, i32* %6)
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* @errno
%53 = call i32 @die_errno(i32 %52)
br label %54
54: ; preds = %51, %48
%55 = load i32, i32* %47
%56 = call i8* @inet_ntoa(i32 %55)
%57 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %56, i32 %49)
%58 = call i32 @handle_client(i32 %49)
br label %48
}