#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 socklen_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ memset(struct sockaddr *, int /*<<< orphan*/, int);
int recvfrom(int, char *, size_t, int, struct sockaddr *, int *);
int sock_receive(int sock, void *buf, size_t len, int flags,
                 struct sockaddr *srcaddr, socklen_t *srcaddrlen) {
  int n;
  *srcaddrlen = sizeof(struct sockaddr);
  memset(srcaddr, 0, sizeof(struct sockaddr));
  while ((n = recvfrom(sock, (char *)buf, len, flags, srcaddr, srcaddrlen)) <
         0) {
    if (errno != EINTR)
      return n;
  }
  return n;
}


x86 assembly (clang, -O0):
<sock_receive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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)
mov    -0x30(%rbp),%rax
movl   $0x4,(%rax)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9
call   1040 <recvfrom@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    11a9 <sock_receive+0x89>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%rax
je     11a4 <sock_receive+0x84>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11af <sock_receive+0x8f>
jmp    1158 <sock_receive+0x38>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_receive>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r14
mov    %r8,%rbx
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %rsi,%r13
mov    %edi,%ebp
movl   $0x4,(%r9)
mov    %r8,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r13,%rsi
mov    %r12,%rdx
mov    %r15d,%ecx
mov    %rbx,%r8
mov    %r14,%r9
call   1040 <recvfrom@plt>
test   %eax,%eax
jns    1190 <sock_receive+0x70>
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e4d(%rip),%rdx
cmp    (%rdx),%rcx
je     1160 <sock_receive+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @sock_receive(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
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
%15 = load i32*, i32** %13
store i32 4, i32* %15
%16 = load %struct.sockaddr*, %struct.sockaddr** %12
%17 = call i32 @memset(%struct.sockaddr* %16, i32 0, i32 4)
br label %18
18: ; preds = %33, %6
%19 = load i32, i32* %8
%20 = load i8*, i8** %9
%21 = load i64, i64* %10
%22 = load i32, i32* %11
%23 = load %struct.sockaddr*, %struct.sockaddr** %12
%24 = load i32*, i32** %13
%25 = call i32 @recvfrom(i32 %19, i8* %20, i64 %21, i32 %22, %struct.sockaddr* %23, i32* %24)
store i32 %25, i32* %14
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %18
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i32, i32* %14
store i32 %32, i32* %7
br label %36
33: ; preds = %27
br label %18
34: ; preds = %18
%35 = load i32, i32* %14
store i32 %35, i32* %7
br label %36
36: ; preds = %34, %31
%37 = load i32, i32* %7
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @sock_receive(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr* %4, i32* %5) {
store i32 4, i32* %5
%7 = tail call i32 @memset(%struct.sockaddr* %4, i32 0, i32 4)
br label %8
8: ; preds = %11, %6
%9 = tail call i32 @recvfrom(i32 %0, i8* %1, i64 %2, i32 %3, %struct.sockaddr* %4, i32* %5)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %8, label %15
15: ; preds = %8, %11
ret i32 %9
}