#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_DGRAM;
int atoi(char *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ ctu(char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ printf(char *, 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 main() {
  int sockfd, n, sen, i;
  struct sockaddr_in servaddr, cliaddr;
  socklen_t len;
  char mesg[1000];
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(32000);
  bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  len = sizeof(cliaddr);
  n = recvfrom(sockfd, mesg, 1000, 0, (struct sockaddr *)&cliaddr, &len);
  sendto(sockfd, "ack", 3, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
  sen = atoi(mesg);
  for (i = 0; i < sen; i++) {
    n = recvfrom(sockfd, mesg, 1000, 0, (struct sockaddr *)&cliaddr, &len);
    mesg[n] = '\0';
    printf("Received: %s\n", mesg);
    ctu(mesg);
    sendto(sockfd, mesg, n, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x7d00,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
movl   $0xc,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x3e8,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%r8
lea    0xdd1(%rip),%rsi
mov    $0x3,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
lea    -0x420(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    12db <main+0x14b>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x3e8,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rsi
lea    0xd66(%rip),%rdi
call   1040 <printf@plt>
lea    -0x420(%rbp),%rdi
call   1080 <ctu@plt>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    -0x30(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1257 <main+0xc7>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    0x2e40(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%r14d
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x7d00,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
movl   $0xc,0xc(%rsp)
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%rbp
lea    0xc(%rsp),%r9
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
call   1070 <recvfrom@plt>
lea    0xdd8(%rip),%rsi
mov    %r14d,%edi
mov    $0x3,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %rbx,%rdi
call   10a0 <atoi@plt>
test   %eax,%eax
jle    12b4 <main+0x124>
mov    %eax,%ebp
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%r15
lea    0xda5(%rip),%r13
nop
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
mov    %r15,%r8
lea    0xc(%rsp),%r9
call   1070 <recvfrom@plt>
movslq %eax,%r12
movb   $0x0,0x30(%rsp,%r12,1)
mov    %r13,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1080 <ctu@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
add    $0xffffffff,%ebp
jne    1260 <main+0xd0>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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_DGRAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [4 x i8] c"ack\00"
@.str.1 = [14 x i8] c"Received: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca [1000 x i8]
store i32 0, i32* %1
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_ANY
%16 = call i32 @htonl(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
store i32 %16, i32* %18
%19 = call i32 @htons(i32 32000)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* %2
%22 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%23 = call i32 @bind(i32 %21, %struct.sockaddr* %22, i32 12)
store i32 12, i32* %8
%24 = load i32, i32* %2
%25 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%26 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%27 = call i32 @recvfrom(i32 %24, i8* %25, i32 1000, i32 0, %struct.sockaddr* %26, i32* %8)
store i32 %27, i32* %3
%28 = load i32, i32* %2
%29 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%30 = call i32 @sendto(i32 %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3, i32 0, %struct.sockaddr* %29, i32 12)
%31 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %4
store i32 0, i32* %5
br label %33
33: ; preds = %54, %0
%34 = load i32, i32* %5
%35 = load i32, i32* %4
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %57
37: ; preds = %33
%38 = load i32, i32* %2
%39 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%40 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%41 = call i32 @recvfrom(i32 %38, i8* %39, i32 1000, i32 0, %struct.sockaddr* %40, i32* %8)
store i32 %41, i32* %3
%42 = load i32, i32* %3
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 %43
store i8 0, i8* %44
%45 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%46 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %45)
%47 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%48 = call i32 @ctu(i8* %47)
%49 = load i32, i32* %2
%50 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%51 = load i32, i32* %3
%52 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%53 = call i32 @sendto(i32 %49, i8* %50, i32 %51, i32 0, %struct.sockaddr* %52, i32 12)
br label %54
54: ; preds = %37
%55 = load i32, i32* %5
%56 = add nsw i32 %55, 1
store i32 %56, i32* %5
br label %33
57: ; preds = %33
ret i32 0
}

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_DGRAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [4 x i8] c"ack\00"
@.str.1 = [14 x i8] c"Received: %s\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [1000 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 [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = tail call i32 @htonl(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 32000)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%20 = call i32 @bind(i32 %11, %struct.sockaddr* %19, i32 12)
store i32 12, i32* %3
%21 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%22 = call i32 @recvfrom(i32 %11, i8* %8, i32 1000, i32 0, %struct.sockaddr* %21, i32* %3)
%23 = call i32 @sendto(i32 %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3, i32 0, %struct.sockaddr* %21, i32 12)
%24 = call i32 @atoi(i8* %8)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %0, %26
%27 = phi i32 [ %34, %26 ], [ 0, %0 ]
%28 = call i32 @recvfrom(i32 %11, i8* %8, i32 1000, i32 0, %struct.sockaddr* %21, i32* %3)
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%32 = call i32 @ctu(i8* %8)
%33 = call i32 @sendto(i32 %11, i8* %8, i32 %28, i32 0, %struct.sockaddr* %21, i32 12)
%34 = add nuw nsw i32 %27, 1
%35 = icmp eq i32 %34, %24
br i1 %35, label %36, label %26
36: ; preds = %26, %0
ret i32 0
}