#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*/ serv_addr;
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ on;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
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 /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ str_echo(int);
int main() {
  pid_t pid;
  int listenfd, connfd, on;
  socklen_t clilen;
  char recv_buf[256];
  struct sockaddr_in serv_addr, cli_addr;
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  if (listenfd < 0) {
    perror("socket");
    exit(EXIT_FAILURE);
  }
  setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  bzero(&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(10001);
  if (bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) {
    perror("bind");
    exit(EXIT_FAILURE);
  }
  listen(listenfd, 5);
  clilen = sizeof(cli_addr);
  for (;;) {
    connfd = accept(listenfd, (struct sockaddr *)&cli_addr, &clilen);
    if (connfd < 0) {
      if (EINTR == errno) {
        continue;
      }
      perror("accept");
      exit(EXIT_FAILURE);
    }
    printf("new connect from %d.\n", connfd);
    if (0 == (pid = fork())) {
      close(listenfd);
      str_echo(connfd);
      exit(EXIT_SUCCESS);
    }
    close(connfd);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%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,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1232 <main+0x52>
lea    0xde1(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0xc(%rbp),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x130(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x12c(%rbp)
mov    $0x2711,%edi
call   1050 <htons@plt>
mov    %eax,-0x130(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
je     12d2 <main+0xf2>
lea    0xd48(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
movl   $0xc,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x140(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    133f <main+0x15f>
mov    0x2ca3(%rip),%rax
mov    (%rax),%rax
mov    0x2ca9(%rip),%rcx
cmp    (%rcx),%rax
jne    1325 <main+0x145>
jmp    12e6 <main+0x106>
lea    0xce0(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%esi
lea    0xcca(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jne    1382 <main+0x1a2>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
mov    -0x10(%rbp),%edi
call   1040 <str_echo@plt>
mov    0x2c2d(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%edi
call   1080 <close@plt>
jmp    12e6 <main+0x106>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2deb(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
jns    1218 <main+0x38>
lea    0xded(%rip),%rdi
jmp    132d <main+0x14d>
mov    %eax,%ebx
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    $0x2711,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jne    1301 <main+0x121>
mov    %ebx,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r15
mov    0x2d10(%rip),%r13
lea    0xd5c(%rip),%r12
jmp    12d0 <main+0xf0>
nopl   0x0(%rax)
mov    0x2ce9(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
jne    1326 <main+0x146>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10d0 <accept@plt>
test   %eax,%eax
js     12c0 <main+0xe0>
mov    %eax,%ebp
mov    %r12,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     130a <main+0x12a>
mov    %ebp,%edi
call   1080 <close@plt>
jmp    12d0 <main+0xf0>
lea    0xcff(%rip),%rdi
jmp    132d <main+0x14d>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %ebp,%edi
call   1040 <str_echo@plt>
mov    0x2c89(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
lea    0xcdf(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

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 = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@EINTR = global i64 0
@errno = global i64 0
@.str.2 = [7 x i8] c"accept\00"
@.str.3 = [22 x i8] c"new connect from %d.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %0
%20 = load i32, i32* %3
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %5, i32 4)
%24 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* @INADDR_ANY
%28 = call i32 @htonl(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = call i32 @htons(i32 10001)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %3
%34 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%35 = call i64 @bind(i32 %33, %struct.sockaddr* %34, i32 12)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %19
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %19
%42 = load i32, i32* %3
%43 = call i32 @listen(i32 %42, i32 5)
store i32 12, i32* %6
br label %44
44: ; preds = %71, %54, %41
%45 = load i32, i32* %3
%46 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%47 = call i32 @accept(i32 %45, %struct.sockaddr* %46, i32* %6)
store i32 %47, i32* %4
%48 = load i32, i32* %4
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %59
50: ; preds = %44
%51 = load i64, i64* @EINTR
%52 = load i64, i64* @errno
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %50
br label %44
55: ; preds = %50
%56 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %44
%60 = load i32, i32* %4
%61 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %60)
%62 = call i32 (...) @fork()
store i32 %62, i32* %2
%63 = icmp eq i32 0, %62
br i1 %63, label %64, label %71
64: ; preds = %59
%65 = load i32, i32* %3
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* %4
%68 = call i32 @str_echo(i32 %67)
%69 = load i32, i32* @EXIT_SUCCESS
%70 = call i32 @exit(i32 %69)
unreachable
71: ; preds = %59
%72 = load i32, i32* %4
%73 = call i32 @close(i32 %72)
br label %44
}

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 = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@EINTR = global i64 0
@errno = global i64 0
@.str.2 = [7 x i8] c"accept\00"
@.str.3 = [22 x i8] c"new connect from %d.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%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 slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %0
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_REUSEADDR
%20 = call i32 @setsockopt(i32 %11, i32 %18, i32 %19, i32* %1, i32 4)
%21 = call i32 @bzero(%struct.sockaddr_in* %3, 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 = call i32 @htonl(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %25, i32* %26
%27 = call i32 @htons(i32 10001)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i64 @bind(i32 %11, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %17
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %17
%37 = call i32 @listen(i32 %11, i32 5)
store i32 12, i32* %2
%38 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %39
39: ; preds = %61, %36
%40 = call i32 @accept(i32 %11, %struct.sockaddr* %38, i32* %2)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %39
%43 = load i64, i64* @EINTR
%44 = load i64, i64* @errno
%45 = icmp eq i64 %43, %44
br i1 %45, label %61, label %46
46: ; preds = %42
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %39
%51 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %40)
%52 = call i32 (...) @fork()
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %50
%55 = call i32 @close(i32 %11)
%56 = call i32 @str_echo(i32 %40)
%57 = load i32, i32* @EXIT_SUCCESS
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %50
%60 = call i32 @close(i32 %40)
br label %61
61: ; preds = %59, %42
br label %39
}