#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 */

/* Type definitions */
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ ce_listen(int, int);
int ce_set_nonblock(int);
int /*<<< orphan*/ ce_unlisten(int, int);
scalar_t__ ce_wait();
int /*<<< orphan*/ printf(char *);
int read(int, void *, size_t);
ssize_t ce_read(int fd, void *buf, size_t count) {
  ssize_t ret;
  if (ce_set_nonblock(fd) == -1) {
    printf("ERROR: Failed to set read fd nonblock\n");
    return -1;
  }
  ce_listen(fd, 1);
  if (ce_wait() != 0) {
    return -1;
  }
  ret = read(fd, buf, count);
  ce_unlisten(fd, 1);
  return ret;
}


x86 assembly (clang, -O0):
<ce_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <ce_set_nonblock@plt>
cmp    $0xffffffff,%eax
jne    119c <ce_read+0x3c>
lea    0xe75(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <ce_read+0x8c>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1060 <ce_listen@plt>
mov    $0x0,%al
call   1070 <ce_wait@plt>
cmp    $0x0,%rax
je     11c6 <ce_read+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <ce_read+0x8c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1080 <ce_unlisten@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ce_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
call   1040 <ce_set_nonblock@plt>
cmp    $0xffffffff,%eax
je     11b0 <ce_read+0x50>
mov    %ebx,%edi
mov    $0x1,%esi
call   1060 <ce_listen@plt>
xor    %eax,%eax
call   1070 <ce_wait@plt>
test   %rax,%rax
jne    11bc <ce_read+0x5c>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <read@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    $0x1,%esi
call   1080 <ce_unlisten@plt>
mov    %ebp,%eax
jmp    11c1 <ce_read+0x61>
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [39 x i8] c"ERROR: Failed to set read fd nonblock\0A\00"
define i32 @ce_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i32, i32* %5
%10 = call i32 @ce_set_nonblock(i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %28
14: ; preds = %3
%15 = load i32, i32* %5
%16 = call i32 @ce_listen(i32 %15, i32 1)
%17 = call i64 (...) @ce_wait()
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 -1, i32* %4
br label %28
20: ; preds = %14
%21 = load i32, i32* %5
%22 = load i8*, i8** %6
%23 = load i64, i64* %7
%24 = call i32 @read(i32 %21, i8* %22, i64 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %5
%26 = call i32 @ce_unlisten(i32 %25, i32 1)
%27 = load i32, i32* %8
store i32 %27, i32* %4
br label %28
28: ; preds = %20, %19, %12
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
@str = [38 x i8] c"ERROR: Failed to set read fd nonblock\00"
define i32 @ce_read(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 @ce_set_nonblock(i32 %0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %15
8: ; preds = %3
%9 = tail call i32 @ce_listen(i32 %0, i32 1)
%10 = tail call i64 (...) @ce_wait()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = tail call i32 @read(i32 %0, i8* %1, i64 %2)
%14 = tail call i32 @ce_unlisten(i32 %0, i32 1)
br label %15
15: ; preds = %8, %12, %6
%16 = phi i32 [ -1, %6 ], [ %13, %12 ], [ -1, %8 ]
ret i32 %16
}