#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 socklen_t;
typedef int /*<<< orphan*/ servAddr;
typedef int /*<<< orphan*/ cliAddr;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
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 close(int);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntop(int /*<<< orphan*/, TYPE_1__ *, char *, int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ respond(int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              void const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int listenfd, connfd;
  int opt;
  struct sockaddr_in cliAddr, servAddr;
  char buff[4096];
  socklen_t cliLen;
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  if (listenfd < 0)
    err_sys("socket error");
  memset(&servAddr, 0, sizeof(servAddr));
  servAddr.sin_family = AF_INET;
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servAddr.sin_port = htons(11111);
  opt = 1;
  setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt,
             sizeof(int));
  if (bind(listenfd, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0)
    err_sys("bind error");
  if (listen(listenfd, 1024) < 0) {
    err_sys("listen error");
    return 1;
  }
  for (;;) {
    cliLen = sizeof(cliAddr);
    connfd = accept(listenfd, (struct sockaddr *)&cliAddr, &cliLen);
    if (connfd < 0) {
      err_sys("accept error");
      break;
    } else {
      printf("Connection from %s, port %d\n",
             inet_ntop(AF_INET, &cliAddr.sin_addr, buff, sizeof(buff)),
             ntohs(cliAddr.sin_port));
      respond(connfd);
      if (close(connfd) == -1) {
        err_sys("close error");
        break;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1224 <main+0x44>
lea    0xde1(%rip),%rdi
call   1050 <err_sys@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10a0 <memset@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x2b67,%edi
call   1070 <htons@plt>
mov    %eax,-0x30(%rbp)
movl   $0x1,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    0x2d48(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1060 <setsockopt@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <bind@plt>
cmp    $0x0,%rax
jge    12b0 <main+0xd0>
lea    0xd62(%rip),%rdi
call   1050 <err_sys@plt>
mov    -0x8(%rbp),%edi
mov    $0x400,%esi
call   10c0 <listen@plt>
cmp    $0x0,%rax
jge    12df <main+0xff>
lea    0xd4a(%rip),%rdi
call   1050 <err_sys@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a0 <main+0x1c0>
jmp    12e4 <main+0x104>
movl   $0xc,-0x1034(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x1034(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    131f <main+0x13f>
lea    0xd10(%rip),%rdi
call   1050 <err_sys@plt>
jmp    1399 <main+0x1b9>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
add    $0x4,%rsi
lea    -0x1030(%rbp),%rdx
mov    $0x1000,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0x1040(%rbp)
mov    -0x20(%rbp),%edi
call   10d0 <ntohs@plt>
mov    -0x1040(%rbp),%rsi
mov    %eax,%edx
lea    0xcd2(%rip),%rdi
call   1080 <printf@plt>
mov    -0xc(%rbp),%edi
call   1040 <respond@plt>
mov    -0xc(%rbp),%edi
call   10b0 <close@plt>
cmp    $0xffffffff,%eax
jne    138f <main+0x1af>
lea    0xcca(%rip),%rdi
call   1050 <err_sys@plt>
jmp    1399 <main+0x1b9>
jmp    1394 <main+0x1b4>
jmp    12e4 <main+0x104>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1038,%rsp
mov    0x2de8(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%r13d
test   %eax,%eax
jns    121d <main+0x3d>
lea    0xde8(%rip),%rdi
call   1050 <err_sys@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10a0 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x2b67,%edi
call   1070 <htons@plt>
mov    %eax,0x10(%rsp)
movl   $0x1,0xc(%rsp)
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
mov    0x2d51(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r13d,%edi
mov    $0x4,%r8d
call   1060 <setsockopt@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10e0 <bind@plt>
test   %rax,%rax
jns    12a5 <main+0xc5>
lea    0xd6d(%rip),%rdi
call   1050 <err_sys@plt>
mov    %r13d,%edi
mov    $0x400,%esi
call   10c0 <listen@plt>
test   %rax,%rax
js     132e <main+0x14e>
lea    0xd74(%rip),%r15
xor    %ebp,%ebp
movl   $0xc,0x8(%rsp)
mov    %r13d,%edi
lea    0x20(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   10f0 <accept@plt>
test   %eax,%eax
js     133c <main+0x15c>
mov    %eax,%r12d
mov    (%rbx),%edi
lea    0x24(%rsp),%rsi
lea    0x30(%rsp),%rdx
mov    $0x1000,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%r14
mov    0x20(%rsp),%edi
call   10d0 <ntohs@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1080 <printf@plt>
mov    %r12d,%edi
call   1040 <respond@plt>
mov    %r12d,%edi
call   10b0 <close@plt>
cmp    $0xffffffff,%eax
jne    12c0 <main+0xe0>
lea    0xd23(%rip),%rdi
jmp    1343 <main+0x163>
mov    $0x1,%ebp
lea    0xcde(%rip),%rdi
jmp    1343 <main+0x163>
lea    0xce2(%rip),%rdi
call   1050 <err_sys@plt>
mov    %ebp,%eax
add    $0x1038,%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
@.str = [13 x i8] c"socket error\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"bind error\00"
@.str.2 = [13 x i8] c"listen error\00"
@.str.3 = [13 x i8] c"accept error\00"
@.str.4 = [29 x i8] c"Connection from %s, port %d\0A\00"
@.str.5 = [12 x i8] c"close error\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 [4096 x i8]
%8 = alloca i32
store i32 0, i32* %1
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = call i32 @htons(i32 11111)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %24, i32* %25
store i32 1, i32* %4
%26 = load i32, i32* %2
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_REUSEADDR
%29 = bitcast i32* %4 to i8*
%30 = call i32 @setsockopt(i32 %26, i32 %27, i32 %28, i8* %29, i32 4)
%31 = load i32, i32* %2
%32 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%33 = call i64 @bind(i32 %31, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %16
%36 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %35, %16
%38 = load i32, i32* %2
%39 = call i64 @listen(i32 %38, i32 1024)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %71
43: ; preds = %37
br label %44
44: ; preds = %69, %43
store i32 12, i32* %8
%45 = load i32, i32* %2
%46 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%47 = call i32 @accept(i32 %45, %struct.sockaddr* %46, i32* %8)
store i32 %47, i32* %3
%48 = load i32, i32* %3
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %44
%51 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %70
52: ; preds = %44
%53 = load i32, i32* @AF_INET
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%55 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%56 = call i8* @inet_ntop(i32 %53, %struct.TYPE_2__* %54, i8* %55, i32 4096)
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @ntohs(i32 %58)
%60 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %56, i32 %59)
%61 = load i32, i32* %3
%62 = call i32 @respond(i32 %61)
%63 = load i32, i32* %3
%64 = call i32 @close(i32 %63)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %68
66: ; preds = %52
%67 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %70
68: ; preds = %52
br label %69
69: ; preds = %68
br label %44
70: ; preds = %66, %50
store i32 0, i32* %1
br label %71
71: ; preds = %70, %41
%72 = load i32, i32* %1
ret i32 %72
}

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
@.str = [13 x i8] c"socket error\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"bind error\00"
@.str.2 = [13 x i8] c"listen error\00"
@.str.3 = [13 x i8] c"accept error\00"
@.str.4 = [29 x i8] c"Connection from %s, port %d\0A\00"
@.str.5 = [12 x i8] c"close error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca [4096 x i8]
%5 = alloca i32
%6 = bitcast i32* %1 to i8*
%7 = bitcast %struct.sockaddr_in* %2 to i8*
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%10 = bitcast i32* %5 to i8*
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = tail call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %22, i32* %23
%24 = call i32 @htons(i32 11111)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %24, i32* %25
store i32 1, i32* %1
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_REUSEADDR
%28 = call i32 @setsockopt(i32 %13, i32 %26, i32 %27, i8* %6, i32 4)
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i64 @bind(i32 %13, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %17
%33 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %17
%35 = call i64 @listen(i32 %13, i32 1024)
%36 = icmp slt i64 %35, 0
br i1 %36, label %53, label %37
37: ; preds = %34
%38 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
br label %41
41: ; preds = %37, %44
store i32 12, i32* %5
%42 = call i32 @accept(i32 %13, %struct.sockaddr* %38, i32* %5)
%43 = icmp slt i32 %42, 0
br i1 %43, label %53, label %44
44: ; preds = %41
%45 = load i32, i32* @AF_INET
%46 = call i8* @inet_ntop(i32 %45, %struct.TYPE_2__* %39, i8* %9, i32 4096)
%47 = load i32, i32* %40
%48 = call i32 @ntohs(i32 %47)
%49 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %46, i32 %48)
%50 = call i32 @respond(i32 %42)
%51 = call i32 @close(i32 %42)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %41
53: ; preds = %44, %41, %34
%54 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), %34 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), %41 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), %44 ]
%55 = phi i32 [ 1, %34 ], [ 0, %41 ], [ 0, %44 ]
%56 = call i32 @err_sys(i8* %54)
ret i32 %55
}