#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;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int epoll_wait(int, struct epoll_event *, int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void receive_on_cpu(int *rcv_fd, int len, int epfd, int cpu_id, int proto) {
  struct epoll_event ev;
  int i, fd;
  char buf[8];
  i = epoll_wait(epfd, &ev, 1, -1);
  if (i < 0) {
    error(1, errno, "epoll_wait failed");
  }
  if (proto == SOCK_STREAM) {
    fd = accept(ev.data.fd, NULL, NULL);
    if (fd < 0) {
      error(1, errno, "failed to accept");
    }
    i = recv(fd, buf, sizeof(buf), 0);
    close(fd);
  } else {
    i = recv(ev.data.fd, buf, sizeof(buf), 0);
  }
  if (i < 0) {
    error(1, errno, "failed to recv");
  }
  for (i = 0; i < len; ++i)
    if (ev.data.fd == rcv_fd[i]) {
      break;
    }
  if (i == len) {
    error(1, 0, "failed to find socket");
  }
  fprintf(stderr, "send cpu %d, receive socket %d\n", cpu_id, i);
  if (cpu_id != i) {
    error(1, 0, "cpu id/receive socket mismatch");
  }
}


x86 assembly (clang, -O0):
<receive_on_cpu>:
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    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
mov    $0xffffffff,%ecx
call   1060 <epoll_wait@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b6 <receive_on_cpu+0x56>
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe4f(%rip),%rdx
call   1070 <error@plt>
mov    -0x18(%rbp),%eax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%eax
jne    1221 <receive_on_cpu+0xc1>
mov    -0x20(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1080 <accept@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11fe <receive_on_cpu+0x9e>
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe19(%rip),%rdx
call   1070 <error@plt>
mov    -0x28(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
jmp    1237 <receive_on_cpu+0xd7>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    125b <receive_on_cpu+0xfb>
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdcd(%rip),%rdx
call   1070 <error@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    129a <receive_on_cpu+0x13a>
mov    -0x20(%rbp),%eax
mov    -0x8(%rbp),%rcx
movslq -0x24(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jne    1287 <receive_on_cpu+0x127>
jmp    129a <receive_on_cpu+0x13a>
jmp    128c <receive_on_cpu+0x12c>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1262 <receive_on_cpu+0x102>
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    12b9 <receive_on_cpu+0x159>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xd7e(%rip),%rdx
call   1070 <error@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xd79(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     12f3 <receive_on_cpu+0x193>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xd7a(%rip),%rdx
call   1070 <error@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_on_cpu>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8d,%ebp
mov    %ecx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbx
mov    %rsp,%rsi
mov    %edx,%edi
mov    $0x1,%edx
mov    $0xffffffff,%ecx
call   1060 <epoll_wait@plt>
test   %eax,%eax
jns    11aa <receive_on_cpu+0x4a>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0xe60(%rip),%rdx
mov    $0x1,%edi
call   1070 <error@plt>
mov    0x2e27(%rip),%rax
mov    (%rsp),%edi
cmp    %ebp,(%rax)
jne    1206 <receive_on_cpu+0xa6>
xor    %esi,%esi
xor    %edx,%edx
call   1080 <accept@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    11e2 <receive_on_cpu+0x82>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
lea    0xe3a(%rip),%rdx
mov    $0x1,%edi
call   1070 <error@plt>
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
mov    %r12d,%edi
call   1040 <close@plt>
test   %ebp,%ebp
jns    121d <receive_on_cpu+0xbd>
jmp    1242 <receive_on_cpu+0xe2>
lea    0x8(%rsp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %ebp,%ebp
js     1242 <receive_on_cpu+0xe2>
test   %r15d,%r15d
jle    1261 <receive_on_cpu+0x101>
mov    %r15d,%eax
mov    (%rsp),%ecx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
cmp    (%rbx,%rbp,4),%ecx
je     1263 <receive_on_cpu+0x103>
add    $0x1,%rbp
cmp    %rbp,%rax
jne    1230 <receive_on_cpu+0xd0>
mov    %eax,%ebp
jmp    1268 <receive_on_cpu+0x108>
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
lea    0xdd1(%rip),%rdx
mov    $0x1,%edi
call   1070 <error@plt>
test   %r15d,%r15d
jg     1222 <receive_on_cpu+0xc2>
xor    %ebp,%ebp
cmp    %r15d,%ebp
jne    127b <receive_on_cpu+0x11b>
lea    0xdc3(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   1070 <error@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
mov    %r14d,%edx
mov    %ebp,%ecx
call   1050 <fprintf@plt>
cmp    %r14d,%ebp
je     12ad <receive_on_cpu+0x14d>
lea    0xdc7(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   1070 <error@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.epoll_event = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@errno = global i32 0
@.str = [18 x i8] c"epoll_wait failed\00"
@SOCK_STREAM = global i32 0
@.str.1 = [17 x i8] c"failed to accept\00"
@.str.2 = [15 x i8] c"failed to recv\00"
@.str.3 = [22 x i8] c"failed to find socket\00"
@stderr = global i32 0
@.str.4 = [32 x i8] c"send cpu %d, receive socket %d\0A\00"
@.str.5 = [31 x i8] c"cpu id/receive socket mismatch\00"
define void @receive_on_cpu(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 [8 x i8]
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
%15 = load i32, i32* %8
%16 = call i32 @epoll_wait(i32 %15, %struct.epoll_event* %11, i32 1, 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 ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %19, %5
%23 = load i32, i32* %10
%24 = load i32, i32* @SOCK_STREAM
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %42
26: ; preds = %22
%27 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %11, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @accept(i32 %29, i32* null, i32* null)
store i32 %30, i32* %13
%31 = load i32, i32* %13
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = load i32, i32* @errno
%35 = call i32 @error(i32 1, i32 %34, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %33, %26
%37 = load i32, i32* %13
%38 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 0
%39 = call i32 @recv(i32 %37, i8* %38, i32 8, i32 0)
store i32 %39, i32* %12
%40 = load i32, i32* %13
%41 = call i32 @close(i32 %40)
br label %48
42: ; preds = %22
%43 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %11, i32 0, i32 0
%44 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = getelementptr inbounds [8 x i8], [8 x i8]* %14, i64 0, i64 0
%47 = call i32 @recv(i32 %45, i8* %46, i32 8, i32 0)
store i32 %47, i32* %12
br label %48
48: ; preds = %42, %36
%49 = load i32, i32* %12
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* @errno
%53 = call i32 @error(i32 1, i32 %52, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %54
54: ; preds = %51, %48
store i32 0, i32* %12
br label %55
55: ; preds = %71, %54
%56 = load i32, i32* %12
%57 = load i32, i32* %7
%58 = icmp slt i32 %56, %57
br i1 %58, label %59, label %74
59: ; preds = %55
%60 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %11, i32 0, i32 0
%61 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %60, i32 0, i32 0
%62 = load i32, i32* %61
%63 = load i32*, i32** %6
%64 = load i32, i32* %12
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %63, i64 %65
%67 = load i32, i32* %66
%68 = icmp eq i32 %62, %67
br i1 %68, label %69, label %70
69: ; preds = %59
br label %74
70: ; preds = %59
br label %71
71: ; preds = %70
%72 = load i32, i32* %12
%73 = add nsw i32 %72, 1
store i32 %73, i32* %12
br label %55
74: ; preds = %69, %55
%75 = load i32, i32* %12
%76 = load i32, i32* %7
%77 = icmp eq i32 %75, %76
br i1 %77, label %78, label %80
78: ; preds = %74
%79 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %80
80: ; preds = %78, %74
%81 = load i32, i32* @stderr
%82 = load i32, i32* %9
%83 = load i32, i32* %12
%84 = call i32 @fprintf(i32 %81, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %82, i32 %83)
%85 = load i32, i32* %9
%86 = load i32, i32* %12
%87 = icmp ne i32 %85, %86
br i1 %87, label %88, label %90
88: ; preds = %80
%89 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
br label %90
90: ; preds = %88, %80
ret void
}

LLVM IR (-O2):
%struct.epoll_event = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@errno = global i32 0
@.str = [18 x i8] c"epoll_wait failed\00"
@SOCK_STREAM = global i32 0
@.str.1 = [17 x i8] c"failed to accept\00"
@.str.2 = [15 x i8] c"failed to recv\00"
@.str.3 = [22 x i8] c"failed to find socket\00"
@stderr = global i32 0
@.str.4 = [32 x i8] c"send cpu %d, receive socket %d\0A\00"
@.str.5 = [31 x i8] c"cpu id/receive socket mismatch\00"
define void @receive_on_cpu(i32* nocapture %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca %struct.epoll_event
%7 = alloca [8 x i8]
%8 = bitcast %struct.epoll_event* %6 to i8*
%9 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%10 = call i32 @epoll_wait(i32 %2, %struct.epoll_event* %6, i32 1, i32 -1)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %5
%13 = load i32, i32* @errno
%14 = call i32 @error(i32 1, i32 %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %12, %5
%16 = load i32, i32* @SOCK_STREAM
%17 = icmp eq i32 %16, %4
%18 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %6, i64 0, i32 0, i32 0
%19 = load i32, i32* %18
br i1 %17, label %20, label %29
20: ; preds = %15
%21 = call i32 @accept(i32 %19, i32* null, i32* null)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @errno
%25 = call i32 @error(i32 1, i32 %24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %23, %20
%27 = call i32 @recv(i32 %21, i8* %9, i32 8, i32 0)
%28 = call i32 @close(i32 %21)
br label %31
29: ; preds = %15
%30 = call i32 @recv(i32 %19, i8* %9, i32 8, i32 0)
br label %31
31: ; preds = %29, %26
%32 = phi i32 [ %27, %26 ], [ %30, %29 ]
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* @errno
%36 = call i32 @error(i32 1, i32 %35, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %34, %31
%38 = icmp sgt i32 %1, 0
br i1 %38, label %39, label %53
39: ; preds = %37
%40 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %6, i64 0, i32 0, i32 0
%41 = load i32, i32* %40
%42 = zext i32 %1 to i64
br label %43
43: ; preds = %39, %48
%44 = phi i64 [ 0, %39 ], [ %49, %48 ]
%45 = getelementptr inbounds i32, i32* %0, i64 %44
%46 = load i32, i32* %45
%47 = icmp eq i32 %41, %46
br i1 %47, label %51, label %48
48: ; preds = %43
%49 = add nuw nsw i64 %44, 1
%50 = icmp eq i64 %49, %42
br i1 %50, label %56, label %43
51: ; preds = %43
%52 = trunc i64 %44 to i32
br label %53
53: ; preds = %51, %37
%54 = phi i32 [ 0, %37 ], [ %52, %51 ]
%55 = icmp eq i32 %54, %1
br i1 %55, label %56, label %59
56: ; preds = %48, %53
%57 = phi i32 [ %54, %53 ], [ %1, %48 ]
%58 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %56, %53
%60 = phi i32 [ %57, %56 ], [ %54, %53 ]
%61 = load i32, i32* @stderr
%62 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %3, i32 %60)
%63 = icmp eq i32 %60, %3
br i1 %63, label %66, label %64
64: ; preds = %59
%65 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
br label %66
66: ; preds = %64, %59
ret void
}