#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__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ fromAddr;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             unsigned int *);
int /*<<< orphan*/ stderr;
int ReceiveEchoMessage(int sock, struct sockaddr_in *pServAddr) {
  struct sockaddr_in fromAddr;
  unsigned int fromAddrLen;
  char msgBuffer[(255) + 1];
  int recvMsgLen;
  fromAddrLen = sizeof(fromAddr);
  recvMsgLen = recvfrom(sock, msgBuffer, (255), 0, (struct sockaddr *)&fromAddr,
                        &fromAddrLen);
  if (recvMsgLen < 0) {
    fprintf(stderr, "recvfrom() failed\n");
    return -1;
  }
  if (fromAddr.sin_addr.s_addr != pServAddr->sin_addr.s_addr) {
    fprintf(stderr, "Error: received a packet from unknown source.\n");
    return -1;
  }
  msgBuffer[recvMsgLen] = '\0';
  printf("Received: %s\n", msgBuffer);
  return 0;
}


x86 assembly (clang, -O0):
<ReceiveEchoMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x8,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
lea    -0x18(%rbp),%r8
mov    $0xff,%edx
xor    %ecx,%ecx
lea    -0x1c(%rbp),%r9
call   1050 <recvfrom@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    119b <ReceiveEchoMessage+0x6b>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <ReceiveEchoMessage+0xc6>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
je     11cd <ReceiveEchoMessage+0x9d>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <ReceiveEchoMessage+0xc6>
movslq -0x124(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x120(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReceiveEchoMessage>:
push   %rbx
sub    $0x110,%rsp
mov    %rsi,%rbx
movl   $0x8,0x4(%rsp)
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    $0xff,%edx
xor    %ecx,%ecx
call   1050 <recvfrom@plt>
test   %eax,%eax
js     1188 <ReceiveEchoMessage+0x58>
mov    0x8(%rsp),%rcx
cmp    (%rbx),%rcx
jne    119a <ReceiveEchoMessage+0x6a>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xec8(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1030 <printf@plt>
xor    %eax,%eax
jmp    11b4 <ReceiveEchoMessage+0x84>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
jmp    11aa <ReceiveEchoMessage+0x7a>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x110,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"recvfrom() failed\0A\00"
@.str.1 = [47 x i8] c"Error: received a packet from unknown source.\0A\00"
@.str.2 = [14 x i8] c"Received: %s\0A\00"
define i32 @ReceiveEchoMessage(i32 %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in*
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca [256 x i8]
%9 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
store i32 8, i32* %7
%10 = load i32, i32* %4
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%12 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%13 = call i32 @recvfrom(i32 %10, i8* %11, i32 255, i32 0, %struct.sockaddr* %12, i32* %7)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %37
19: ; preds = %2
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
%22 = load i64, i64* %21
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
%26 = load i64, i64* %25
%27 = icmp ne i64 %22, %26
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %37
31: ; preds = %19
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 %33
store i8 0, i8* %34
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%36 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %35)
store i32 0, i32* %3
br label %37
37: ; preds = %31, %28, %16
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"recvfrom() failed\0A\00"
@.str.1 = [47 x i8] c"Error: received a packet from unknown source.\0A\00"
@.str.2 = [14 x i8] c"Received: %s\0A\00"
define i32 @ReceiveEchoMessage(i32 %0, %struct.sockaddr_in* nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
store i32 8, i32* %4
%9 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%10 = call i32 @recvfrom(i32 %0, i8* %8, i32 255, i32 0, %struct.sockaddr* %9, i32* %4)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %28
15: ; preds = %2
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%17 = load i64, i64* %16
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
%19 = load i64, i64* %18
%20 = icmp eq i64 %17, %19
br i1 %20, label %24, label %21
21: ; preds = %15
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
br label %28
24: ; preds = %15
%25 = zext i32 %10 to i64
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %25
store i8 0, i8* %26
%27 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %28
28: ; preds = %24, %21, %12
%29 = phi i32 [ -1, %12 ], [ -1, %21 ], [ 0, %24 ]
ret i32 %29
}