#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_DCCP;
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, 3);
  if (i < 0)
    error(1, errno, "epoll_wait failed");
  if (proto == SOCK_STREAM || proto == SOCK_DCCP) {
    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    $0x3,%ecx
call   1050 <epoll_wait@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119b <receive_once+0x4b>
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe6a(%rip),%rdx
call   1060 <error@plt>
mov    -0x8(%rbp),%eax
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%eax
je     11bf <receive_once+0x6f>
mov    -0x8(%rbp),%eax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%eax
jne    1218 <receive_once+0xc8>
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    11f5 <receive_once+0xa5>
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe22(%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    122e <receive_once+0xde>
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    1252 <receive_once+0x102>
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdd6(%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    $0x3,%ecx
call   1050 <epoll_wait@plt>
test   %eax,%eax
jns    118a <receive_once+0x3a>
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe80(%rip),%rdx
mov    $0x1,%edi
call   1060 <error@plt>
mov    0x2e47(%rip),%rax
mov    0x8(%rsp),%edi
cmp    %ebx,(%rax)
je     11bd <receive_once+0x6d>
mov    0x2e48(%rip),%rax
cmp    %ebx,(%rax)
je     11bd <receive_once+0x6d>
lea    0x10(%rsp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %ebp,%ebp
jns    1220 <receive_once+0xd0>
jmp    1206 <receive_once+0xb6>
xor    %esi,%esi
xor    %edx,%edx
call   1070 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11e6 <receive_once+0x96>
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
lea    0xe36(%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    1220 <receive_once+0xd0>
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
lea    0xe0d(%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
@SOCK_DCCP = 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 3)
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 %24, label %20
20: ; preds = %16
%21 = load i32, i32* %4
%22 = load i32, i32* @SOCK_DCCP
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %40
24: ; preds = %20, %16
%25 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %5, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @accept(i32 %27, i32* null, i32* null)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i32, i32* @errno
%33 = call i32 @error(i32 1, i32 %32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %31, %24
%35 = load i32, i32* %7
%36 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%37 = call i32 @recv(i32 %35, i8* %36, i32 8, i32 0)
store i32 %37, i32* %6
%38 = load i32, i32* %7
%39 = call i32 @close(i32 %38)
br label %46
40: ; preds = %20
%41 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %5, i32 0, i32 0
%42 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%45 = call i32 @recv(i32 %43, i8* %44, i32 8, i32 0)
store i32 %45, i32* %6
br label %46
46: ; preds = %40, %34
%47 = load i32, i32* %6
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load i32, i32* @errno
%51 = call i32 @error(i32 1, i32 %50, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %52
52: ; preds = %49, %46
%53 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %5, i32 0, i32 0
%54 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %53, i32 0, i32 0
%55 = load i32, i32* %54
ret i32 %55
}

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
@SOCK_DCCP = 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 3)
%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 = load i32, i32* @SOCK_DCCP
%16 = icmp eq i32 %15, %1
%17 = select i1 %14, i1 true, i1 %16
%18 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 0, i32 0
%19 = load i32, i32* %18
br i1 %17, label %20, label %29
20: ; preds = %12
%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* %6, i32 8, i32 0)
%28 = call i32 @close(i32 %21)
br label %31
29: ; preds = %12
%30 = call i32 @recv(i32 %19, i8* %6, 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 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 0, i32 0
%39 = load i32, i32* %38
ret i32 %39
}