#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ manage_timeout();
int open_pipe(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ process_buffer(char *, int);
int read(int, char *, int);
int reopen_pipe(char *, int, int /*<<< orphan*/ *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int fifod;
  int exit_program;
  int num_reads;
  char buf[16];
  fd_set rset;
  int num_fd;
  struct timeval tout;
  tout.tv_sec = 1;
  tout.tv_usec = 0;
  fifod = open_pipe("my_pipe", &rset);
  exit_program = 0;
  while (exit_program == 0) {
    num_fd = select(fifod + 1, &rset, NULL, NULL, &tout);
    if (num_fd == 0) {
      tout.tv_sec = 1;
      tout.tv_usec = 0;
      fifod = reopen_pipe("my_pipe", fifod, &rset);
      manage_timeout();
    }
    if (num_fd > 0) {
      if (FD_ISSET(fifod, &rset)) {
        (void)fprintf(stdout, "...reading from descriptor %d\n", fifod);
        num_reads = read(fifod, buf, sizeof(buf));
        if (num_reads > 0) {
          process_buffer(buf, num_reads);
        } else {
          if (num_reads == 0)
            (void)fprintf(stdout, "...EOF condition, close and reopen FIFO\n");
          else
            (void)fprintf(stdout, "...ERROR, close and reopen the FIFO\n");
          fifod = reopen_pipe("my_pipe", fifod, &rset);
        }
      } else {
        (void)fprintf(stderr, "...unknown file descriptor (?)\n");
      }
    }
  }
  (void)close(fifod);
  (void)fprintf(stdout, "...pipe closed, exit program\n");
  return 0;
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
lea    0xe44(%rip),%rdi
lea    -0x34(%rbp),%rsi
call   1090 <open_pipe@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    130e <main+0x17e>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x48(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jne    122c <main+0x9c>
movl   $0x1,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x14(%rbp),%esi
lea    0xde7(%rip),%rdi
lea    -0x34(%rbp),%rdx
call   1050 <reopen_pipe@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   10b0 <manage_timeout@plt>
cmpl   $0x0,-0x38(%rbp)
jle    1309 <main+0x179>
mov    -0x14(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12ed <main+0x15d>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xda9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1295 <main+0x105>
lea    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   10a0 <process_buffer@plt>
jmp    12e8 <main+0x158>
cmpl   $0x0,-0x1c(%rbp)
jne    12bb <main+0x12b>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
lea    0xd78(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    12d2 <main+0x142>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
lea    0xd85(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%esi
lea    0xd24(%rip),%rdi
lea    -0x34(%rbp),%rdx
call   1050 <reopen_pipe@plt>
mov    %eax,-0x14(%rbp)
jmp    1304 <main+0x174>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
lea    0xd78(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1309 <main+0x179>
jmp    11cf <main+0x3f>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2cbb(%rip),%rax
mov    (%rax),%edi
lea    0xd6f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movl   $0x1,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xe5a(%rip),%rdi
lea    0x1c(%rsp),%rbx
mov    %rbx,%rsi
call   1080 <open_pipe@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
mov    0x2e17(%rip),%r13
lea    0x20(%rsp),%r14
jmp    11fb <main+0x7b>
nopl   0x0(%rax,%rax,1)
movl   $0x1,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xe18(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1040 <reopen_pipe@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10a0 <manage_timeout@plt>
lea    0x1(%rbp),%r12d
jmp    1227 <main+0xa7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1070 <select@plt>
test   %eax,%eax
je     11d0 <main+0x50>
jle    1227 <main+0xa7>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     1210 <main+0x90>
mov    0x0(%r13),%edi
lea    0xdaf(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1290 <main+0x110>
mov    %r14,%rdi
mov    %eax,%esi
call   1090 <process_buffer@plt>
jmp    1227 <main+0xa7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%edi
lea    0xdb5(%rip),%rsi
lea    0xd85(%rip),%rax
cmove  %rax,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xd4c(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1040 <reopen_pipe@plt>
mov    %eax,%ebp
jmp    11fb <main+0x7b>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [8 x i8] c"my_pipe\00"
@stdout = global i32 0
@.str.1 = [31 x i8] c"...reading from descriptor %d\0A\00"
@.str.2 = [41 x i8] c"...EOF condition, close and reopen FIFO\0A\00"
@.str.3 = [37 x i8] c"...ERROR, close and reopen the FIFO\0A\00"
@stderr = global i32 0
@.str.4 = [32 x i8] c"...unknown file descriptor (?)\0A\00"
@.str.5 = [30 x i8] c"...pipe closed, exit program\0A\00"
@EXIT_SUCCESS = 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
%9 = alloca [16 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.timeval
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 1, i32* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 0, i64* %14
%15 = call i32 @open_pipe(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32* %10)
store i32 %15, i32* %6
store i32 0, i32* %7
br label %16
16: ; preds = %68, %2
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %69
19: ; preds = %16
%20 = load i32, i32* %6
%21 = add nsw i32 %20, 1
%22 = call i32 @select(i32 %21, i32* %10, i32* null, i32* null, %struct.timeval* %12)
store i32 %22, i32* %11
%23 = load i32, i32* %11
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %19
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i64 0, i64* %27
%28 = load i32, i32* %6
%29 = call i32 @reopen_pipe(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %28, i32* %10)
store i32 %29, i32* %6
%30 = call i32 (...) @manage_timeout()
br label %31
31: ; preds = %25, %19
%32 = load i32, i32* %11
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %68
34: ; preds = %31
%35 = load i32, i32* %6
%36 = call i64 @FD_ISSET(i32 %35, i32* %10)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %64
38: ; preds = %34
%39 = load i32, i32* @stdout
%40 = load i32, i32* %6
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %6
%43 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%44 = call i32 @read(i32 %42, i8* %43, i32 16)
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %38
%48 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%49 = load i32, i32* %8
%50 = call i32 @process_buffer(i8* %48, i32 %49)
br label %63
51: ; preds = %38
%52 = load i32, i32* %8
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i32, i32* @stdout
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %60
57: ; preds = %51
%58 = load i32, i32* @stdout
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
br label %60
60: ; preds = %57, %54
%61 = load i32, i32* %6
%62 = call i32 @reopen_pipe(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %61, i32* %10)
store i32 %62, i32* %6
br label %63
63: ; preds = %60, %47
br label %67
64: ; preds = %34
%65 = load i32, i32* @stderr
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %67
67: ; preds = %64, %63
br label %68
68: ; preds = %67, %31
br label %16
69: ; preds = %16
%70 = load i32, i32* %6
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* @stdout
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [8 x i8] c"my_pipe\00"
@stdout = global i32 0
@.str.1 = [31 x i8] c"...reading from descriptor %d\0A\00"
@.str.2 = [41 x i8] c"...EOF condition, close and reopen FIFO\0A\00"
@.str.3 = [37 x i8] c"...ERROR, close and reopen the FIFO\0A\00"
@stderr = global i32 0
@.str.4 = [32 x i8] c"...unknown file descriptor (?)\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [16 x i8]
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = bitcast %struct.timeval* %5 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 1, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %10
%11 = call i32 @open_pipe(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32* %4)
br label %12
12: ; preds = %39, %2
%13 = phi i32 [ %11, %2 ], [ %40, %39 ]
%14 = add nsw i32 %13, 1
br label %15
15: ; preds = %44, %12
%16 = call i32 @select(i32 %14, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
store i32 1, i32* %9
store i64 0, i64* %10
%19 = call i32 @reopen_pipe(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %13, i32* %4)
%20 = call i32 (...) @manage_timeout()
br label %39
21: ; preds = %15
%22 = icmp sgt i32 %16, 0
br i1 %22, label %23, label %44
23: ; preds = %21
%24 = call i64 @FD_ISSET(i32 %13, i32* %4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %41, label %26
26: ; preds = %23
%27 = load i32, i32* @stdout
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%29 = call i32 @read(i32 %13, i8* %6, i32 16)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @process_buffer(i8* %6, i32 %29)
br label %44
33: ; preds = %26
%34 = icmp eq i32 %29, 0
%35 = load i32, i32* @stdout
%36 = select i1 %34, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0)
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* %36)
%38 = call i32 @reopen_pipe(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %13, i32* %4)
br label %39
39: ; preds = %33, %18
%40 = phi i32 [ %19, %18 ], [ %38, %33 ]
br label %12
41: ; preds = %23
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %41, %31, %21
br label %15
}