#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 /*<<< orphan*/ srvAdr;
typedef int /*<<< orphan*/ cliAdr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errExit(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int write(int, char *, int);
int main() {
  int sockFd, newSockFd;
  struct sockaddr_in srvAdr, cliAdr;
  int cliLen, n;
  char mesg[100];
  int pid;
  if ((sockFd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    errExit("can't open stream socket\n");
  memset(&srvAdr, 0, sizeof(srvAdr));
  srvAdr.sin_family = AF_INET;
  srvAdr.sin_addr.s_addr = htonl(INADDR_ANY);
  srvAdr.sin_port = htons(8000);
  if (bind(sockFd, (struct sockaddr *)&srvAdr, sizeof(srvAdr)) < 0)
    errExit("can't bind local address\n");
  listen(sockFd, 5);
  while (1) {
    printf("server waiting for new connection:\n");
    cliLen = sizeof(cliAdr);
    newSockFd = accept(sockFd, (struct sockaddr *)&cliAdr, &cliLen);
    if (newSockFd < 0)
      errExit("accept error\n");
    printf("connected to client :%s\n", inet_ntoa(cliAdr.sin_addr));
    pid = fork();
    if (pid == 0) {
      while (1) {
        n = read(newSockFd, mesg, 100);
        if (n < 0)
          errExit("recv error\n");
        if (n == 0)
          break;
        mesg[n] = 0;
        if (write(newSockFd, mesg, n) != n)
          errExit("send error\n");
        printf("received and sent following message:\n%s\n", mesg);
      }
      exit(0);
    } else {
      close(newSockFd);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1233 <main+0x43>
lea    0xdd2(%rip),%rdi
call   1040 <errExit@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    $0x1f40,%edi
call   1060 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
cmp    $0x0,%rax
jge    1294 <main+0xa4>
lea    0xd8b(%rip),%rdi
call   1040 <errExit@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   10c0 <listen@plt>
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xc,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12df <main+0xef>
lea    0xd7e(%rip),%rdi
call   1040 <errExit@plt>
mov    -0x28(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd75(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x0,-0xa4(%rbp)
jne    13a7 <main+0x1b7>
jmp    1317 <main+0x127>
mov    -0xc(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x64,%edx
call   10b0 <read@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1344 <main+0x154>
lea    0xd40(%rip),%rdi
call   1040 <errExit@plt>
cmpl   $0x0,-0x30(%rbp)
jne    1353 <main+0x163>
jmp    13a0 <main+0x1b0>
movslq -0x30(%rbp),%rax
movb   $0x0,-0xa0(%rbp,%rax,1)
mov    -0xc(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1030 <write@plt>
cmp    -0x30(%rbp),%eax
je     1386 <main+0x196>
lea    0xd0a(%rip),%rdi
call   1040 <errExit@plt>
lea    -0xa0(%rbp),%rsi
lea    0xd03(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1317 <main+0x127>
xor    %edi,%edi
call   10f0 <exit@plt>
mov    -0xc(%rbp),%edi
call   10a0 <close@plt>
jmp    12a1 <main+0xb1>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    0x2dd0(%rip),%r14
mov    (%r14),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    123d <main+0x3d>
lea    0xdc8(%rip),%rdi
call   1050 <errExit@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10a0 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    $0x1f40,%edi
call   1070 <htons@plt>
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10e0 <bind@plt>
test   %rax,%rax
jns    1298 <main+0x98>
lea    0xd87(%rip),%rdi
call   1050 <errExit@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   10d0 <listen@plt>
lea    0xdf1(%rip),%r14
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
lea    0xd86(%rip),%r13
nopl   0x0(%rax)
mov    %r14,%rdi
call   1030 <puts@plt>
movl   $0xc,0xc(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10f0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12ef <main+0xef>
lea    0xd4a(%rip),%rdi
call   1050 <errExit@plt>
mov    0x20(%rsp),%edi
call   1060 <inet_ntoa@plt>
mov    %r13,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %eax,%eax
je     1319 <main+0x119>
mov    %ebp,%edi
call   10b0 <close@plt>
jmp    12c0 <main+0xc0>
lea    0x30(%rsp),%r13
lea    0xd36(%rip),%r14
lea    0xd47(%rip),%r15
lea    0xd34(%rip),%r12
jmp    134d <main+0x14d>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x64,%edx
call   10c0 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1370 <main+0x170>
jne    1378 <main+0x178>
jmp    139a <main+0x19a>
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1050 <errExit@plt>
movslq %ebx,%rax
movb   $0x0,0x30(%rsp,%rax,1)
mov    %ebp,%edi
mov    %r13,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
cmp    %ebx,%eax
je     1340 <main+0x140>
mov    %r12,%rdi
call   1050 <errExit@plt>
jmp    1340 <main+0x140>
xor    %edi,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [26 x i8] c"can't open stream socket\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [26 x i8] c"can't bind local address\0A\00"
@.str.2 = [36 x i8] c"server waiting for new connection:\0A\00"
@.str.3 = [14 x i8] c"accept error\0A\00"
@.str.4 = [25 x i8] c"connected to client :%s\0A\00"
@.str.5 = [12 x i8] c"recv error\0A\00"
@.str.6 = [12 x i8] c"send error\0A\00"
@.str.7 = [41 x i8] c"received and sent following message:\0A%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100 x i8]
%9 = alloca i32
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* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @errExit(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, 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* %4, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = call i32 @htons(i32 8000)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* %2
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%28 = call i64 @bind(i32 %26, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %16
%31 = call i32 @errExit(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %16
%33 = load i32, i32* %2
%34 = call i32 @listen(i32 %33, i32 5)
br label %35
35: ; preds = %32, %86
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 12, i32* %6
%37 = load i32, i32* %2
%38 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%39 = call i32 @accept(i32 %37, %struct.sockaddr* %38, i32* %6)
store i32 %39, i32* %3
%40 = load i32, i32* %3
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %35
%43 = call i32 @errExit(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %35
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%46 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i8* @inet_ntoa(i32 %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %48)
%50 = call i32 (...) @fork()
store i32 %50, i32* %9
%51 = load i32, i32* %9
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %83
53: ; preds = %44
br label %54
54: ; preds = %53, %78
%55 = load i32, i32* %3
%56 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%57 = call i32 @read(i32 %55, i8* %56, i32 100)
store i32 %57, i32* %7
%58 = load i32, i32* %7
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %54
%61 = call i32 @errExit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %62
62: ; preds = %60, %54
%63 = load i32, i32* %7
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %62
br label %81
66: ; preds = %62
%67 = load i32, i32* %7
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 %68
store i8 0, i8* %69
%70 = load i32, i32* %3
%71 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%72 = load i32, i32* %7
%73 = call i32 @write(i32 %70, i8* %71, i32 %72)
%74 = load i32, i32* %7
%75 = icmp ne i32 %73, %74
br i1 %75, label %76, label %78
76: ; preds = %66
%77 = call i32 @errExit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
br label %78
78: ; preds = %76, %66
%79 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0), i8* %79)
br label %54
81: ; preds = %65
%82 = call i32 @exit(i32 0)
unreachable
83: ; preds = %44
%84 = load i32, i32* %3
%85 = call i32 @close(i32 %84)
br label %86
86: ; preds = %83
br label %35
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [26 x i8] c"can't open stream socket\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [26 x i8] c"can't bind local address\0A\00"
@.str.3 = [14 x i8] c"accept error\0A\00"
@.str.4 = [25 x i8] c"connected to client :%s\0A\00"
@.str.5 = [12 x i8] c"recv error\0A\00"
@.str.6 = [12 x i8] c"send error\0A\00"
@.str.7 = [41 x i8] c"received and sent following message:\0A%s\0A\00"
@str = [35 x i8] c"server waiting for new connection:\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%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 %15
13: ; preds = %0
%14 = tail call i32 @errExit(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %20, i32* %21
%22 = call i32 @htons(i32 8000)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%25 = call i64 @bind(i32 %11, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = call i32 @errExit(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %15
%30 = call i32 @listen(i32 %11, i32 5)
%31 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
br label %33
33: ; preds = %63, %29
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
store i32 12, i32* %3
%35 = call i32 @accept(i32 %11, %struct.sockaddr* %31, i32* %3)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @errExit(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
%40 = load i32, i32* %32
%41 = call i8* @inet_ntoa(i32 %40)
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %41)
%43 = call i32 (...) @fork()
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %63
45: ; preds = %39, %59
%46 = call i32 @read(i32 %35, i8* %8, i32 100)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @errExit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %52
50: ; preds = %45
%51 = icmp eq i32 %46, 0
br i1 %51, label %61, label %52
52: ; preds = %48, %50
%53 = sext i32 %46 to i64
%54 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 %53
store i8 0, i8* %54
%55 = call i32 @write(i32 %35, i8* %8, i32 %46)
%56 = icmp eq i32 %55, %46
br i1 %56, label %59, label %57
57: ; preds = %52
%58 = call i32 @errExit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
br label %59
59: ; preds = %57, %52
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.7, i64 0, i64 0), i8* %8)
br label %45
61: ; preds = %50
%62 = call i32 @exit(i32 0)
unreachable
63: ; preds = %39
%64 = call i32 @close(i32 %35)
br label %33
}