#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(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 *pcliaddr, socklen_t clilen) {
  int n;
  char recv_msg[256] = {'\0'};
  char send_msg[256] = {'\0'};
  while (1) {
    bzero(recv_msg, sizeof(recv_msg));
    bzero(send_msg, sizeof(send_msg));
    n = recvfrom(sockfd, recv_msg, sizeof(recv_msg), 0, pcliaddr, &clilen);
    if (n < 0) {
      if (EINTR == errno) {
        continue;
      }
      perror("recvfrom");
      close(sockfd);
      exit(EXIT_FAILURE);
    }
    sprintf(send_msg, "ECHO BACK : %s", recv_msg);
    n = sendto(sockfd, send_msg, strlen(send_msg), 0, pcliaddr, clilen);
    if (n < 0) {
      if (EINTR == errno) {
        continue;
      }
      perror("sendto");
      close(sockfd);
      exit(EXIT_FAILURE);
    }
  }
}


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   1040 <memset@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1090 <bzero@plt>
lea    -0x220(%rbp),%rdi
mov    $0x100,%esi
call   1090 <bzero@plt>
mov    -0x4(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x10(%rbp),%r8
mov    $0x100,%edx
xor    %ecx,%ecx
lea    -0x14(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1259 <dg_echo+0xc9>
mov    0x2db1(%rip),%rax
mov    (%rax),%rax
mov    0x2db7(%rip),%rcx
cmp    (%rcx),%rax
jne    1237 <dg_echo+0xa7>
jmp    11cb <dg_echo+0x3b>
lea    0xdc2(%rip),%rdi
call   1080 <perror@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x220(%rbp),%rdi
lea    -0x120(%rbp),%rdx
lea    0xd9b(%rip),%rsi
call   10a0 <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    -0x14(%rbp),%r9d
xor    %ecx,%ecx
call   1050 <sendto@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1302 <dg_echo+0x172>
mov    0x2d08(%rip),%rax
mov    (%rax),%rax
mov    0x2d0e(%rip),%rcx
cmp    (%rcx),%rax
jne    12e0 <dg_echo+0x150>
jmp    11cb <dg_echo+0x3b>
lea    0xd31(%rip),%rdi
call   1080 <perror@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    11cb <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    %rsi,%r15
mov    %edi,%r14d
mov    %edx,0xc(%rsp)
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),%rbp
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r12
lea    0xd67(%rip),%r13
jmp    12ca <dg_echo+0x14a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2d19(%rip),%rax
mov    (%rax),%rax
mov    0x2d1f(%rip),%rcx
cmp    (%rcx),%rax
jne    1350 <dg_echo+0x1d0>
mov    %rbp,%rdi
mov    $0x100,%esi
call   1080 <bzero@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
call   1080 <bzero@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   1060 <recvfrom@plt>
test   %eax,%eax
js     12b0 <dg_echo+0x130>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    0xc(%rsp),%r9d
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1040 <sendto@plt>
test   %eax,%eax
jns    12ca <dg_echo+0x14a>
mov    0x2c98(%rip),%rax
mov    (%rax),%rax
mov    0x2c9e(%rip),%rcx
cmp    (%rcx),%rax
je     12ca <dg_echo+0x14a>
lea    0xcca(%rip),%rdi
jmp    1357 <dg_echo+0x1d7>
lea    0xca9(%rip),%rdi
call   1070 <perror@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@EINTR = global i64 0
@errno = global i64 0
@.str = [9 x i8] c"recvfrom\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [15 x i8] c"ECHO BACK : %s\00"
@.str.2 = [7 x i8] c"sendto\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 [256 x i8]
%9 = alloca [256 x i8]
store i32 %0, i32* %4
store %struct.sockaddr* %1, %struct.sockaddr** %5
store i32 %2, i32* %6
%10 = bitcast [256 x i8]* %8 to i8*
%11 = bitcast [256 x i8]* %9 to i8*
br label %12
12: ; preds = %3, %27, %51, %58
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%14 = call i32 @bzero(i8* %13, i32 256)
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%16 = call i32 @bzero(i8* %15, i32 256)
%17 = load i32, i32* %4
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%19 = load %struct.sockaddr*, %struct.sockaddr** %5
%20 = call i32 @recvfrom(i32 %17, i8* %18, i32 256, i32 0, %struct.sockaddr* %19, i32* %6)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %12
%24 = load i64, i64* @EINTR
%25 = load i64, i64* @errno
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
br label %12
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %12
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%37 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = load i32, i32* %4
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%41 = call i32 @strlen(i8* %40)
%42 = load %struct.sockaddr*, %struct.sockaddr** %5
%43 = load i32, i32* %6
%44 = call i32 @sendto(i32 %38, i8* %39, i32 %41, i32 0, %struct.sockaddr* %42, i32 %43)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %58
47: ; preds = %34
%48 = load i64, i64* @EINTR
%49 = load i64, i64* @errno
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %52
51: ; preds = %47
br label %12
52: ; preds = %47
%53 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%54 = load i32, i32* %4
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %34
br label %12
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@EINTR = global i64 0
@errno = global i64 0
@.str = [9 x i8] c"recvfrom\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [15 x i8] c"ECHO BACK : %s\00"
@.str.2 = [7 x i8] c"sendto\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]
store i32 %2, i32* %4
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
br label %9
9: ; preds = %18, %3
%10 = call i32 @bzero(i8* %7, i32 256)
%11 = call i32 @bzero(i8* %8, i32 256)
%12 = call i32 @recvfrom(i32 %0, i8* %7, i32 256, i32 0, %struct.sockaddr* %1, i32* %4)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %9
%15 = load i64, i64* @EINTR
%16 = load i64, i64* @errno
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14, %30, %24
br label %9
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @close(i32 %0)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %9
%25 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%26 = call i32 @strlen(i8* %8)
%27 = load i32, i32* %4
%28 = call i32 @sendto(i32 %0, i8* %8, i32 %26, i32 0, %struct.sockaddr* %1, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %18
30: ; preds = %24
%31 = load i64, i64* @EINTR
%32 = load i64, i64* @errno
%33 = icmp eq i64 %31, %32
br i1 %33, label %18, label %34
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @close(i32 %0)
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
}