#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 iovec {
  int dummy;
};

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
int readv(int, struct iovec *, int);
int writev(int, struct iovec *, int);
int BMI_sockio_nbvector(int s, struct iovec *vector, int count, int recv_flag) {
  int ret;
  do {
    if (recv_flag) {
      ret = readv(s, vector, count);
    } else {
      ret = writev(s, vector, count);
    }
  } while (ret == -1 && errno == EINTR);
  if (ret == -1 && errno == EWOULDBLOCK)
    return (0);
  return (ret);
}


x86 assembly (clang, -O0):
<BMI_sockio_nbvector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1156 <BMI_sockio_nbvector+0x36>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <readv@plt>
mov    %eax,-0x1c(%rbp)
jmp    1168 <BMI_sockio_nbvector+0x48>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <writev@plt>
mov    %eax,-0x1c(%rbp)
jmp    116d <BMI_sockio_nbvector+0x4d>
xor    %eax,%eax
cmpl   $0xffffffff,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jne    1196 <BMI_sockio_nbvector+0x76>
mov    0x2e5d(%rip),%rax
mov    (%rax),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1135 <BMI_sockio_nbvector+0x15>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d1 <BMI_sockio_nbvector+0xb1>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rax
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
jne    11d1 <BMI_sockio_nbvector+0xb1>
movl   $0x0,-0x4(%rbp)
jmp    11d7 <BMI_sockio_nbvector+0xb7>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BMI_sockio_nbvector>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2ea3(%rip),%r12
mov    0x2e8c(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
test   %r14d,%r14d
je     1170 <BMI_sockio_nbvector+0x50>
call   1040 <readv@plt>
jmp    1175 <BMI_sockio_nbvector+0x55>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
call   1030 <writev@plt>
mov    (%r12),%rdx
cmp    $0xffffffff,%eax
jne    1184 <BMI_sockio_nbvector+0x64>
cmp    0x0(%r13),%rdx
je     1150 <BMI_sockio_nbvector+0x30>
mov    0x2e4d(%rip),%rsi
xor    %ecx,%ecx
cmp    (%rsi),%rdx
cmovne %eax,%ecx
cmp    $0xffffffff,%eax
cmovne %eax,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @BMI_sockio_nbvector(i32 %0, %struct.iovec* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.iovec*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %6
store %struct.iovec* %1, %struct.iovec** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
br label %11
11: ; preds = %32, %4
%12 = load i32, i32* %9
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = load i32, i32* %6
%16 = load %struct.iovec*, %struct.iovec** %7
%17 = load i32, i32* %8
%18 = call i32 @readv(i32 %15, %struct.iovec* %16, i32 %17)
store i32 %18, i32* %10
br label %24
19: ; preds = %11
%20 = load i32, i32* %6
%21 = load %struct.iovec*, %struct.iovec** %7
%22 = load i32, i32* %8
%23 = call i32 @writev(i32 %20, %struct.iovec* %21, i32 %22)
store i32 %23, i32* %10
br label %24
24: ; preds = %19, %14
br label %25
25: ; preds = %24
%26 = load i32, i32* %10
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br label %32
32: ; preds = %28, %25
%33 = phi i1 [ false, %25 ], [ %31, %28 ]
br i1 %33, label %11, label %34
34: ; preds = %32
%35 = load i32, i32* %10
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = load i64, i64* @errno
%39 = load i64, i64* @EWOULDBLOCK
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %37
store i32 0, i32* %5
br label %44
42: ; preds = %37, %34
%43 = load i32, i32* %10
store i32 %43, i32* %5
br label %44
44: ; preds = %42, %41
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
%struct.iovec = type { i32 }
@errno = global i64 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @BMI_sockio_nbvector(i32 %0, %struct.iovec* %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %3, 0
br label %6
6: ; preds = %11, %4
br i1 %5, label %9, label %7
7: ; preds = %6
%8 = tail call i32 @readv(i32 %0, %struct.iovec* %1, i32 %2)
br label %11
9: ; preds = %6
%10 = tail call i32 @writev(i32 %0, %struct.iovec* %1, i32 %2)
br label %11
11: ; preds = %7, %9
%12 = phi i32 [ %8, %7 ], [ %10, %9 ]
%13 = icmp eq i32 %12, -1
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
%17 = select i1 %13, i1 %16, i1 false
br i1 %17, label %6, label %18
18: ; preds = %11
%19 = load i64, i64* @EWOULDBLOCK
%20 = icmp eq i64 %14, %19
%21 = select i1 %13, i1 %20, i1 false
%22 = select i1 %21, i32 0, i32 %12
ret i32 %22
}