#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*/ 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*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(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;
  socklen_t clilen;
  char buf[4096];
  struct sockaddr_in cliaddr, servaddr;
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  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("Server running at port %d waiting for connections.\n", 3000);
  for (;;) {
    clilen = sizeof(cliaddr);
    connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
    printf("%s\n", "Received request...");
    while ((n = recv(connfd, buf, 4096, 0)) > 0) {
      printf("String received from client and resent to the client is : %s\n",
             buf);
      send(connfd, buf, n, 0);
      bzero(buf, 4096);
    }
    if (n < 0) {
      perror("Read error");
      exit(1);
    }
    close(connfd);
  }
  close(listenfd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1038(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x103c(%rbp)
mov    $0xbb8,%edi
call   1040 <htons@plt>
mov    %eax,-0x1040(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1040(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    -0x14(%rbp),%edi
mov    $0x8,%esi
call   1090 <listen@plt>
lea    0xda0(%rip),%rdi
mov    $0xbb8,%esi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
lea    0xdf2(%rip),%rdi
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    12ff <main+0x12f>
lea    -0x1020(%rbp),%rsi
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1050 <send@plt>
lea    -0x1020(%rbp),%rdi
mov    $0x1000,%esi
call   10c0 <bzero@plt>
jmp    129e <main+0xce>
cmpl   $0x0,-0x1c(%rbp)
jge    131f <main+0x14f>
lea    0xd76(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
jmp    126c <main+0x9c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1038,%rsp
mov    0x2df0(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%r13d
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x2dc6(%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    %r13d,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    %r13d,%edi
mov    $0x8,%esi
call   10a0 <listen@plt>
lea    0xda7(%rip),%rdi
mov    $0xbb8,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xdc8(%rip),%r12
lea    0x30(%rsp),%rbx
lea    0xdd0(%rip),%rbp
nopl   0x0(%rax,%rax,1)
movl   $0xc,0xc(%rsp)
mov    %r13d,%edi
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   10e0 <accept@plt>
mov    %eax,%r14d
mov    %r12,%rdi
call   1040 <puts@plt>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r15d
test   %eax,%eax
jle    1300 <main+0x120>
mov    %rbp,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
call   10d0 <bzero@plt>
jmp    12b0 <main+0xd0>
cs nopw 0x0(%rax,%rax,1)
nop
test   %r15d,%r15d
js     1312 <main+0x132>
mov    %r14d,%edi
call   1090 <close@plt>
jmp    1280 <main+0xa0>
lea    0xd6d(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%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
@INADDR_ANY = global i32 0
@.str = [52 x i8] c"Server running at port %d waiting for connections.\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [20 x i8] c"Received request...\00"
@.str.3 = [62 x i8] c"String received from client and resent to the client is : %s\0A\00"
@.str.4 = [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 [4096 x i8]
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = call i32 @htons(i32 3000)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %6
%25 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = load i32, i32* %6
%28 = call i32 @listen(i32 %27, i32 8)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 3000)
br label %30
30: ; preds = %55, %2
store i32 12, i32* %9
%31 = load i32, i32* %6
%32 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%33 = call i32 @accept(i32 %31, %struct.sockaddr* %32, i32* %9)
store i32 %33, i32* %7
%34 = call i32 (i8*, ...) @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.2, i64 0, i64 0))
br label %35
35: ; preds = %40, %30
%36 = load i32, i32* %7
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%38 = call i32 @recv(i32 %36, i8* %37, i32 4096, i32 0)
store i32 %38, i32* %8
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %49
40: ; preds = %35
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0), i8* %41)
%43 = load i32, i32* %7
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%45 = load i32, i32* %8
%46 = call i32 @send(i32 %43, i8* %44, i32 %45, i32 0)
%47 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%48 = call i32 @bzero(i8* %47, i32 4096)
br label %35
49: ; preds = %35
%50 = load i32, i32* %8
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %49
%56 = load i32, i32* %7
%57 = call i32 @close(i32 %56)
br label %30
}

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
@.str = [52 x i8] c"Server running at port %d waiting for connections.\0A\00"
@.str.2 = [20 x i8] c"Received request...\00"
@.str.3 = [62 x i8] c"String received from client and resent to the client is : %s\0A\00"
@.str.4 = [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 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = tail call i32 @htonl(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 3000)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%22 = call i32 @bind(i32 %13, %struct.sockaddr* %21, i32 12)
%23 = call i32 @listen(i32 %13, i32 8)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 3000)
%25 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %26
26: ; preds = %44, %2
store i32 12, i32* %3
%27 = call i32 @accept(i32 %13, %struct.sockaddr* %25, i32* %3)
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @recv(i32 %27, i8* %8, i32 4096, i32 0)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %26, %31
%32 = phi i32 [ %36, %31 ], [ %29, %26 ]
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0), i8* %8)
%34 = call i32 @send(i32 %27, i8* %8, i32 %32, i32 0)
%35 = call i32 @bzero(i8* %8, i32 4096)
%36 = call i32 @recv(i32 %27, i8* %8, i32 4096, i32 0)
%37 = icmp sgt i32 %36, 0
br i1 %37, label %31, label %38
38: ; preds = %31, %26
%39 = phi i32 [ %29, %26 ], [ %36, %31 ]
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %38
%45 = call i32 @close(i32 %27)
br label %26
}