#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 /*<<< orphan*/ sa_data;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ EAGAIN;
int /*<<< orphan*/ d(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcmp(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int recvfrom(int, char *, size_t, int, struct sockaddr *, int /*<<< orphan*/ *);
ssize_t read_only_from(int sock, char *buffer, size_t packetSize, int flags,
                       struct sockaddr *wanted, socklen_t *wantedSize) {
  ssize_t read = 0;
  char *temp = malloc(sizeof(char) * packetSize);
  if (temp == NULL) {
    return -1;
  }
  struct sockaddr from;
  socklen_t fromSize;
  read = recvfrom(sock, temp, packetSize, flags, &from, &fromSize);
  if (1 || read > 0 && !memcmp(from.sa_data, wanted->sa_data, 14)) {
    memcpy(buffer, temp, packetSize);
    free(temp);
    return read;
  }
  d("Discarding foreign packet\n");
  free(temp);
  errno = EAGAIN;
  return -1;
}


x86 assembly (clang, -O0):
<read_only_from>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    118f <read_only_from+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <read_only_from+0x8d>
mov    -0x8(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    -0x48(%rbp),%r8
lea    -0x4c(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_only_from>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r12d
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%ebp
mov    %r15d,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     119b <read_only_from+0x5b>
mov    %rax,%rbx
lea    0x8(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r15,%rdx
mov    %r12d,%ecx
call   1060 <recvfrom@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11a0 <read_only_from+0x60>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@EAGAIN = global i32 0
@errno = global i32 0
define i32 @read_only_from(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr* %4, i32* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca %struct.sockaddr*
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca %struct.sockaddr
%17 = alloca i32
store i32 %0, i32* %8
store i8* %1, i8** %9
store i64 %2, i64* %10
store i32 %3, i32* %11
store %struct.sockaddr* %4, %struct.sockaddr** %12
store i32* %5, i32** %13
store i32 0, i32* %14
%18 = load i64, i64* %10
%19 = mul i64 1, %18
%20 = trunc i64 %19 to i32
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %15
%22 = load i8*, i8** %15
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %25
24: ; preds = %6
store i32 -1, i32* %7
br label %38
25: ; preds = %6
%26 = load i32, i32* %8
%27 = load i8*, i8** %15
%28 = load i64, i64* %10
%29 = load i32, i32* %11
%30 = call i32 @recvfrom(i32 %26, i8* %27, i64 %28, i32 %29, %struct.sockaddr* %16, i32* %17)
store i32 %30, i32* %14
%31 = load i8*, i8** %9
%32 = load i8*, i8** %15
%33 = load i64, i64* %10
%34 = call i32 @memcpy(i8* %31, i8* %32, i64 %33)
%35 = load i8*, i8** %15
%36 = call i32 @free(i8* %35)
%37 = load i32, i32* %14
store i32 %37, i32* %7
br label %38
38: ; preds = %25, %24
%39 = load i32, i32* %7
ret i32 %39
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@EAGAIN = global i32 0
@errno = global i32 0
define i32 @read_only_from(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr* nocapture readnone %4, i32* nocapture readnone %5) {
%7 = alloca %struct.sockaddr
%8 = alloca i32
%9 = trunc i64 %2 to i32
%10 = tail call i8* @malloc(i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %18, label %12
12: ; preds = %6
%13 = bitcast %struct.sockaddr* %7 to i8*
%14 = bitcast i32* %8 to i8*
%15 = call i32 @recvfrom(i32 %0, i8* %10, i64 %2, i32 %3, %struct.sockaddr* %7, i32* %8)
%16 = call i32 @memcpy(i8* %1, i8* %10, i64 %2)
%17 = call i32 @free(i8* %10)
br label %18
18: ; preds = %6, %12
%19 = phi i32 [ %15, %12 ], [ -1, %6 ]
ret i32 %19
}