#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 mq_opt_info {
  int dummy;
};
struct mq_object {
  int dummy;
};

/* Variables and functions */
#define EAGAIN 129
#define EINTR 128
int /*<<< orphan*/ ETIMEDOUT;
int /*<<< orphan*/ MSG_NOSIGNAL;
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ comm_get_rest_time(struct mq_object const *,
                                      struct mq_opt_info const *);
int /*<<< orphan*/ comm_wait_fd(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int recv(int, char *, size_t, int /*<<< orphan*/);
int comm_recv_n(const struct mq_object *mq, int fd, char *data, size_t count,
                const struct mq_opt_info *info,
                int (*is_finish)(char *data, size_t count)) {
  size_t recv_size = 0;
  do {
    int r;
    r = recv(fd, data + recv_size, count - recv_size, MSG_NOSIGNAL);
    switch (r) {
    case 0:
      return recv_size;
    case -1:
      switch (errno) {
      case EAGAIN:
        if (comm_wait_fd(fd, POLLIN, comm_get_rest_time(mq, info)) == 0) {
          errno = ETIMEDOUT;
          return recv_size;
        }
        break;
      case EINTR:
        continue;
      default:
        return -1;
      }
      break;
    default:
      recv_size += r;
      break;
    }
  } while ((recv_size < count) &&
           ((is_finish == NULL) || (is_finish(data, recv_size) < 0)));
  return recv_size;
}


x86 assembly (clang, -O0):
<comm_recv_n>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x40(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%rdx
sub    %rax,%rdx
mov    0x2e55(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
sub    $0xffffffff,%eax
je     11ad <comm_recv_n+0x7d>
jmp    1191 <comm_recv_n+0x61>
mov    -0x48(%rbp),%eax
test   %eax,%eax
jne    124a <comm_recv_n+0x11a>
jmp    11a1 <comm_recv_n+0x71>
mov    -0x40(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    12ad <comm_recv_n+0x17d>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
sub    $0x80,%eax
je     1234 <comm_recv_n+0x104>
jmp    11c9 <comm_recv_n+0x99>
mov    -0x4c(%rbp),%eax
sub    $0x81,%eax
jne    1239 <comm_recv_n+0x109>
jmp    11dc <comm_recv_n+0xac>
mov    -0x14(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <comm_get_rest_time@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%esi
mov    %eax,%edx
call   1050 <comm_wait_fd@plt>
cmp    $0x0,%eax
jne    122f <comm_recv_n+0xff>
mov    0x2db8(%rip),%rax
mov    (%rax),%ecx
mov    0x2db7(%rip),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    12ad <comm_recv_n+0x17d>
jmp    1245 <comm_recv_n+0x115>
jmp    125b <comm_recv_n+0x12b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <comm_recv_n+0x17d>
jmp    1256 <comm_recv_n+0x126>
movslq -0x44(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    125b <comm_recv_n+0x12b>
mov    -0x40(%rbp),%rcx
xor    %eax,%eax
cmp    -0x28(%rbp),%rcx
mov    %al,-0x55(%rbp)
jae    129b <comm_recv_n+0x16b>
mov    $0x1,%al
cmpq   $0x0,-0x38(%rbp)
mov    %al,-0x56(%rbp)
je     1295 <comm_recv_n+0x165>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
setl   %al
mov    %al,-0x56(%rbp)
mov    -0x56(%rbp),%al
mov    %al,-0x55(%rbp)
mov    -0x55(%rbp),%al
test   $0x1,%al
jne    1157 <comm_recv_n+0x27>
mov    -0x40(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<comm_recv_n>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%r14
mov    %r8,0x10(%rsp)
mov    %rcx,%r12
mov    %rdx,%r13
mov    %esi,%r15d
mov    %rdi,0x8(%rsp)
xor    %ebx,%ebx
mov    0x2e6b(%rip),%rbp
nopl   (%rax)
lea    (%rbx,%r13,1),%rsi
mov    %r12,%rdx
sub    %rbx,%rdx
mov    0x0(%rbp),%ecx
mov    %r15d,%edi
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1190 <comm_recv_n+0x60>
test   %eax,%eax
je     122b <comm_recv_n+0xfb>
cltq
add    %rax,%rbx
jmp    11f1 <comm_recv_n+0xc1>
nopl   0x0(%rax)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
cmp    $0x80,%eax
je     11f1 <comm_recv_n+0xc1>
cmp    $0x81,%eax
jne    1212 <comm_recv_n+0xe2>
mov    %r12,%rbp
mov    %r13,%r12
mov    %r14,%r13
mov    %r15d,%r14d
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x8(%rsp),%rdi
mov    0x10(%rsp),%rsi
call   1040 <comm_get_rest_time@plt>
mov    %r15d,%edi
mov    0x4(%rsp),%esi
mov    %eax,%edx
call   1050 <comm_wait_fd@plt>
test   %eax,%eax
je     1219 <comm_recv_n+0xe9>
mov    %r13,%r14
mov    %r12,%r13
mov    %rbp,%r12
mov    0x2dd7(%rip),%rbp
cmp    %r12,%rbx
jae    122b <comm_recv_n+0xfb>
test   %r14,%r14
je     1160 <comm_recv_n+0x30>
mov    %r13,%rdi
mov    %rbx,%rsi
call   *%r14
test   %eax,%eax
js     1160 <comm_recv_n+0x30>
jmp    122b <comm_recv_n+0xfb>
mov    $0xffffffff,%ebx
jmp    122b <comm_recv_n+0xfb>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    0x2daf(%rip),%rcx
mov    %eax,(%rcx)
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mq_object = type { i32 }
%struct.mq_opt_info = type { i32 }
@MSG_NOSIGNAL = global i32 0
@errno = global i32 0
@POLLIN = global i32 0
@ETIMEDOUT = global i32 0
define i32 @comm_recv_n(%struct.mq_object* %0, i32 %1, i8* %2, i64 %3, %struct.mq_opt_info* %4, i32 (i8*, i64)* %5) {
%7 = alloca i32
%8 = alloca %struct.mq_object*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca %struct.mq_opt_info*
%13 = alloca i32 (i8*, i64)*
%14 = alloca i64
%15 = alloca i32
store %struct.mq_object* %0, %struct.mq_object** %8
store i32 %1, i32* %9
store i8* %2, i8** %10
store i64 %3, i64* %11
store %struct.mq_opt_info* %4, %struct.mq_opt_info** %12
store i32 (i8*, i64)* %5, i32 (i8*, i64)** %13
store i64 0, i64* %14
br label %16
16: ; preds = %69, %6
%17 = load i32, i32* %9
%18 = load i8*, i8** %10
%19 = load i64, i64* %14
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = load i64, i64* %11
%22 = load i64, i64* %14
%23 = sub i64 %21, %22
%24 = load i32, i32* @MSG_NOSIGNAL
%25 = call i32 @recv(i32 %17, i8* %20, i64 %23, i32 %24)
store i32 %25, i32* %15
%26 = load i32, i32* %15
switch i32 %26, label %48 [
i32 0, label %27
i32 -1, label %30
]
27: ; preds = %16
%28 = load i64, i64* %14
%29 = trunc i64 %28 to i32
store i32 %29, i32* %7
br label %74
30: ; preds = %16
%31 = load i32, i32* @errno
switch i32 %31, label %46 [
i32 129, label %32
i32 128, label %45
]
32: ; preds = %30
%33 = load i32, i32* %9
%34 = load i32, i32* @POLLIN
%35 = load %struct.mq_object*, %struct.mq_object** %8
%36 = load %struct.mq_opt_info*, %struct.mq_opt_info** %12
%37 = call i32 @comm_get_rest_time(%struct.mq_object* %35, %struct.mq_opt_info* %36)
%38 = call i32 @comm_wait_fd(i32 %33, i32 %34, i32 %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %32
%41 = load i32, i32* @ETIMEDOUT
store i32 %41, i32* @errno
%42 = load i64, i64* %14
%43 = trunc i64 %42 to i32
store i32 %43, i32* %7
br label %74
44: ; preds = %32
br label %47
45: ; preds = %30
br label %54
46: ; preds = %30
store i32 -1, i32* %7
br label %74
47: ; preds = %44
br label %53
48: ; preds = %16
%49 = load i32, i32* %15
%50 = sext i32 %49 to i64
%51 = load i64, i64* %14
%52 = add i64 %51, %50
store i64 %52, i64* %14
br label %53
53: ; preds = %48, %47
br label %54
54: ; preds = %53, %45
%55 = load i64, i64* %14
%56 = load i64, i64* %11
%57 = icmp ult i64 %55, %56
br i1 %57, label %58, label %69
58: ; preds = %54
%59 = load i32 (i8*, i64)*, i32 (i8*, i64)** %13
%60 = icmp eq i32 (i8*, i64)* %59, null
br i1 %60, label %67, label %61
61: ; preds = %58
%62 = load i32 (i8*, i64)*, i32 (i8*, i64)** %13
%63 = load i8*, i8** %10
%64 = load i64, i64* %14
%65 = call i32 %62(i8* %63, i64 %64)
%66 = icmp slt i32 %65, 0
br label %67
67: ; preds = %61, %58
%68 = phi i1 [ true, %58 ], [ %66, %61 ]
br label %69
69: ; preds = %67, %54
%70 = phi i1 [ false, %54 ], [ %68, %67 ]
br i1 %70, label %16, label %71
71: ; preds = %69
%72 = load i64, i64* %14
%73 = trunc i64 %72 to i32
store i32 %73, i32* %7
br label %74
74: ; preds = %71, %46, %40, %27
%75 = load i32, i32* %7
ret i32 %75
}

LLVM IR (-O2):
%struct.mq_object = type { i32 }
%struct.mq_opt_info = type { i32 }
@MSG_NOSIGNAL = global i32 0
@errno = global i32 0
@POLLIN = global i32 0
@ETIMEDOUT = global i32 0
define i32 @comm_recv_n(%struct.mq_object* %0, i32 %1, i8* %2, i64 %3, %struct.mq_opt_info* %4, i32 (i8*, i64)* %5) {
%7 = icmp eq i32 (i8*, i64)* %5, null
br label %8
8: ; preds = %36, %6
%9 = phi i64 [ 0, %6 ], [ %30, %36 ]
%10 = getelementptr inbounds i8, i8* %2, i64 %9
%11 = sub i64 %3, %9
%12 = load i32, i32* @MSG_NOSIGNAL
%13 = tail call i32 @recv(i32 %1, i8* %10, i64 %11, i32 %12)
switch i32 %13, label %26 [
i32 0, label %14
i32 -1, label %16
]
14: ; preds = %8
%15 = trunc i64 %9 to i32
br label %39
16: ; preds = %8
%17 = load i32, i32* @errno
switch i32 %17, label %39 [
i32 129, label %18
i32 128, label %29
]
18: ; preds = %16
%19 = load i32, i32* @POLLIN
%20 = tail call i32 @comm_get_rest_time(%struct.mq_object* %0, %struct.mq_opt_info* %4)
%21 = tail call i32 @comm_wait_fd(i32 %1, i32 %19, i32 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %18
%24 = load i32, i32* @ETIMEDOUT
store i32 %24, i32* @errno
%25 = trunc i64 %9 to i32
br label %39
26: ; preds = %8
%27 = sext i32 %13 to i64
%28 = add i64 %9, %27
br label %29
29: ; preds = %16, %18, %26
%30 = phi i64 [ %9, %16 ], [ %28, %26 ], [ %9, %18 ]
%31 = icmp ult i64 %30, %3
br i1 %31, label %32, label %37
32: ; preds = %29
br i1 %7, label %36, label %33
33: ; preds = %32
%34 = tail call i32 %5(i8* %2, i64 %30)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33, %32
br label %8
37: ; preds = %29, %33
%38 = trunc i64 %30 to i32
br label %39
39: ; preds = %16, %14, %23, %37
%40 = phi i32 [ %38, %37 ], [ %15, %14 ], [ %25, %23 ], [ -1, %16 ]
ret i32 %40
}