#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 */
struct TYPE_2__ {
  int fd;
};
struct epoll_event {
  TYPE_1__ data;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
int /*<<< orphan*/ EPOLLIN;
int /*<<< orphan*/ EPOLL_CTL_ADD;
int /*<<< orphan*/ close(int);
int connect_and_send(int, 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 *);
int receive_once(int, int);
void test(int *rcv_fds, int count, int family, int proto, int fd) {
  struct epoll_event ev;
  int epfd, i, send_fd, recv_fd;
  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_fd = connect_and_send(family, proto);
  recv_fd = receive_once(epfd, proto);
  if (recv_fd != fd)
    error(1, 0, "received on an unexpected socket");
  close(send_fd);
  close(epfd);
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    $0x1,%edi
call   1070 <epoll_create@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11aa <test+0x4a>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe5b(%rip),%rdx
call   1060 <error@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    122e <test+0xce>
mov    -0x8(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    -0x20(%rbp),%rcx
call   1040 <epoll_ctl@plt>
cmp    $0x0,%rax
je     121b <test+0xbb>
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe01(%rip),%rdx
call   1060 <error@plt>
jmp    1220 <test+0xc0>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11bd <test+0x5d>
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1080 <connect_and_send@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <receive_once@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     1269 <test+0x109>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xdd1(%rip),%rdx
call   1060 <error@plt>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,0xc(%rsp)
mov    %ecx,0x8(%rsp)
mov    %edx,0x4(%rsp)
mov    %esi,%r14d
mov    %rdi,%rbp
mov    $0x1,%edi
call   1070 <epoll_create@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ab <test+0x4b>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdx
mov    $0x1,%edi
call   1060 <error@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
test   %r14d,%r14d
jle    1220 <test+0xc0>
mov    %r14d,%r15d
xor    %r12d,%r12d
mov    0x2e16(%rip),%r14
lea    0x10(%rsp),%r13
jmp    11e9 <test+0x89>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12
cmp    %r12,%r15
je     1220 <test+0xc0>
mov    0x0(%rbp,%r12,4),%edx
mov    %edx,0x10(%rsp)
mov    (%r14),%esi
mov    %ebx,%edi
mov    %r13,%rcx
call   1040 <epoll_ctl@plt>
test   %rax,%rax
je     11e0 <test+0x80>
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdfe(%rip),%rdx
call   1060 <error@plt>
jmp    11e0 <test+0x80>
mov    0x4(%rsp),%edi
mov    0x8(%rsp),%ebp
mov    %ebp,%esi
call   1080 <connect_and_send@plt>
mov    %eax,%r14d
mov    %ebx,%edi
mov    %ebp,%esi
call   1050 <receive_once@plt>
cmp    0xc(%rsp),%eax
je     1254 <test+0xf4>
lea    0xded(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   1060 <error@plt>
mov    %r14d,%edi
call   1030 <close@plt>
mov    %ebx,%edi
call   1030 <close@plt>
add    $0x18,%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"
@.str.2 = [33 x i8] c"received on an unexpected socket\00"
define void @test(i32* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.epoll_event
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%16 = call i32 @epoll_create(i32 1)
store i32 %16, i32* %12
%17 = load i32, i32* %12
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %5
%20 = load i32, i32* @errno
%21 = call i32 @error(i32 1, i32 %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %19, %5
%23 = load i32, i32* @EPOLLIN
%24 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %11, i32 0, i32 1
store i32 %23, i32* %24
store i32 0, i32* %13
br label %25
25: ; preds = %50, %22
%26 = load i32, i32* %13
%27 = load i32, i32* %7
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %53
29: ; preds = %25
%30 = load i32*, i32** %6
%31 = load i32, i32* %13
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %11, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i32, i32* %12
%38 = load i32, i32* @EPOLL_CTL_ADD
%39 = load i32*, i32** %6
%40 = load i32, i32* %13
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32, i32* %42
%44 = call i64 @epoll_ctl(i32 %37, i32 %38, i32 %43, %struct.epoll_event* %11)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %29
%47 = load i32, i32* @errno
%48 = call i32 @error(i32 1, i32 %47, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %49
49: ; preds = %46, %29
br label %50
50: ; preds = %49
%51 = load i32, i32* %13
%52 = add nsw i32 %51, 1
store i32 %52, i32* %13
br label %25
53: ; preds = %25
%54 = load i32, i32* %8
%55 = load i32, i32* %9
%56 = call i32 @connect_and_send(i32 %54, i32 %55)
store i32 %56, i32* %14
%57 = load i32, i32* %12
%58 = load i32, i32* %9
%59 = call i32 @receive_once(i32 %57, i32 %58)
store i32 %59, i32* %15
%60 = load i32, i32* %15
%61 = load i32, i32* %10
%62 = icmp ne i32 %60, %61
br i1 %62, label %63, label %65
63: ; preds = %53
%64 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %65
65: ; preds = %63, %53
%66 = load i32, i32* %14
%67 = call i32 @close(i32 %66)
%68 = load i32, i32* %12
%69 = call i32 @close(i32 %68)
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"
@.str.2 = [33 x i8] c"received on an unexpected socket\00"
define void @test(i32* nocapture %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca %struct.epoll_event
%7 = bitcast %struct.epoll_event* %6 to i8*
%8 = tail call i32 @epoll_create(i32 1)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = load i32, i32* @errno
%12 = tail call i32 @error(i32 1, i32 %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %10, %5
%14 = load i32, i32* @EPOLLIN
%15 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %6, i64 0, i32 1
store i32 %14, i32* %15
%16 = icmp sgt i32 %1, 0
br i1 %16, label %17, label %33
17: ; preds = %13
%18 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %6, i64 0, i32 0, i32 0
%19 = zext i32 %1 to i64
br label %20
20: ; preds = %17, %30
%21 = phi i64 [ 0, %17 ], [ %31, %30 ]
%22 = getelementptr inbounds i32, i32* %0, i64 %21
%23 = load i32, i32* %22
store i32 %23, i32* %18
%24 = load i32, i32* @EPOLL_CTL_ADD
%25 = call i64 @epoll_ctl(i32 %8, i32 %24, i32 %23, %struct.epoll_event* %6)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %20
%28 = load i32, i32* @errno
%29 = call i32 @error(i32 1, i32 %28, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %20, %27
%31 = add nuw nsw i64 %21, 1
%32 = icmp eq i64 %31, %19
br i1 %32, label %33, label %20
33: ; preds = %30, %13
%34 = call i32 @connect_and_send(i32 %2, i32 %3)
%35 = call i32 @receive_once(i32 %8, i32 %3)
%36 = icmp eq i32 %35, %4
br i1 %36, label %39, label %37
37: ; preds = %33
%38 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
%40 = call i32 @close(i32 %34)
%41 = call i32 @close(i32 %8)
ret void
}