#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 uint64_t;
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int mill_fdwait(int, int, int, char *);
int now();
int send(int, char *, int, int /*<<< orphan*/);
int socketpair(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
               int *);
int main() {
  int fds[2];
  int rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
  assert(rc == 0);
  rc = mill_fdwait((fds[0]), (2), (-1),
                   "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                   "outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/"
                   "heads/master/tests/fdwait.c"
                   ":"
                   "48");
  assert(rc & 2);
  assert(!(rc & ~2));
  rc = mill_fdwait((fds[0]), (2), (100),
                   "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                   "outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/"
                   "heads/master/tests/fdwait.c"
                   ":"
                   "53");
  assert(rc);
  assert(rc & 2);
  assert(!(rc & ~2));
  uint64_t ms = now();
  rc = mill_fdwait((fds[0]), (1), (100),
                   "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                   "outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/"
                   "heads/master/tests/fdwait.c"
                   ":"
                   "60");
  assert(rc == 0);
  ms = now() - ms;
  assert(ms > 90 && ms < 110);
  ssize_t sz = send(fds[1], "A", 1, 0);
  assert(sz == 1);
  rc = mill_fdwait((fds[0]), (1), (-1),
                   "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                   "outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/"
                   "heads/master/tests/fdwait.c"
                   ":"
                   "68");
  assert(rc & 1);
  assert(!(rc & ~1));
  rc = mill_fdwait((fds[0]), (1 | 2), (-1),
                   "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                   "outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/"
                   "heads/master/tests/fdwait.c"
                   ":"
                   "73");
  assert(rc & 1);
  assert(rc & 2);
  assert(!(rc & ~(1 | 2)));
  close(fds[0]);
  close(fds[1]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rcx
xor    %edx,%edx
call   1060 <socketpair@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%edi
mov    $0x2,%esi
mov    $0xffffffff,%edx
lea    0xe4c(%rip),%rcx
call   1070 <mill_fdwait@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
and    $0x2,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%eax
and    $0xfffffffd,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%edi
mov    $0x2,%esi
mov    $0x64,%edx
lea    0xe9c(%rip),%rcx
call   1070 <mill_fdwait@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%edi
and    $0x2,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%eax
and    $0xfffffffd,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    $0x0,%al
call   1080 <now@plt>
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
mov    $0x64,%edx
lea    0xeda(%rip),%rcx
call   1070 <mill_fdwait@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    $0x0,%al
call   1080 <now@plt>
sub    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x5a,-0x14(%rbp)
mov    %al,-0x19(%rbp)
jle    1283 <main+0x123>
cmpl   $0x6e,-0x14(%rbp)
setl   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%edi
lea    0xf13(%rip),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
mov    $0xffffffff,%edx
lea    0xee1(%rip),%rcx
call   1070 <mill_fdwait@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
and    $0x1,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%eax
and    $0xfffffffe,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%edi
mov    $0x3,%esi
mov    $0xffffffff,%edx
lea    0xf31(%rip),%rcx
call   1070 <mill_fdwait@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
and    $0x1,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%edi
and    $0x2,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%eax
and    $0xfffffffc,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1060 <socketpair@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    0x8(%rsp),%edi
lea    0xe65(%rip),%rcx
mov    $0x2,%esi
mov    $0xffffffff,%edx
call   1070 <mill_fdwait@plt>
mov    %eax,%ebx
mov    %eax,%edi
and    $0x2,%edi
call   1050 <assert@plt>
xor    %edi,%edi
test   $0xfffffffd,%ebx
sete   %dil
call   1050 <assert@plt>
mov    0x8(%rsp),%edi
lea    0xebd(%rip),%rcx
mov    $0x2,%esi
mov    $0x64,%edx
call   1070 <mill_fdwait@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1050 <assert@plt>
mov    %ebx,%edi
and    $0x2,%edi
call   1050 <assert@plt>
xor    %edi,%edi
test   $0xfffffffd,%ebx
sete   %dil
call   1050 <assert@plt>
xor    %eax,%eax
call   1080 <now@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%edi
lea    0xf05(%rip),%rcx
mov    $0x1,%esi
mov    $0x64,%edx
call   1070 <mill_fdwait@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
xor    %eax,%eax
call   1080 <now@plt>
sub    %ebx,%eax
add    $0xffffffa5,%eax
xor    %edi,%edi
cmp    $0x13,%eax
setb   %dil
call   1050 <assert@plt>
mov    0xc(%rsp),%edi
lea    0xf53(%rip),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
xor    %edi,%edi
cmp    $0x1,%eax
sete   %dil
call   1050 <assert@plt>
mov    0x8(%rsp),%edi
lea    0xf30(%rip),%rcx
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1070 <mill_fdwait@plt>
mov    %eax,%ebx
mov    %eax,%edi
and    $0x1,%edi
call   1050 <assert@plt>
xor    %edi,%edi
cmp    $0x2,%ebx
setb   %dil
call   1050 <assert@plt>
mov    0x8(%rsp),%edi
lea    0xf8b(%rip),%rcx
mov    $0x3,%esi
mov    $0xffffffff,%edx
call   1070 <mill_fdwait@plt>
mov    %eax,%ebx
mov    %eax,%edi
and    $0x1,%edi
call   1050 <assert@plt>
mov    %ebx,%edi
and    $0x2,%edi
call   1050 <assert@plt>
xor    %edi,%edi
cmp    $0x4,%ebx
setb   %dil
call   1050 <assert@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [143 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/heads/master/tests/fdwait.c:48\00"
@.str.1 = [143 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/heads/master/tests/fdwait.c:53\00"
@.str.2 = [143 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/heads/master/tests/fdwait.c:60\00"
@.str.3 = [2 x i8] c"A\00"
@.str.4 = [143 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/heads/master/tests/fdwait.c:68\00"
@.str.5 = [143 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/heads/master/tests/fdwait.c:73\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @AF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%9 = call i32 @socketpair(i32 %6, i32 %7, i32 0, i32* %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, 0
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%15 = load i32, i32* %14
%16 = call i32 @mill_fdwait(i32 %15, i32 2, i32 -1, i8* getelementptr inbounds ([143 x i8], [143 x i8]* @.str, i64 0, i64 0))
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = and i32 %17, 2
%19 = call i32 @assert(i32 %18)
%20 = load i32, i32* %3
%21 = and i32 %20, -3
%22 = icmp ne i32 %21, 0
%23 = xor i1 %22, true
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @mill_fdwait(i32 %27, i32 2, i32 100, i8* getelementptr inbounds ([143 x i8], [143 x i8]* @.str.1, i64 0, i64 0))
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = call i32 @assert(i32 %29)
%31 = load i32, i32* %3
%32 = and i32 %31, 2
%33 = call i32 @assert(i32 %32)
%34 = load i32, i32* %3
%35 = and i32 %34, -3
%36 = icmp ne i32 %35, 0
%37 = xor i1 %36, true
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = call i32 (...) @now()
store i32 %40, i32* %4
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i32 @mill_fdwait(i32 %42, i32 1, i32 100, i8* getelementptr inbounds ([143 x i8], [143 x i8]* @.str.2, i64 0, i64 0))
store i32 %43, i32* %3
%44 = load i32, i32* %3
%45 = icmp eq i32 %44, 0
%46 = zext i1 %45 to i32
%47 = call i32 @assert(i32 %46)
%48 = call i32 (...) @now()
%49 = load i32, i32* %4
%50 = sub nsw i32 %48, %49
store i32 %50, i32* %4
%51 = load i32, i32* %4
%52 = icmp sgt i32 %51, 90
br i1 %52, label %53, label %56
53: ; preds = %0
%54 = load i32, i32* %4
%55 = icmp slt i32 %54, 110
br label %56
56: ; preds = %53, %0
%57 = phi i1 [ false, %0 ], [ %55, %53 ]
%58 = zext i1 %57 to i32
%59 = call i32 @assert(i32 %58)
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%61 = load i32, i32* %60
%62 = call i32 @send(i32 %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 0)
store i32 %62, i32* %5
%63 = load i32, i32* %5
%64 = icmp eq i32 %63, 1
%65 = zext i1 %64 to i32
%66 = call i32 @assert(i32 %65)
%67 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%68 = load i32, i32* %67
%69 = call i32 @mill_fdwait(i32 %68, i32 1, i32 -1, i8* getelementptr inbounds ([143 x i8], [143 x i8]* @.str.4, i64 0, i64 0))
store i32 %69, i32* %3
%70 = load i32, i32* %3
%71 = and i32 %70, 1
%72 = call i32 @assert(i32 %71)
%73 = load i32, i32* %3
%74 = and i32 %73, -2
%75 = icmp ne i32 %74, 0
%76 = xor i1 %75, true
%77 = zext i1 %76 to i32
%78 = call i32 @assert(i32 %77)
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%80 = load i32, i32* %79
%81 = call i32 @mill_fdwait(i32 %80, i32 3, i32 -1, i8* getelementptr inbounds ([143 x i8], [143 x i8]* @.str.5, i64 0, i64 0))
store i32 %81, i32* %3
%82 = load i32, i32* %3
%83 = and i32 %82, 1
%84 = call i32 @assert(i32 %83)
%85 = load i32, i32* %3
%86 = and i32 %85, 2
%87 = call i32 @assert(i32 %86)
%88 = load i32, i32* %3
%89 = and i32 %88, -4
%90 = icmp ne i32 %89, 0
%91 = xor i1 %90, true
%92 = zext i1 %91 to i32
%93 = call i32 @assert(i32 %92)
%94 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%95 = load i32, i32* %94
%96 = call i32 @close(i32 %95)
%97 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%98 = load i32, i32* %97
%99 = call i32 @close(i32 %98)
ret i32 0
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [143 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/heads/master/tests/fdwait.c:48\00"
@.str.1 = [143 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/heads/master/tests/fdwait.c:53\00"
@.str.2 = [143 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/heads/master/tests/fdwait.c:60\00"
@.str.3 = [2 x i8] c"A\00"
@.str.4 = [143 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/heads/master/tests/fdwait.c:68\00"
@.str.5 = [143 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/acornejo/libmill/refs/heads/master/tests/fdwait.c:73\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @socketpair(i32 %3, i32 %4, i32 0, i32* %5)
%7 = icmp eq i32 %6, 0
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = load i32, i32* %5
%11 = call i32 @mill_fdwait(i32 %10, i32 2, i32 -1, i8* getelementptr inbounds ([143 x i8], [143 x i8]* @.str, i64 0, i64 0))
%12 = and i32 %11, 2
%13 = call i32 @assert(i32 %12)
%14 = and i32 %11, -3
%15 = icmp eq i32 %14, 0
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i32, i32* %5
%19 = call i32 @mill_fdwait(i32 %18, i32 2, i32 100, i8* getelementptr inbounds ([143 x i8], [143 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @assert(i32 %19)
%21 = and i32 %19, 2
%22 = call i32 @assert(i32 %21)
%23 = and i32 %19, -3
%24 = icmp eq i32 %23, 0
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = call i32 (...) @now()
%28 = load i32, i32* %5
%29 = call i32 @mill_fdwait(i32 %28, i32 1, i32 100, i8* getelementptr inbounds ([143 x i8], [143 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i32 %29, 0
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = call i32 (...) @now()
%34 = sub i32 -91, %27
%35 = add i32 %34, %33
%36 = icmp ult i32 %35, 19
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @send(i32 %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 0)
%42 = icmp eq i32 %41, 1
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
%45 = load i32, i32* %5
%46 = call i32 @mill_fdwait(i32 %45, i32 1, i32 -1, i8* getelementptr inbounds ([143 x i8], [143 x i8]* @.str.4, i64 0, i64 0))
%47 = and i32 %46, 1
%48 = call i32 @assert(i32 %47)
%49 = icmp ult i32 %46, 2
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
%52 = load i32, i32* %5
%53 = call i32 @mill_fdwait(i32 %52, i32 3, i32 -1, i8* getelementptr inbounds ([143 x i8], [143 x i8]* @.str.5, i64 0, i64 0))
%54 = and i32 %53, 1
%55 = call i32 @assert(i32 %54)
%56 = and i32 %53, 2
%57 = call i32 @assert(i32 %56)
%58 = icmp ult i32 %53, 4
%59 = zext i1 %58 to i32
%60 = call i32 @assert(i32 %59)
%61 = load i32, i32* %5
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* %39
%64 = call i32 @close(i32 %63)
ret i32 0
}