#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};
typedef int mode_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ d;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NDELAY;
int O_RDONLY;
int O_WRONLY;
int S_IFIFO;
int /*<<< orphan*/ S_ISFIFO(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ mknod(char *, int, int /*<<< orphan*/);
int open(char *, int);
scalar_t__ read(int, char *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/,
                  int /*<<< orphan*/, int /*<<< orphan*/);
int stat(char *, struct stat *);
int umask(int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  struct stat sb;
  fd_set rfds;
  char d[8192];
  int fd, fdw;
  mode_t m;
  if (argc != 2) {
    write(2, "Usage: wait-trigger pipe\n",
          sizeof("Usage: wait-trigger pipe\n") - 1);
    exit(1);
  }
  umask(m = umask(0));
  m = (0666 & ~m);
  for (;;) {
    (void)mknod(argv[1], S_IFIFO | m, 0);
    if (stat(argv[1], &sb) == -1) {
      write(2, "Can't access/create pipe trigger\n",
            sizeof("Can't access/create pipe trigger\n") - 1);
      exit(1);
    }
    if (!S_ISFIFO(sb.st_mode)) {
      write(2, "Can't access/create pipe trigger\n",
            sizeof("Can't access/create pipe trigger\n") - 1);
      exit(1);
    }
    fd = open(argv[1], O_RDONLY | O_NDELAY);
    (void)fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NDELAY);
    fdw = open(argv[1], O_WRONLY | O_NDELAY);
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    if (select(fd + 1, &rfds, 0, 0, 0) > 0) {
      while (read(fd, d, sizeof(d)) > 0)
        ;
      break;
    }
    (void)close(fdw);
    (void)close(fd);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1213 <main+0x43>
mov    $0x2,%edi
lea    0xe01(%rip),%rsi
mov    $0x19,%edx
call   1040 <write@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
xor    %edi,%edi
call   1080 <umask@plt>
mov    %eax,%edi
mov    %edi,-0x202c(%rbp)
call   1080 <umask@plt>
mov    -0x202c(%rbp),%eax
xor    $0xffffffff,%eax
and    $0x1b6,%eax
mov    %eax,-0x202c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
or     -0x202c(%rbp),%esi
xor    %edx,%edx
call   10a0 <mknod@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
jne    1293 <main+0xc3>
mov    $0x2,%edi
lea    0xd9b(%rip),%rsi
mov    $0x21,%edx
call   1040 <write@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <S_ISFIFO@plt>
cmp    $0x0,%eax
jne    12c4 <main+0xf4>
mov    $0x2,%edi
lea    0xd6a(%rip),%rsi
mov    $0x21,%edx
call   1040 <write@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d15(%rip),%rax
mov    (%rax),%esi
mov    0x2cf4(%rip),%rax
or     (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x2024(%rbp)
mov    -0x2024(%rbp),%eax
mov    %eax,-0x2034(%rbp)
mov    0x2cdc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2030(%rbp)
mov    -0x2024(%rbp),%edi
mov    0x2cb7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0x2034(%rbp),%edi
mov    -0x2030(%rbp),%esi
mov    %eax,%edx
mov    0x2ca1(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2c6b(%rip),%rax
mov    (%rax),%esi
mov    0x2c82(%rip),%rax
or     (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x2028(%rbp)
lea    -0x1c(%rbp),%rdi
call   10b0 <FD_ZERO@plt>
mov    -0x2024(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   10f0 <FD_SET@plt>
mov    -0x2024(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %r8d,%r8d
mov    %r8d,%edx
mov    %r8d,%ecx
call   10c0 <select@plt>
cmp    $0x0,%rax
jle    13c8 <main+0x1f8>
jmp    139d <main+0x1cd>
mov    -0x2024(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x2000,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jle    13c3 <main+0x1f3>
jmp    139d <main+0x1cd>
jmp    13e3 <main+0x213>
mov    -0x2028(%rbp),%edi
call   1060 <close@plt>
mov    -0x2024(%rbp),%edi
call   1060 <close@plt>
jmp    123b <main+0x6b>
xor    %edi,%edi
call   10e0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2018,%rsp
cmp    $0x2,%edi
jne    1336 <main+0x166>
mov    %rsi,%r12
xor    %edi,%edi
call   1080 <umask@plt>
mov    %eax,%r13d
not    %r13d
mov    %eax,%edi
call   1080 <umask@plt>
and    $0x1b6,%r13d
mov    0x8(%r12),%rdi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
or     %r13d,%esi
xor    %edx,%edx
call   10a0 <mknod@plt>
mov    0x8(%r12),%rdi
lea    0x8(%rsp),%rsi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
je     1323 <main+0x153>
mov    0x2d91(%rip),%r15
lea    0xc(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x8(%rsp),%edi
call   1050 <S_ISFIFO@plt>
test   %eax,%eax
je     1323 <main+0x153>
mov    0x8(%r12),%rdi
mov    (%r15),%esi
mov    0x2d78(%rip),%rax
or     (%rax),%esi
call   10d0 <open@plt>
mov    %eax,%ebp
mov    0x2d58(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
or     (%r15),%eax
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    0x8(%r12),%rdi
mov    (%r15),%esi
mov    0x2cff(%rip),%rax
or     (%rax),%esi
call   10d0 <open@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   10b0 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   10f0 <FD_SET@plt>
mov    %ebp,%edi
add    $0x1,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10c0 <select@plt>
test   %rax,%rax
jg     1356 <main+0x186>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x8(%r12),%rdi
mov    0x2cc1(%rip),%rax
mov    (%rax),%esi
or     %r13d,%esi
xor    %edx,%edx
call   10a0 <mknod@plt>
mov    0x8(%r12),%rdi
lea    0x8(%rsp),%rsi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
jne    1250 <main+0x80>
lea    0xcf0(%rip),%rsi
mov    $0x2,%edi
mov    $0x21,%edx
jmp    1347 <main+0x177>
lea    0xcc3(%rip),%rsi
mov    $0x2,%edi
mov    $0x19,%edx
call   1040 <write@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0x10(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x2000,%edx
call   1070 <read@plt>
test   %rax,%rax
jg     1360 <main+0x190>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [26 x i8] c"Usage: wait-trigger pipe\0A\00"
@S_IFIFO = global i32 0
@.str.1 = [34 x i8] c"Can't access/create pipe trigger\0A\00"
@O_RDONLY = global i32 0
@O_NDELAY = global i32 0
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_WRONLY = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca [8192 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @write(i32 2, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 25)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = call i32 @umask(i32 0)
store i32 %18, i32* %11
%19 = call i32 @umask(i32 %18)
%20 = load i32, i32* %11
%21 = xor i32 %20, -1
%22 = and i32 438, %21
store i32 %22, i32* %11
br label %23
23: ; preds = %85, %17
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = load i32, i32* @S_IFIFO
%28 = load i32, i32* %11
%29 = or i32 %27, %28
%30 = call i32 @mknod(i8* %26, i32 %29, i32 0)
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @stat(i8* %33, %struct.stat* %6)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %23
%37 = call i32 @write(i32 2, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 33)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %23
%40 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @S_ISFIFO(i32 %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %39
%45 = call i32 @write(i32 2, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 33)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %39
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = load i32, i32* @O_RDONLY
%52 = load i32, i32* @O_NDELAY
%53 = or i32 %51, %52
%54 = call i32 @open(i8* %50, i32 %53)
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = load i32, i32* @F_SETFL
%57 = load i32, i32* %9
%58 = load i32, i32* @F_GETFL
%59 = call i32 @fcntl(i32 %57, i32 %58, i32 0)
%60 = load i32, i32* @O_NDELAY
%61 = or i32 %59, %60
%62 = call i32 @fcntl(i32 %55, i32 %56, i32 %61)
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 1
%65 = load i8*, i8** %64
%66 = load i32, i32* @O_WRONLY
%67 = load i32, i32* @O_NDELAY
%68 = or i32 %66, %67
%69 = call i32 @open(i8* %65, i32 %68)
store i32 %69, i32* %10
%70 = call i32 @FD_ZERO(i32* %7)
%71 = load i32, i32* %9
%72 = call i32 @FD_SET(i32 %71, i32* %7)
%73 = load i32, i32* %9
%74 = add nsw i32 %73, 1
%75 = call i64 @select(i32 %74, i32* %7, i32 0, i32 0, i32 0)
%76 = icmp sgt i64 %75, 0
br i1 %76, label %77, label %85
77: ; preds = %47
br label %78
78: ; preds = %83, %77
%79 = load i32, i32* %9
%80 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%81 = call i64 @read(i32 %79, i8* %80, i32 8192)
%82 = icmp sgt i64 %81, 0
br i1 %82, label %83, label %84
83: ; preds = %78
br label %78
84: ; preds = %78
br label %90
85: ; preds = %47
%86 = load i32, i32* %10
%87 = call i32 @close(i32 %86)
%88 = load i32, i32* %9
%89 = call i32 @close(i32 %88)
br label %23
90: ; preds = %84
%91 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [26 x i8] c"Usage: wait-trigger pipe\0A\00"
@S_IFIFO = global i32 0
@.str.1 = [34 x i8] c"Can't access/create pipe trigger\0A\00"
@O_RDONLY = global i32 0
@O_NDELAY = global i32 0
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_WRONLY = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca [8192 x i8]
%6 = bitcast %struct.stat* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %0, 2
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = tail call i32 @write(i32 2, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 25)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = tail call i32 @umask(i32 0)
%15 = tail call i32 @umask(i32 %14)
%16 = and i32 %14, 438
%17 = xor i32 %16, 438
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @S_IFIFO
%21 = or i32 %20, %17
%22 = call i32 @mknod(i8* %19, i32 %21, i32 0)
%23 = load i8*, i8** %18
%24 = call i32 @stat(i8* %23, %struct.stat* %3)
%25 = icmp eq i32 %24, -1
br i1 %25, label %28, label %26
26: ; preds = %13
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %31
28: ; preds = %63, %13
%29 = call i32 @write(i32 2, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 33)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %26, %63
%32 = load i32, i32* %27
%33 = call i32 @S_ISFIFO(i32 %32)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @write(i32 2, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 33)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %31
%39 = load i8*, i8** %18
%40 = load i32, i32* @O_RDONLY
%41 = load i32, i32* @O_NDELAY
%42 = or i32 %41, %40
%43 = call i32 @open(i8* %39, i32 %42)
%44 = load i32, i32* @F_SETFL
%45 = load i32, i32* @F_GETFL
%46 = call i32 @fcntl(i32 %43, i32 %45, i32 0)
%47 = load i32, i32* @O_NDELAY
%48 = or i32 %47, %46
%49 = call i32 @fcntl(i32 %43, i32 %44, i32 %48)
%50 = load i8*, i8** %18
%51 = load i32, i32* @O_WRONLY
%52 = load i32, i32* @O_NDELAY
%53 = or i32 %52, %51
%54 = call i32 @open(i8* %50, i32 %53)
%55 = call i32 @FD_ZERO(i32* %4)
%56 = call i32 @FD_SET(i32 %43, i32* %4)
%57 = add nsw i32 %43, 1
%58 = call i64 @select(i32 %57, i32* %4, i32 0, i32 0, i32 0)
%59 = icmp sgt i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %38, %60
%61 = call i64 @read(i32 %43, i8* %8, i32 8192)
%62 = icmp sgt i64 %61, 0
br i1 %62, label %60, label %73
63: ; preds = %38
%64 = call i32 @close(i32 %54)
%65 = call i32 @close(i32 %43)
%66 = load i8*, i8** %18
%67 = load i32, i32* @S_IFIFO
%68 = or i32 %67, %17
%69 = call i32 @mknod(i8* %66, i32 %68, i32 0)
%70 = load i8*, i8** %18
%71 = call i32 @stat(i8* %70, %struct.stat* %3)
%72 = icmp eq i32 %71, -1
br i1 %72, label %28, label %31
73: ; preds = %60
%74 = call i32 @exit(i32 0)
unreachable
}