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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
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 *, char *);
int /*<<< orphan*/ puts(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int listenfd, connfd, n;
  pid_t childpid;
  socklen_t clilen;
  char buf[4096];
  struct sockaddr_in cliaddr, servaddr;
  if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("Problem in creating the socket");
    exit(2);
  }
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(3000);
  bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  listen(listenfd, 8);
  printf("%s\n", "Server running...waiting for connections.");
  for (;;) {
    clilen = sizeof(cliaddr);
    connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
    printf("%s\n", "Received request...");
    if ((childpid = fork()) == 0) {
      printf("%s\n", "Child created for dealing with client requests");
      close(listenfd);
      while ((n = recv(connfd, buf, 4096, 0)) > 0) {
        printf("%s", "String received from and resent to the client:");
        puts(buf);
        send(connfd, buf, n, 0);
        bzero(buf, 4096);
      }
      if (n < 0)
        printf("%s\n", "Read error");
      exit(0);
    }
    close(connfd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1244 <main+0x54>
lea    0xdcb(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x2,%edi
call   10f0 <exit@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1048(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0x104c(%rbp)
mov    $0xbb8,%edi
call   1050 <htons@plt>
mov    %eax,-0x1050(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1050(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    -0x14(%rbp),%edi
mov    $0x8,%esi
call   10a0 <listen@plt>
lea    0xd80(%rip),%rdi
lea    0xd7d(%rip),%rsi
call   1070 <printf@plt>
movl   $0xc,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1040(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x18(%rbp)
lea    0xd50(%rip),%rdi
lea    0xd77(%rip),%rsi
call   1070 <printf@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jne    1398 <main+0x1a8>
lea    0xd2a(%rip),%rdi
lea    0xd65(%rip),%rsi
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1374 <main+0x184>
lea    0xd5e(%rip),%rdi
lea    0xd5a(%rip),%rsi
call   1070 <printf@plt>
lea    -0x1030(%rbp),%rdi
call   1040 <puts@plt>
mov    -0x18(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1060 <send@plt>
lea    -0x1030(%rbp),%rdi
mov    $0x1000,%esi
call   10d0 <bzero@plt>
jmp    1309 <main+0x119>
cmpl   $0x0,-0x1c(%rbp)
jge    1391 <main+0x1a1>
lea    0xc9a(%rip),%rdi
lea    0xd36(%rip),%rsi
call   1070 <printf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
jmp    12ab <main+0xbb>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1038,%rsp
mov    0x2de0(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
test   %eax,%eax
jns    1234 <main+0x44>
lea    0xddb(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x2,%edi
call   10f0 <exit@plt>
mov    %eax,%r14d
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0xbb8,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    %r14d,%edi
mov    $0x8,%esi
call   10a0 <listen@plt>
lea    0xd9c(%rip),%rbx
lea    0xd99(%rip),%rsi
mov    %rbx,%rdi
call   1070 <printf@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   10e0 <accept@plt>
mov    %eax,%ebp
lea    0xd98(%rip),%rsi
mov    %rbx,%rdi
call   1070 <printf@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     1315 <main+0x125>
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
lea    0xd46(%rip),%r13
lea    0xd6d(%rip),%rbx
mov    %ebp,%edi
call   1090 <close@plt>
movl   $0xc,0xc(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10e0 <accept@plt>
mov    %eax,%ebp
mov    %r13,%rdi
mov    %rbx,%rsi
call   1070 <printf@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
jne    12e0 <main+0xf0>
lea    0xd03(%rip),%rdi
lea    0xd3e(%rip),%rsi
call   1070 <printf@plt>
mov    %r14d,%edi
call   1090 <close@plt>
lea    0x30(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    13a5 <main+0x1b5>
lea    0xd40(%rip),%r14
lea    0xd3c(%rip),%r15
lea    0x30(%rsp),%r12
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <printf@plt>
mov    %r12,%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %r12,%rdi
mov    $0x1000,%esi
call   10d0 <bzero@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     1360 <main+0x170>
test   %ebx,%ebx
jns    13bc <main+0x1cc>
lea    0xc6f(%rip),%rdi
lea    0xd0b(%rip),%rsi
call   1070 <printf@plt>
xor    %edi,%edi
call   10f0 <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 = [31 x i8] c"Problem in creating the socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [42 x i8] c"Server running...waiting for connections.\00"
@.str.3 = [20 x i8] c"Received request...\00"
@.str.4 = [47 x i8] c"Child created for dealing with client requests\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [47 x i8] c"String received from and resent to the client:\00"
@.str.7 = [11 x i8] c"Read error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [4096 x i8]
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 2)
unreachable
21: ; preds = %2
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, 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* %13, 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 3000)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%32 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = load i32, i32* %6
%34 = call i32 @listen(i32 %33, i32 8)
%35 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %69, %21
store i32 12, i32* %10
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%39 = call i32 @accept(i32 %37, %struct.sockaddr* %38, i32* %10)
store i32 %39, i32* %7
%40 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 (...) @fork()
store i32 %41, i32* %9
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %69
43: ; preds = %36
%44 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %52, %43
%48 = load i32, i32* %7
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%50 = call i32 @recv(i32 %48, i8* %49, i32 4096, i32 0)
store i32 %50, i32* %8
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %62
52: ; preds = %47
%53 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0))
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%55 = call i32 @puts(i8* %54)
%56 = load i32, i32* %7
%57 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%58 = load i32, i32* %8
%59 = call i32 @send(i32 %56, i8* %57, i32 %58, i32 0)
%60 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%61 = call i32 @bzero(i8* %60, i32 4096)
br label %47
62: ; preds = %47
%63 = load i32, i32* %8
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %62
%66 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
br label %67
67: ; preds = %65, %62
%68 = call i32 @exit(i32 0)
unreachable
69: ; preds = %36
%70 = load i32, i32* %7
%71 = call i32 @close(i32 %70)
br label %36
}

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 = [31 x i8] c"Problem in creating the socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [42 x i8] c"Server running...waiting for connections.\00"
@.str.3 = [20 x i8] c"Received request...\00"
@.str.4 = [47 x i8] c"Child created for dealing with client requests\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [47 x i8] c"String received from and resent to the client:\00"
@.str.7 = [11 x i8] c"Read error\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [4096 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast %struct.sockaddr_in* %6 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 %18
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 2)
unreachable
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, 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* %6, i64 0, i32 1, i32 0
store i32 %22, i32* %23
%24 = tail call i32 @htons(i32 3000)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%27 = call i32 @bind(i32 %13, %struct.sockaddr* %26, i32 12)
%28 = call i32 @listen(i32 %13, i32 8)
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%30 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
store i32 12, i32* %3
%31 = call i32 @accept(i32 %13, %struct.sockaddr* %30, i32* %3)
%32 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 (...) @fork()
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %56
35: ; preds = %56, %18
%36 = phi i32 [ %31, %18 ], [ %59, %56 ]
%37 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @close(i32 %13)
%39 = call i32 @recv(i32 %36, i8* %8, i32 4096, i32 0)
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %49
41: ; preds = %35, %41
%42 = phi i32 [ %47, %41 ], [ %39, %35 ]
%43 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0))
%44 = call i32 @puts(i8* %8)
%45 = call i32 @send(i32 %36, i8* %8, i32 %42, i32 0)
%46 = call i32 @bzero(i8* %8, i32 4096)
%47 = call i32 @recv(i32 %36, i8* %8, i32 4096, i32 0)
%48 = icmp sgt i32 %47, 0
br i1 %48, label %41, label %49
49: ; preds = %41, %35
%50 = phi i32 [ %39, %35 ], [ %47, %41 ]
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
br label %54
54: ; preds = %52, %49
%55 = call i32 @exit(i32 0)
unreachable
56: ; preds = %18, %56
%57 = phi i32 [ %59, %56 ], [ %31, %18 ]
%58 = call i32 @close(i32 %57)
store i32 12, i32* %3
%59 = call i32 @accept(i32 %13, %struct.sockaddr* %30, i32* %3)
%60 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%61 = call i32 (...) @fork()
%62 = icmp eq i32 %61, 0
br i1 %62, label %35, label %56
}