#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 */

/* Type definitions */
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ send_msg;
typedef int /*<<< orphan*/ recv_msg;

/* Variables and functions */
int /*<<< orphan*/ bzero(void *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             int /*<<< orphan*/ *);
int sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strlen(char *);
void dg_echo(int sockfd, struct sockaddr *cliaddr, socklen_t clilen) {
  socklen_t len;
  int n;
  char recv_msg[256] = {'\0'};
  char send_msg[256] = {'\0'};
  while (1) {
    len = clilen;
    bzero((void *)recv_msg, sizeof(recv_msg));
    bzero((void *)send_msg, sizeof(send_msg));
    n = recvfrom(sockfd, recv_msg, sizeof(recv_msg), 0, cliaddr, &len);
    printf("[server] recv_msg = %s.\n", recv_msg);
    if (n < 0) {
      perror("recvfrom");
      close(sockfd);
      exit(-1);
    }
    sprintf(send_msg, "ECHO BACK : %s", recv_msg);
    n = sendto(sockfd, send_msg, strlen(send_msg), 0, cliaddr, len);
    printf("[server] send_msg = %s.\n", send_msg);
  }
}


x86 assembly (clang, -O0):
<dg_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10a0 <bzero@plt>
lea    -0x220(%rbp),%rdi
mov    $0x100,%esi
call   10a0 <bzero@plt>
mov    -0x4(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x10(%rbp),%r8
mov    $0x100,%edx
xor    %ecx,%ecx
lea    -0x18(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x120(%rbp),%rsi
lea    0xdce(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    125f <dg_echo+0xbf>
lea    0xdd1(%rip),%rdi
call   1090 <perror@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    -0x220(%rbp),%rdi
lea    -0x120(%rbp),%rdx
lea    0xdae(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x22c(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x228(%rbp)
lea    -0x220(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x22c(%rbp),%edi
mov    -0x228(%rbp),%rsi
mov    %eax,%edx
mov    -0x10(%rbp),%r8
mov    -0x18(%rbp),%r9d
xor    %ecx,%ecx
call   1060 <sendto@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x220(%rbp),%rsi
lea    0xd66(%rip),%rdi
call   1040 <printf@plt>
jmp    11db <dg_echo+0x3b>

x86 assembly (clang, -O2):
<dg_echo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %edx,%r14d
mov    %rsi,%r12
mov    %edi,%r15d
xorps  %xmm0,%xmm0
movaps %xmm0,0x200(%rsp)
movaps %xmm0,0x1f0(%rsp)
movaps %xmm0,0x1e0(%rsp)
movaps %xmm0,0x1d0(%rsp)
movaps %xmm0,0x1c0(%rsp)
movaps %xmm0,0x1b0(%rsp)
movaps %xmm0,0x1a0(%rsp)
movaps %xmm0,0x190(%rsp)
movaps %xmm0,0x180(%rsp)
movaps %xmm0,0x170(%rsp)
movaps %xmm0,0x160(%rsp)
movaps %xmm0,0x150(%rsp)
movaps %xmm0,0x140(%rsp)
movaps %xmm0,0x130(%rsp)
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0x110(%rsp),%rbx
lea    0x10(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,0xc(%rsp)
mov    %rbx,%rdi
mov    $0x100,%esi
call   1090 <bzero@plt>
mov    %rbp,%rdi
mov    $0x100,%esi
call   1090 <bzero@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
mov    %r12,%r8
lea    0xc(%rsp),%r9
call   1070 <recvfrom@plt>
mov    %eax,%r13d
lea    0xd0d(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
test   %r13d,%r13d
js     1345 <dg_echo+0x1b5>
mov    %rbp,%rdi
lea    0xd18(%rip),%rsi
mov    %rbx,%rdx
call   10a0 <sprintf@plt>
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    0xc(%rsp),%r9d
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1050 <sendto@plt>
lea    0xcf9(%rip),%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
jmp    12b0 <dg_echo+0x120>
lea    0xccd(%rip),%rdi
call   1080 <perror@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [25 x i8] c"[server] recv_msg = %s.\0A\00"
@.str.1 = [9 x i8] c"recvfrom\00"
@.str.2 = [15 x i8] c"ECHO BACK : %s\00"
@.str.3 = [25 x i8] c"[server] send_msg = %s.\0A\00"
define void @dg_echo(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = alloca [256 x i8]
store i32 %0, i32* %4
store %struct.sockaddr* %1, %struct.sockaddr** %5
store i32 %2, i32* %6
%11 = bitcast [256 x i8]* %9 to i8*
%12 = bitcast [256 x i8]* %10 to i8*
br label %13
13: ; preds = %3, %32
%14 = load i32, i32* %6
store i32 %14, i32* %7
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%16 = call i32 @bzero(i8* %15, i32 256)
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%18 = call i32 @bzero(i8* %17, i32 256)
%19 = load i32, i32* %4
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%21 = load %struct.sockaddr*, %struct.sockaddr** %5
%22 = call i32 @recvfrom(i32 %19, i8* %20, i32 256, i32 0, %struct.sockaddr* %21, i32* %7)
store i32 %22, i32* %8
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%24 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %13
%28 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %13
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%35 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = load i32, i32* %4
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%39 = call i32 @strlen(i8* %38)
%40 = load %struct.sockaddr*, %struct.sockaddr** %5
%41 = load i32, i32* %7
%42 = call i32 @sendto(i32 %36, i8* %37, i32 %39, i32 0, %struct.sockaddr* %40, i32 %41)
store i32 %42, i32* %8
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%44 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %13
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [25 x i8] c"[server] recv_msg = %s.\0A\00"
@.str.1 = [9 x i8] c"recvfrom\00"
@.str.2 = [15 x i8] c"ECHO BACK : %s\00"
@.str.3 = [25 x i8] c"[server] send_msg = %s.\0A\00"
define void @dg_echo(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = bitcast i32* %4 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
br label %10
10: ; preds = %20, %3
store i32 %2, i32* %4
%11 = call i32 @bzero(i8* %8, i32 256)
%12 = call i32 @bzero(i8* %9, i32 256)
%13 = call i32 @recvfrom(i32 %0, i8* %8, i32 256, i32 0, %struct.sockaddr* %1, i32* %4)
%14 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %8)
%15 = icmp slt i32 %13, 0
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @close(i32 %0)
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %10
%21 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%22 = call i32 @strlen(i8* %9)
%23 = load i32, i32* %4
%24 = call i32 @sendto(i32 %0, i8* %9, i32 %22, i32 0, %struct.sockaddr* %1, i32 %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %9)
br label %10
}