#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*/ srvaddr;
typedef int socklen_t;
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ cliaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int Accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ Bind(int, struct sockaddr *, int);
int /*<<< orphan*/ Close(int);
int /*<<< orphan*/ Fork();
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ Listen(int, int);
int /*<<< orphan*/ SOCK_STREAM;
int Socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ str_echo(int);
int main(void) {
  int listenfd, connfd;
  pid_t childpid;
  socklen_t clilen;
  struct sockaddr_in cliaddr, srvaddr;
  listenfd = Socket(AF_INET, SOCK_STREAM, 0);
  memset(&srvaddr, 0, sizeof(srvaddr));
  srvaddr.sin_family = AF_INET;
  srvaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  srvaddr.sin_port = htons(9877);
  Bind(listenfd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
  Listen(listenfd, 1024);
  for (;;) {
    clilen = sizeof(cliaddr);
    connfd = Accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
    if ((childpid = Fork()) == 0) {
      Close(listenfd);
      str_echo(connfd);
      exit(0);
    }
    Close(connfd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <Socket@plt>
mov    %eax,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x2695,%edi
call   1060 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <Bind@plt>
mov    -0x8(%rbp),%edi
mov    $0x400,%esi
call   1090 <Listen@plt>
movl   $0xc,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1040 <Accept@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1030 <Fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jne    1277 <main+0xc7>
mov    -0x8(%rbp),%edi
call   10b0 <Close@plt>
mov    -0xc(%rbp),%edi
call   1050 <str_echo@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0xc(%rbp),%edi
call   10b0 <Close@plt>
jmp    1233 <main+0x83>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e27(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <Socket@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    $0x2695,%edi
call   1060 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10a0 <Bind@plt>
mov    %r14d,%edi
mov    $0x400,%esi
call   1090 <Listen@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r14d,%edi
call   1040 <Accept@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <Fork@plt>
test   %eax,%eax
je     128a <main+0xda>
lea    0x18(%rsp),%r15
lea    0x4(%rsp),%rbx
mov    %ebp,%edi
call   10b0 <Close@plt>
movl   $0xc,0x4(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <Accept@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <Fork@plt>
test   %eax,%eax
jne    1260 <main+0xb0>
mov    %r14d,%edi
call   10b0 <Close@plt>
mov    %ebp,%edi
call   1050 <str_echo@plt>
xor    %edi,%edi
call   10d0 <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
@INADDR_ANY = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @Socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %2
%11 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = call i32 @htonl(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = call i32 @htons(i32 9877)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* %2
%21 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%22 = call i32 @Bind(i32 %20, %struct.sockaddr* %21, i32 12)
%23 = load i32, i32* %2
%24 = call i32 @Listen(i32 %23, i32 1024)
br label %25
25: ; preds = %37, %0
store i32 12, i32* %5
%26 = load i32, i32* %2
%27 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%28 = call i32 @Accept(i32 %26, %struct.sockaddr* %27, i32* %5)
store i32 %28, i32* %3
%29 = call i32 (...) @Fork()
store i32 %29, i32* %4
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %25
%32 = load i32, i32* %2
%33 = call i32 @Close(i32 %32)
%34 = load i32, i32* %3
%35 = call i32 @str_echo(i32 %34)
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %25
%38 = load i32, i32* %3
%39 = call i32 @Close(i32 %38)
br label %25
}

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
@INADDR_ANY = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @Socket(i32 %7, i32 %8, i32 0)
%10 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = call i32 @htonl(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %14, i32* %15
%16 = call i32 @htons(i32 9877)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %16, i32* %17
%18 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%19 = call i32 @Bind(i32 %9, %struct.sockaddr* %18, i32 12)
%20 = call i32 @Listen(i32 %9, i32 1024)
%21 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
store i32 12, i32* %1
%22 = call i32 @Accept(i32 %9, %struct.sockaddr* %21, i32* %1)
%23 = call i32 (...) @Fork()
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %30, %0
%26 = phi i32 [ %22, %0 ], [ %33, %30 ]
%27 = call i32 @Close(i32 %9)
%28 = call i32 @str_echo(i32 %26)
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %0, %30
%31 = phi i32 [ %33, %30 ], [ %22, %0 ]
%32 = call i32 @Close(i32 %31)
store i32 12, i32* %1
%33 = call i32 @Accept(i32 %9, %struct.sockaddr* %21, i32* %1)
%34 = call i32 (...) @Fork()
%35 = icmp eq i32 %34, 0
br i1 %35, label %25, label %30
}