#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 timespec {
  int tv_sec;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ sigset_t;
typedef scalar_t__ sig_atomic_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_BLOCK;
scalar_t__ errno;
int /*<<< orphan*/ memset(struct timespec *, int /*<<< orphan*/, int);
int pselect(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
            int /*<<< orphan*/ *, struct timespec *, int /*<<< orphan*/ *);
int recv(int, void *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ sigdelset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
ssize_t atomic_recv(int fd, void *buf, size_t size,
                    volatile sig_atomic_t *flag) {
  sigset_t sigmask;
  fd_set readfds;
  struct timespec timeout;
  int r;
  sigprocmask(SIG_BLOCK, NULL, &sigmask);
  sigdelset(&sigmask, SIGCHLD);
  FD_ZERO(&readfds);
  FD_SET(fd, &readfds);
  memset(&timeout, 0, sizeof timeout);
  timeout.tv_sec = 10;
  r = pselect(fd + 1, &readfds, NULL, NULL, &timeout, &sigmask);
  if (r == -1) {
    if (errno == EINTR && flag && *flag)
      return 0;
    else
      return r;
  }
  if (r == 0) {
    return 0;
  }
  return recv(fd, buf, size, 0);
}


x86 assembly (clang, -O0):
<atomic_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x24(%rbp),%rdx
call   1030 <sigprocmask@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    -0x24(%rbp),%rdi
call   1080 <sigdelset@plt>
lea    -0x28(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <FD_SET@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
movl   $0xa,-0x30(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
lea    -0x24(%rbp),%r9
mov    %rcx,%rdx
call   1050 <pselect@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1253 <atomic_recv+0xe3>
mov    0x2dcb(%rip),%rax
mov    (%rax),%rax
mov    0x2db1(%rip),%rcx
cmp    (%rcx),%rax
jne    1248 <atomic_recv+0xd8>
cmpq   $0x0,-0x20(%rbp)
je     1248 <atomic_recv+0xd8>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmp    $0x0,%rax
je     1248 <atomic_recv+0xd8>
movl   $0x0,-0x4(%rbp)
jmp    127e <atomic_recv+0x10e>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    127e <atomic_recv+0x10e>
cmpl   $0x0,-0x34(%rbp)
jne    1269 <atomic_recv+0xf9>
movl   $0x0,-0x4(%rbp)
jmp    127e <atomic_recv+0x10e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<atomic_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r12
mov    %rdx,0x8(%rsp)
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0x14(%rsp),%r13
xor    %esi,%esi
mov    %r13,%rdx
call   1030 <sigprocmask@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    %r13,%rdi
call   1080 <sigdelset@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
movl   $0xa,(%rsp)
lea    0x1(%rbx),%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %r13,%r9
call   1050 <pselect@plt>
test   %eax,%eax
je     123e <atomic_recv+0xce>
cmp    $0xffffffff,%eax
jne    122d <atomic_recv+0xbd>
test   %r12,%r12
je     1226 <atomic_recv+0xb6>
mov    0x2dca(%rip),%rax
mov    0x2db3(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,(%rax)
jne    1226 <atomic_recv+0xb6>
xor    %eax,%eax
cmpq   $0x0,(%r12)
jne    123e <atomic_recv+0xce>
mov    $0xffffffff,%eax
jmp    123e <atomic_recv+0xce>
mov    %ebx,%edi
mov    %r15,%rsi
mov    0x8(%rsp),%rdx
xor    %ecx,%ecx
call   1040 <recv@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@SIG_BLOCK = global i32 0
@SIGCHLD = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @atomic_recv(i32 %0, i8* %1, i64 %2, i64* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64*
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.timespec
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64* %3, i64** %9
%14 = load i32, i32* @SIG_BLOCK
%15 = call i32 @sigprocmask(i32 %14, i32* null, i32* %10)
%16 = load i32, i32* @SIGCHLD
%17 = call i32 @sigdelset(i32* %10, i32 %16)
%18 = call i32 @FD_ZERO(i32* %11)
%19 = load i32, i32* %6
%20 = call i32 @FD_SET(i32 %19, i32* %11)
%21 = call i32 @memset(%struct.timespec* %12, i32 0, i32 4)
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 0
store i32 10, i32* %22
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
%25 = call i32 @pselect(i32 %24, i32* %11, i32* null, i32* null, %struct.timespec* %12, i32* %10)
store i32 %25, i32* %13
%26 = load i32, i32* %13
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %42
28: ; preds = %4
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %40
32: ; preds = %28
%33 = load i64*, i64** %9
%34 = icmp ne i64* %33, null
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i64*, i64** %9
%37 = load volatile i64, i64* %36
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 0, i32* %5
br label %51
40: ; preds = %35, %32, %28
%41 = load i32, i32* %13
store i32 %41, i32* %5
br label %51
42: ; preds = %4
%43 = load i32, i32* %13
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %42
store i32 0, i32* %5
br label %51
46: ; preds = %42
%47 = load i32, i32* %6
%48 = load i8*, i8** %7
%49 = load i64, i64* %8
%50 = call i32 @recv(i32 %47, i8* %48, i64 %49, i32 0)
store i32 %50, i32* %5
br label %51
51: ; preds = %46, %45, %40, %39
%52 = load i32, i32* %5
ret i32 %52
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@SIG_BLOCK = global i32 0
@SIGCHLD = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @atomic_recv(i32 %0, i8* %1, i64 %2, i64* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timespec
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = bitcast %struct.timespec* %7 to i8*
%11 = load i32, i32* @SIG_BLOCK
%12 = call i32 @sigprocmask(i32 %11, i32* null, i32* %5)
%13 = load i32, i32* @SIGCHLD
%14 = call i32 @sigdelset(i32* %5, i32 %13)
%15 = call i32 @FD_ZERO(i32* %6)
%16 = call i32 @FD_SET(i32 %0, i32* %6)
%17 = call i32 @memset(%struct.timespec* %7, i32 0, i32 4)
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i64 0, i32 0
store i32 10, i32* %18
%19 = add nsw i32 %0, 1
%20 = call i32 @pselect(i32 %19, i32* %6, i32* null, i32* null, %struct.timespec* %7, i32* %5)
switch i32 %20, label %31 [
i32 -1, label %21
i32 0, label %33
]
21: ; preds = %4
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
%25 = icmp ne i64* %3, null
%26 = and i1 %25, %24
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load volatile i64, i64* %3
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27, %21
br label %33
31: ; preds = %4
%32 = call i32 @recv(i32 %0, i8* %1, i64 %2, i32 0)
br label %33
33: ; preds = %4, %27, %31, %30
%34 = phi i32 [ -1, %30 ], [ %32, %31 ], [ 0, %27 ], [ %20, %4 ]
ret i32 %34
}