#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 timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};
struct sockaddr_pkt {
  int dummy;
};
struct sockaddr_ll {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int pkt_card_sock_read(int msock_h, char *buf, int maxlen,
                       struct sockaddr_ll *fromaddr) {
  fd_set fds;
  int ret_val, recvlen;
  int fromlen;
  struct timeval tv;
  recvlen = 0;
  FD_ZERO(&fds);
  FD_SET(msock_h, &fds);
  tv.tv_sec = 0;
  tv.tv_usec = 5000;
  do {
    ret_val = select(msock_h + 1, &fds, NULL, NULL, &tv);
  } while ((ret_val < 0) && (errno == EINTR));
  if (FD_ISSET(msock_h, &fds)) {
    fromlen = sizeof(struct sockaddr_pkt);
    recvlen = recvfrom(msock_h, buf, maxlen, 0, (struct sockaddr *)fromaddr,
                       &fromlen);
  }
  return (recvlen);
}


x86 assembly (clang, -O0):
<pkt_card_sock_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1070 <FD_SET@plt>
movq   $0x0,-0x38(%rbp)
movl   $0x1388,-0x40(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x28(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x28(%rbp)
mov    %al,-0x41(%rbp)
jge    11d7 <pkt_card_sock_read+0x87>
mov    0x2e1c(%rip),%rax
mov    (%rax),%rax
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1191 <pkt_card_sock_read+0x41>
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     121b <pkt_card_sock_read+0xcb>
movl   $0x4,-0x30(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%r8
xor    %ecx,%ecx
lea    -0x30(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkt_card_sock_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,0x20(%rsp)
mov    %edx,0xc(%rsp)
mov    %rsi,0x18(%rsp)
mov    %edi,%r13d
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
movq   $0x0,0x30(%rsp)
movl   $0x1388,0x28(%rsp)
lea    0x1(%r13),%ebp
lea    0x28(%rsp),%r14
mov    0x2e38(%rip),%r15
mov    0x2e29(%rip),%r12
nop
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1050 <select@plt>
test   %eax,%eax
jns    11ce <pkt_card_sock_read+0x7e>
mov    (%r12),%rax
cmp    %rax,(%r15)
je     11b0 <pkt_card_sock_read+0x60>
lea    0x14(%rsp),%rsi
mov    %r13d,%edi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1207 <pkt_card_sock_read+0xb7>
movl   $0x4,0x10(%rsp)
lea    0x10(%rsp),%r9
mov    %r13d,%edi
mov    0x18(%rsp),%rsi
mov    0xc(%rsp),%edx
xor    %ecx,%ecx
mov    0x20(%rsp),%r8
call   1060 <recvfrom@plt>
jmp    1209 <pkt_card_sock_read+0xb9>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @pkt_card_sock_read(i32 %0, i8* %1, i32 %2, %struct.sockaddr_ll* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_ll*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.timeval
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store %struct.sockaddr_ll* %3, %struct.sockaddr_ll** %8
store i32 0, i32* %11
%14 = call i32 @FD_ZERO(i32* %9)
%15 = load i32, i32* %5
%16 = call i32 @FD_SET(i32 %15, i32* %9)
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i64 0, i64* %17
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 5000, i32* %18
br label %19
19: ; preds = %30, %4
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
%22 = call i32 @select(i32 %21, i32* %9, i32* null, i32* null, %struct.timeval* %13)
store i32 %22, i32* %10
br label %23
23: ; preds = %19
%24 = load i32, i32* %10
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br label %30
30: ; preds = %26, %23
%31 = phi i1 [ false, %23 ], [ %29, %26 ]
br i1 %31, label %19, label %32
32: ; preds = %30
%33 = load i32, i32* %5
%34 = call i64 @FD_ISSET(i32 %33, i32* %9)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %32
store i32 4, i32* %12
%37 = load i32, i32* %5
%38 = load i8*, i8** %6
%39 = load i32, i32* %7
%40 = load %struct.sockaddr_ll*, %struct.sockaddr_ll** %8
%41 = bitcast %struct.sockaddr_ll* %40 to %struct.sockaddr*
%42 = call i32 @recvfrom(i32 %37, i8* %38, i32 %39, i32 0, %struct.sockaddr* %41, i32* %12)
store i32 %42, i32* %11
br label %43
43: ; preds = %36, %32
%44 = load i32, i32* %11
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @pkt_card_sock_read(i32 %0, i8* %1, i32 %2, %struct.sockaddr_ll* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = bitcast %struct.timeval* %7 to i8*
%11 = call i32 @FD_ZERO(i32* %5)
%12 = call i32 @FD_SET(i32 %0, i32* %5)
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
store i64 0, i64* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
store i32 5000, i32* %14
%15 = add nsw i32 %0, 1
br label %16
16: ; preds = %16, %4
%17 = call i32 @select(i32 %15, i32* %5, i32* null, i32* null, %struct.timeval* %7)
%18 = icmp slt i32 %17, 0
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
%22 = select i1 %18, i1 %21, i1 false
br i1 %22, label %16, label %23
23: ; preds = %16
%24 = call i64 @FD_ISSET(i32 %0, i32* %5)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
store i32 4, i32* %6
%27 = bitcast %struct.sockaddr_ll* %3 to %struct.sockaddr*
%28 = call i32 @recvfrom(i32 %0, i8* %1, i32 %2, i32 0, %struct.sockaddr* %27, i32* %6)
br label %29
29: ; preds = %26, %23
%30 = phi i32 [ %28, %26 ], [ 0, %23 ]
ret i32 %30
}