#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 recv(int, char *, int, int /*<<< orphan*/);
int receive_once(int epfd, 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");
  }
  return ev.data.fd;
}


x86 assembly (clang, -O0):
<receive_once>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x1,%edx
mov    $0xffffffff,%ecx
call   1050 <epoll_wait@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119b <receive_once+0x4b>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe6a(%rip),%rdx
call   1060 <error@plt>
mov    -0x8(%rbp),%eax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%eax
jne    1206 <receive_once+0xb6>
mov    -0x10(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1070 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11e3 <receive_once+0x93>
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe34(%rip),%rdx
call   1060 <error@plt>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
jmp    121c <receive_once+0xcc>
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1240 <receive_once+0xf0>
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xde8(%rip),%rdx
call   1060 <error@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_once>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
lea    0x8(%rsp),%rsi
mov    $0x1,%edx
mov    $0xffffffff,%ecx
call   1050 <epoll_wait@plt>
test   %eax,%eax
jns    118a <receive_once+0x3a>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
lea    0xe80(%rip),%rdx
mov    $0x1,%edi
call   1060 <error@plt>
mov    0x2e4f(%rip),%rax
mov    0x8(%rsp),%edi
cmp    %ebx,(%rax)
jne    11e4 <receive_once+0x94>
xor    %esi,%esi
xor    %edx,%edx
call   1070 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11c2 <receive_once+0x72>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdx
mov    $0x1,%edi
call   1060 <error@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
test   %ebp,%ebp
jns    1215 <receive_once+0xc5>
jmp    11fb <receive_once+0xab>
lea    0x10(%rsp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %ebp,%ebp
jns    1215 <receive_once+0xc5>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
lea    0xe18(%rip),%rdx
mov    $0x1,%edi
call   1060 <error@plt>
mov    0x8(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
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"
define i32 @receive_once(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.epoll_event
%6 = alloca i32
%7 = alloca i32
%8 = alloca [8 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %3
%10 = call i32 @epoll_wait(i32 %9, %struct.epoll_event* %5, i32 1, i32 -1)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = call i32 @error(i32 1, i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %13, %2
%17 = load i32, i32* %4
%18 = load i32, i32* @SOCK_STREAM
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %36
20: ; preds = %16
%21 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %5, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @accept(i32 %23, i32* null, i32* null)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i32, i32* @errno
%29 = call i32 @error(i32 1, i32 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %27, %20
%31 = load i32, i32* %7
%32 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%33 = call i32 @recv(i32 %31, i8* %32, i32 8, i32 0)
store i32 %33, i32* %6
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
br label %42
36: ; preds = %16
%37 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %5, i32 0, i32 0
%38 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%41 = call i32 @recv(i32 %39, i8* %40, i32 8, i32 0)
store i32 %41, i32* %6
br label %42
42: ; preds = %36, %30
%43 = load i32, i32* %6
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* @errno
%47 = call i32 @error(i32 1, i32 %46, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %45, %42
%49 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %5, i32 0, i32 0
%50 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %49, i32 0, i32 0
%51 = load i32, i32* %50
ret i32 %51
}

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"
define i32 @receive_once(i32 %0, i32 %1) {
%3 = alloca %struct.epoll_event
%4 = alloca [8 x i8]
%5 = bitcast %struct.epoll_event* %3 to i8*
%6 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%7 = call i32 @epoll_wait(i32 %0, %struct.epoll_event* %3, i32 1, i32 -1)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = call i32 @error(i32 1, i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %9, %2
%13 = load i32, i32* @SOCK_STREAM
%14 = icmp eq i32 %13, %1
%15 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 0, i32 0
%16 = load i32, i32* %15
br i1 %14, label %17, label %26
17: ; preds = %12
%18 = call i32 @accept(i32 %16, i32* null, i32* null)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @errno
%22 = call i32 @error(i32 1, i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %20, %17
%24 = call i32 @recv(i32 %18, i8* %6, i32 8, i32 0)
%25 = call i32 @close(i32 %18)
br label %28
26: ; preds = %12
%27 = call i32 @recv(i32 %16, i8* %6, i32 8, i32 0)
br label %28
28: ; preds = %26, %23
%29 = phi i32 [ %24, %23 ], [ %27, %26 ]
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* @errno
%33 = call i32 @error(i32 1, i32 %32, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %31, %28
%35 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 0, i32 0
%36 = load i32, i32* %35
ret i32 %36
}