#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef scalar_t__ uint16_t;
struct TYPE_2__ {
  int fd;
};
struct epoll_event {
  TYPE_1__ data;
  int /*<<< orphan*/ events;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ EPOLLIN;
int /*<<< orphan*/ EPOLL_CTL_ADD;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_DOMAIN;
int /*<<< orphan*/ close(int);
int epoll_create(int);
scalar_t__ epoll_ctl(int, int /*<<< orphan*/, int, struct epoll_event *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, ...);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__ *,
                      int /*<<< orphan*/ *);
int receive_once(int, int);
int /*<<< orphan*/ send_from_v4(int);
void test(int *rcv_fds, int count, int proto) {
  struct epoll_event ev;
  int epfd, i, test_fd;
  uint16_t test_family;
  socklen_t len;
  epfd = epoll_create(1);
  if (epfd < 0)
    error(1, errno, "failed to create epoll");
  ev.events = EPOLLIN;
  for (i = 0; i < count; ++i) {
    ev.data.fd = rcv_fds[i];
    if (epoll_ctl(epfd, EPOLL_CTL_ADD, rcv_fds[i], &ev))
      error(1, errno, "failed to register sock epoll");
  }
  send_from_v4(proto);
  test_fd = receive_once(epfd, proto);
  if (getsockopt(test_fd, SOL_SOCKET, SO_DOMAIN, &test_family, &len))
    error(1, errno, "failed to read socket domain");
  if (test_family != AF_INET)
    error(1, 0, "expected to receive on v4 socket but got v6 (%d)",
          test_family);
  close(epfd);
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    $0x1,%edi
call   1090 <epoll_create@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11b5 <test+0x45>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe52(%rip),%rdx
mov    $0x0,%al
call   1080 <error@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    123b <test+0xcb>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    -0x18(%rbp),%rcx
call   1040 <epoll_ctl@plt>
cmp    $0x0,%rax
je     1228 <test+0xb8>
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdf6(%rip),%rdx
mov    $0x0,%al
call   1080 <error@plt>
jmp    122d <test+0xbd>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11c8 <test+0x58>
mov    -0x10(%rbp),%edi
call   1050 <send_from_v4@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1070 <receive_once@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
mov    0x2d5c(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
lea    -0x34(%rbp),%r8
call   1060 <getsockopt@plt>
cmp    $0x0,%rax
je     1299 <test+0x129>
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xda3(%rip),%rdx
mov    $0x0,%al
call   1080 <error@plt>
mov    -0x30(%rbp),%rax
mov    0x2d3c(%rip),%rcx
cmp    (%rcx),%rax
je     12c6 <test+0x156>
mov    -0x30(%rbp),%rcx
mov    $0x1,%edi
xor    %esi,%esi
lea    0xd93(%rip),%rdx
mov    $0x0,%al
call   1080 <error@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,0xc(%rsp)
mov    %esi,%ebx
mov    %rdi,%rbp
mov    $0x1,%edi
call   1090 <epoll_create@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11b4 <test+0x44>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    0xe58(%rip),%rdx
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <error@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
test   %ebx,%ebx
jle    1223 <test+0xb3>
mov    %ebx,%ebx
mov    0x2e02(%rip),%r14
lea    0x10(%rsp),%r13
xor    %r12d,%r12d
jmp    11e9 <test+0x79>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12
cmp    %r12,%rbx
je     1223 <test+0xb3>
mov    0x0(%rbp,%r12,4),%edx
mov    %edx,0x10(%rsp)
mov    (%r14),%esi
mov    %r15d,%edi
mov    %r13,%rcx
call   1040 <epoll_ctl@plt>
test   %rax,%rax
je     11e0 <test+0x70>
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdfd(%rip),%rdx
xor    %eax,%eax
call   1080 <error@plt>
jmp    11e0 <test+0x70>
mov    0xc(%rsp),%ebx
mov    %ebx,%edi
call   1050 <send_from_v4@plt>
mov    %r15d,%edi
mov    %ebx,%esi
call   1070 <receive_once@plt>
mov    0x2da9(%rip),%rcx
mov    (%rcx),%esi
mov    0x2d78(%rip),%rcx
mov    (%rcx),%edx
lea    0x18(%rsp),%rcx
lea    0x24(%rsp),%r8
mov    %eax,%edi
call   1060 <getsockopt@plt>
test   %rax,%rax
je     127c <test+0x10c>
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
lea    0xdc5(%rip),%rdx
mov    $0x1,%edi
xor    %eax,%eax
call   1080 <error@plt>
mov    0x18(%rsp),%rcx
mov    0x2d58(%rip),%rax
cmp    (%rax),%rcx
je     12a2 <test+0x132>
lea    0xdbe(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1080 <error@plt>
mov    %r15d,%edi
call   1030 <close@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.epoll_event = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@errno = global i32 0
@.str = [23 x i8] c"failed to create epoll\00"
@EPOLLIN = global i32 0
@EPOLL_CTL_ADD = global i32 0
@.str.1 = [30 x i8] c"failed to register sock epoll\00"
@SOL_SOCKET = global i32 0
@SO_DOMAIN = global i32 0
@.str.2 = [29 x i8] c"failed to read socket domain\00"
@AF_INET = global i64 0
@.str.3 = [49 x i8] c"expected to receive on v4 socket but got v6 (%d)\00"
define void @test(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.epoll_event
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%13 = call i32 @epoll_create(i32 1)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i32, i32* @errno
%18 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %16, %3
%20 = load i32, i32* @EPOLLIN
%21 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %7, i32 0, i32 1
store i32 %20, i32* %21
store i32 0, i32* %9
br label %22
22: ; preds = %47, %19
%23 = load i32, i32* %9
%24 = load i32, i32* %5
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %50
26: ; preds = %22
%27 = load i32*, i32** %4
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %7, i32 0, i32 0
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i32, i32* %8
%35 = load i32, i32* @EPOLL_CTL_ADD
%36 = load i32*, i32** %4
%37 = load i32, i32* %9
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
%40 = load i32, i32* %39
%41 = call i64 @epoll_ctl(i32 %34, i32 %35, i32 %40, %struct.epoll_event* %7)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %26
%44 = load i32, i32* @errno
%45 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %44, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %46
46: ; preds = %43, %26
br label %47
47: ; preds = %46
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 1
store i32 %49, i32* %9
br label %22
50: ; preds = %22
%51 = load i32, i32* %6
%52 = call i32 @send_from_v4(i32 %51)
%53 = load i32, i32* %8
%54 = load i32, i32* %6
%55 = call i32 @receive_once(i32 %53, i32 %54)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = load i32, i32* @SOL_SOCKET
%58 = load i32, i32* @SO_DOMAIN
%59 = call i64 @getsockopt(i32 %56, i32 %57, i32 %58, i64* %11, i32* %12)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %50
%62 = load i32, i32* @errno
%63 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %62, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %64
64: ; preds = %61, %50
%65 = load i64, i64* %11
%66 = load i64, i64* @AF_INET
%67 = icmp ne i64 %65, %66
br i1 %67, label %68, label %71
68: ; preds = %64
%69 = load i64, i64* %11
%70 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i64 %69)
br label %71
71: ; preds = %68, %64
%72 = load i32, i32* %8
%73 = call i32 @close(i32 %72)
ret void
}

LLVM IR (-O2):
%struct.epoll_event = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@errno = global i32 0
@.str = [23 x i8] c"failed to create epoll\00"
@EPOLLIN = global i32 0
@EPOLL_CTL_ADD = global i32 0
@.str.1 = [30 x i8] c"failed to register sock epoll\00"
@SOL_SOCKET = global i32 0
@SO_DOMAIN = global i32 0
@.str.2 = [29 x i8] c"failed to read socket domain\00"
@AF_INET = global i64 0
@.str.3 = [49 x i8] c"expected to receive on v4 socket but got v6 (%d)\00"
define void @test(i32* nocapture %0, i32 %1, i32 %2) {
%4 = alloca %struct.epoll_event
%5 = alloca i64
%6 = alloca i32
%7 = bitcast %struct.epoll_event* %4 to i8*
%8 = bitcast i64* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = tail call i32 @epoll_create(i32 1)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i32, i32* @errno
%14 = tail call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %12, %3
%16 = load i32, i32* @EPOLLIN
%17 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %4, i64 0, i32 1
store i32 %16, i32* %17
%18 = icmp sgt i32 %1, 0
br i1 %18, label %19, label %35
19: ; preds = %15
%20 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %4, i64 0, i32 0, i32 0
%21 = zext i32 %1 to i64
br label %22
22: ; preds = %19, %32
%23 = phi i64 [ 0, %19 ], [ %33, %32 ]
%24 = getelementptr inbounds i32, i32* %0, i64 %23
%25 = load i32, i32* %24
store i32 %25, i32* %20
%26 = load i32, i32* @EPOLL_CTL_ADD
%27 = call i64 @epoll_ctl(i32 %10, i32 %26, i32 %25, %struct.epoll_event* %4)
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %22
%30 = load i32, i32* @errno
%31 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %30, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %22, %29
%33 = add nuw nsw i64 %23, 1
%34 = icmp eq i64 %33, %21
br i1 %34, label %35, label %22
35: ; preds = %32, %15
%36 = call i32 @send_from_v4(i32 %2)
%37 = call i32 @receive_once(i32 %10, i32 %2)
%38 = load i32, i32* @SOL_SOCKET
%39 = load i32, i32* @SO_DOMAIN
%40 = call i64 @getsockopt(i32 %37, i32 %38, i32 %39, i64* %5, i32* %6)
%41 = icmp eq i64 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %35
%43 = load i32, i32* @errno
%44 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %43, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %42, %35
%46 = load i64, i64* %5
%47 = load i64, i64* @AF_INET
%48 = icmp eq i64 %46, %47
br i1 %48, label %51, label %49
49: ; preds = %45
%50 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i64 %46)
br label %51
51: ; preds = %49, %45
%52 = call i32 @close(i32 %10)
ret void
}