#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 /*<<< orphan*/ fd_set;

/* Variables and functions */
int ENOTCONN;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ enable_all_events(int);
int open_named_pipe(char *);
int /*<<< orphan*/ perror(char *);
int read_event(int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int sfd;
  fd_set rfds;
  int ret;
  sfd = open_named_pipe("/tmp/openl2tpd.evt");
  if (sfd < 0)
    goto error;
  enable_all_events(sfd);
  FD_ZERO(&rfds);
  FD_SET(sfd, &rfds);
  while (1) {
    ret = select(sfd + 1, &rfds, NULL, NULL, NULL);
    if (ret == -1) {
      perror("select");
    } else {
      ret = read_event(sfd);
      if (ret == -ENOTCONN)
        goto out;
    }
  }
out:
  close(sfd);
  return EXIT_SUCCESS;
error:
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rdi
call   1070 <open_named_pipe@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b4 <main+0x34>
jmp    1252 <main+0xd2>
mov    -0x14(%rbp),%edi
call   1080 <enable_all_events@plt>
lea    -0x18(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1060 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1209 <main+0x89>
lea    0xe14(%rip),%rdi
call   1090 <perror@plt>
jmp    1234 <main+0xb4>
mov    -0x14(%rbp),%edi
call   1040 <read_event@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
xor    %ecx,%ecx
mov    0x2dc8(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    122f <main+0xaf>
jmp    1239 <main+0xb9>
jmp    1234 <main+0xb4>
jmp    11d1 <main+0x51>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    125e <main+0xde>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xe6d(%rip),%rdi
call   1070 <open_named_pipe@plt>
test   %eax,%eax
js     1213 <main+0x93>
mov    %eax,%ebx
mov    %eax,%edi
call   1080 <enable_all_events@plt>
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   10a0 <FD_SET@plt>
mov    %ebx,%ebp
add    $0x1,%ebp
lea    0xe4b(%rip),%r15
mov    0x2e19(%rip),%r12
jmp    11e0 <main+0x60>
mov    %r15,%rdi
call   1090 <perror@plt>
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <select@plt>
cmp    $0xffffffff,%eax
je     11d1 <main+0x51>
mov    %ebx,%edi
call   1040 <read_event@plt>
add    (%r12),%eax
jne    11e0 <main+0x60>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x2dbf(%rip),%rax
jmp    121a <main+0x9a>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"/tmp/openl2tpd.evt\00"
@.str.1 = [7 x i8] c"select\00"
@ENOTCONN = global i32 0
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 @open_named_pipe(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
br label %41
13: ; preds = %2
%14 = load i32, i32* %6
%15 = call i32 @enable_all_events(i32 %14)
%16 = call i32 @FD_ZERO(i32* %7)
%17 = load i32, i32* %6
%18 = call i32 @FD_SET(i32 %17, i32* %7)
br label %19
19: ; preds = %13, %36
%20 = load i32, i32* %6
%21 = add nsw i32 %20, 1
%22 = call i32 @select(i32 %21, i32* %7, i32* null, i32* null, i32* null)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %36
27: ; preds = %19
%28 = load i32, i32* %6
%29 = call i32 @read_event(i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = load i32, i32* @ENOTCONN
%32 = sub nsw i32 0, %31
%33 = icmp eq i32 %30, %32
br i1 %33, label %34, label %35
34: ; preds = %27
br label %37
35: ; preds = %27
br label %36
36: ; preds = %35, %25
br label %19
37: ; preds = %34
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* @EXIT_SUCCESS
store i32 %40, i32* %3
br label %43
41: ; preds = %12
%42 = load i32, i32* @EXIT_FAILURE
store i32 %42, i32* %3
br label %43
43: ; preds = %41, %37
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str = [19 x i8] c"/tmp/openl2tpd.evt\00"
@.str.1 = [7 x i8] c"select\00"
@ENOTCONN = global i32 0
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @open_named_pipe(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%6 = icmp slt i32 %5, 0
br i1 %6, label %25, label %7
7: ; preds = %2
%8 = tail call i32 @enable_all_events(i32 %5)
%9 = call i32 @FD_ZERO(i32* %3)
%10 = call i32 @FD_SET(i32 %5, i32* %3)
%11 = add nuw nsw i32 %5, 1
br label %12
12: ; preds = %22, %7
%13 = call i32 @select(i32 %11, i32* %3, i32* null, i32* null, i32* null)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %22
17: ; preds = %12
%18 = call i32 @read_event(i32 %5)
%19 = load i32, i32* @ENOTCONN
%20 = sub nsw i32 0, %19
%21 = icmp eq i32 %18, %20
br i1 %21, label %23, label %22
22: ; preds = %17, %15
br label %12
23: ; preds = %17
%24 = call i32 @close(i32 %5)
br label %25
25: ; preds = %2, %23
%26 = phi i32* [ @EXIT_SUCCESS, %23 ], [ @EXIT_FAILURE, %2 ]
%27 = load i32, i32* %26
ret i32 %27
}