#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*/ server_addr;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ str_echo(int);
int main(int argc, char **argv) {
  int listenfd, connfd;
  struct sockaddr_in server_addr, client_addr;
  socklen_t addr_len;
  pid_t child_pid;
  if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket() error");
    exit(EXIT_FAILURE);
  }
  bzero(&server_addr, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  server_addr.sin_port = htons(9669);
  if (bind(listenfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) <
      0) {
    perror("bind() error");
    exit(EXIT_FAILURE);
  }
  if (listen(listenfd, 128) < 0) {
    perror("listen() error");
    exit(EXIT_FAILURE);
  }
  for (;;) {
    addr_len = sizeof(client_addr);
    if ((connfd = accept(listenfd, (struct sockaddr *)&client_addr,
                         (socklen_t *)&addr_len)) < 0) {
      perror("accept() error");
      exit(EXIT_FAILURE);
    }
    child_pid = fork();
    if (child_pid < 0) {
      perror("fork() error");
      exit(EXIT_FAILURE);
    } else if (child_pid == 0) {
      close(listenfd);
      str_echo(connfd);
      exit(EXIT_SUCCESS);
    } else
      close(connfd);
  }
}


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    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1215 <main+0x55>
lea    0xdfe(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    $0x25c5,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1282 <main+0xc2>
lea    0xda0(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x80,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    12b3 <main+0xf3>
lea    0xd7c(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    12b8 <main+0xf8>
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12f5 <main+0x135>
lea    0xd49(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jge    1325 <main+0x165>
lea    0xd28(%rip),%rdi
call   1090 <perror@plt>
mov    0x2caa(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
cmpq   $0x0,-0x48(%rbp)
jne    134e <main+0x18e>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <str_echo@plt>
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    135b <main+0x19b>
jmp    12b8 <main+0xf8>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e17(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
jns    11f4 <main+0x34>
lea    0xe11(%rip),%rdi
jmp    12e1 <main+0x121>
mov    %eax,%r14d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    $0x25c5,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jns    1251 <main+0x91>
lea    0xdc3(%rip),%rdi
jmp    12e1 <main+0x121>
mov    %r14d,%edi
mov    $0x80,%esi
call   1070 <listen@plt>
test   %rax,%rax
jns    126c <main+0xac>
lea    0xdb2(%rip),%rdi
jmp    12e1 <main+0x121>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r14d,%edi
call   10b0 <accept@plt>
test   %eax,%eax
js     12d1 <main+0x111>
mov    %eax,%ebp
lea    0x18(%rsp),%r15
lea    0x4(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12da <main+0x11a>
je     12f4 <main+0x134>
mov    %ebp,%edi
call   1060 <close@plt>
movl   $0xc,0x4(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12a0 <main+0xe0>
lea    0xd53(%rip),%rdi
jmp    12e1 <main+0x121>
lea    0xd59(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1030 <str_echo@plt>
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
call   10c0 <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 = [15 x i8] c"socket() error\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [13 x i8] c"bind() error\00"
@.str.2 = [15 x i8] c"listen() error\00"
@.str.3 = [15 x i8] c"accept() error\00"
@.str.4 = [13 x i8] c"fork() error\00"
@EXIT_SUCCESS = 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 %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%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 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 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* %8, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = call i32 @htons(i32 9669)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%32 = call i64 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %20
%35 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %20
%39 = load i32, i32* %6
%40 = call i64 @listen(i32 %39, i32 128)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %38
br label %47
47: ; preds = %78, %46
store i32 12, i32* %10
%48 = load i32, i32* %6
%49 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%50 = call i32 @accept(i32 %48, %struct.sockaddr* %49, i32* %10)
store i32 %50, i32* %7
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %47
%53 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %47
%57 = call i64 (...) @fork()
store i64 %57, i64* %11
%58 = load i64, i64* %11
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %56
%61 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %56
%65 = load i64, i64* %11
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %74
67: ; preds = %64
%68 = load i32, i32* %6
%69 = call i32 @close(i32 %68)
%70 = load i32, i32* %7
%71 = call i32 @str_echo(i32 %70)
%72 = load i32, i32* @EXIT_SUCCESS
%73 = call i32 @exit(i32 %72)
unreachable
74: ; preds = %64
%75 = load i32, i32* %7
%76 = call i32 @close(i32 %75)
br label %77
77: ; preds = %74
br label %78
78: ; preds = %77
br label %47
}

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