#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(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sprintf(char *, char *, char *);
int main(int argc, char **argv) {
  int listenfd, connfd, n;
  struct sockaddr_in servaddr, cliaddr;
  socklen_t clilen;
  pid_t childpid;
  char mesg[1000];
  char resp[1024];
  int rn;
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(32000);
  bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  listen(listenfd, 1024);
  for (;;) {
    clilen = sizeof(cliaddr);
    connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
    if ((childpid = fork()) == 0) {
      close(listenfd);
      for (;;) {
        n = recvfrom(connfd, mesg, 1000, 0, (struct sockaddr *)&cliaddr,
                     &clilen);
        printf("-------------------------------------------------------\n");
        mesg[n] = 0;
        printf("Received the following:\n");
        printf("%s", mesg);
        printf("-------------------------------------------------------\n");
        rn = sprintf(resp, "Hello %s!\n", mesg);
        printf("response: \n");
        printf(resp);
        printf("\n-------------------------------------------------------\n");
        sendto(connfd, resp, rn, 0, (struct sockaddr *)&cliaddr,
               sizeof(cliaddr));
      }
    }
    close(connfd);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%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)
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    $0x7d00,%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    -0x14(%rbp),%edi
mov    $0x400,%esi
call   1080 <listen@plt>
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x0,%eax
jne    136c <main+0x19c>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x430(%rbp),%rsi
lea    -0x38(%rbp),%r8
mov    $0x3e8,%edx
xor    %ecx,%ecx
lea    -0x3c(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x1c(%rbp)
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    0xd2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x430(%rbp),%rsi
lea    0xd32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd3f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x830(%rbp),%rdi
lea    -0x430(%rbp),%rdx
lea    0xd0b(%rip),%rsi
call   10d0 <sprintf@plt>
mov    %eax,-0x834(%rbp)
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x830(%rbp),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xcf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    -0x834(%rbp),%edx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
jmp    1290 <main+0xc0>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
jmp    125b <main+0x8b>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    0x2df0(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x7d00,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    %ebp,%edi
mov    $0x400,%esi
call   1090 <listen@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   10d0 <accept@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     12bb <main+0xdb>
lea    0x20(%rsp),%rbx
lea    0xc(%rsp),%r15
mov    %r14d,%edi
call   1080 <close@plt>
movl   $0xc,0xc(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   10d0 <accept@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
jne    1290 <main+0xb0>
mov    %ebp,%edi
call   1080 <close@plt>
lea    0x30(%rsp),%rbp
lea    0x420(%rsp),%r15
nop
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
lea    0x20(%rsp),%r12
mov    %r12,%r8
lea    0xc(%rsp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,%r13d
lea    0xd39(%rip),%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
movslq %r13d,%rax
movb   $0x0,0x30(%rsp,%rax,1)
lea    0xcfe(%rip),%rdi
call   1030 <puts@plt>
lea    0xce4(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
lea    0xccb(%rip),%rsi
mov    %rbp,%rdx
call   10e0 <sprintf@plt>
mov    %eax,%ebx
lea    0xcdd(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xcd2(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
mov    $0xc,%r9d
call   1070 <sendto@plt>
jmp    12d0 <main+0xf0>

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 = [57 x i8] c"-------------------------------------------------------\0A\00"
@.str.1 = [25 x i8] c"Received the following:\0A\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [11 x i8] c"Hello %s!\0A\00"
@.str.4 = [12 x i8] c"response: \0A\00"
@.str.5 = [58 x i8] c"\0A-------------------------------------------------------\0A\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 %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca i32
%13 = alloca [1000 x i8]
%14 = alloca [1024 x i8]
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %6
%19 = call i32 @bzero(%struct.sockaddr_in* %9, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = call i32 @htons(i32 32000)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %6
%29 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%30 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = load i32, i32* %6
%32 = call i32 @listen(i32 %31, i32 1024)
br label %33
33: ; preds = %67, %2
store i32 12, i32* %11
%34 = load i32, i32* %6
%35 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%36 = call i32 @accept(i32 %34, %struct.sockaddr* %35, i32* %11)
store i32 %36, i32* %7
%37 = call i32 (...) @fork()
store i32 %37, i32* %12
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %67
39: ; preds = %33
%40 = load i32, i32* %6
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %42, %39
%43 = load i32, i32* %7
%44 = getelementptr inbounds [1000 x i8], [1000 x i8]* %13, i64 0, i64 0
%45 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%46 = call i32 @recvfrom(i32 %43, i8* %44, i32 1000, i32 0, %struct.sockaddr* %45, i32* %11)
store i32 %46, i32* %8
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [1000 x i8], [1000 x i8]* %13, i64 0, i64 %49
store i8 0, i8* %50
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%52 = getelementptr inbounds [1000 x i8], [1000 x i8]* %13, i64 0, i64 0
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %52)
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%56 = getelementptr inbounds [1000 x i8], [1000 x i8]* %13, i64 0, i64 0
%57 = call i32 @sprintf(i8* %55, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %56)
store i32 %57, i32* %15
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%60 = call i32 (i8*, ...) @printf(i8* %59)
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0))
%62 = load i32, i32* %7
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%64 = load i32, i32* %15
%65 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%66 = call i32 @sendto(i32 %62, i8* %63, i32 %64, i32 0, %struct.sockaddr* %65, i32 12)
br label %42
67: ; preds = %33
%68 = load i32, i32* %7
%69 = call i32 @close(i32 %68)
br label %33
}

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.2 = [3 x i8] c"%s\00"
@.str.3 = [11 x i8] c"Hello %s!\0A\00"
@str.6 = [24 x i8] c"Received the following:\00"
@str.7 = [56 x i8] c"-------------------------------------------------------\00"
@str.8 = [11 x i8] c"response: \00"
@str.9 = [57 x i8] c"\0A-------------------------------------------------------\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [1000 x i8]
%7 = alloca [1024 x i8]
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = tail call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %20, i32* %21
%22 = tail call i32 @htons(i32 32000)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @bind(i32 %15, %struct.sockaddr* %24, i32 12)
%26 = call i32 @listen(i32 %15, i32 1024)
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
store i32 12, i32* %5
%28 = call i32 @accept(i32 %15, %struct.sockaddr* %27, i32* %5)
%29 = call i32 (...) @fork()
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %47
31: ; preds = %47, %2
%32 = phi i32 [ %28, %2 ], [ %50, %47 ]
%33 = call i32 @close(i32 %15)
br label %34
34: ; preds = %34, %31
%35 = call i32 @recvfrom(i32 %32, i8* %11, i32 1000, i32 0, %struct.sockaddr* %27, i32* %5)
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str.7, i64 0, i64 0))
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 %37
store i8 0, i8* %38
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.6, i64 0, i64 0))
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str.7, i64 0, i64 0))
%42 = call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %11)
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.8, i64 0, i64 0))
%44 = call i32 (i8*, ...) @printf(i8* %12)
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.9, i64 0, i64 0))
%46 = call i32 @sendto(i32 %32, i8* %12, i32 %42, i32 0, %struct.sockaddr* %27, i32 12)
br label %34
47: ; preds = %2, %47
%48 = phi i32 [ %50, %47 ], [ %28, %2 ]
%49 = call i32 @close(i32 %48)
store i32 12, i32* %5
%50 = call i32 @accept(i32 %15, %struct.sockaddr* %27, i32* %5)
%51 = call i32 (...) @fork()
%52 = icmp eq i32 %51, 0
br i1 %52, label %31, label %47
}