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

/* Variables and functions */
scalar_t__ EEXIST;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ O_WRONLY;
int S_IFIFO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dump_random_chars(char *, int);
scalar_t__ errno;
scalar_t__ mknod(char *, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ register_sigterm();
int /*<<< orphan*/ remove(char *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  char page[4096];
  fd_set fds;
  if (argc < 2) {
    printf("pipe_writer.o <pipe_path>\n");
    return -1;
  }
  if (mknod(argv[1], S_IFIFO | 0666, 0) < 0) {
    if (errno == EEXIST) {
      remove(argv[1]);
      unlink(argv[1]);
      if (mknod(argv[1], S_IFIFO | 0666, 0) < 0) {
        perror("mknod:");
        return -1;
      }
    } else {
      perror("mknod:");
      return -1;
    }
  }
  register_sigterm();
  fd = open(argv[1], O_WRONLY);
  if (fd < 0) {
    perror("open:");
  }
  FD_SET(fd, &fds);
  while (1) {
    if (select(fd + 1, NULL, &fds, NULL, NULL) < 0) {
      perror("select:");
      break;
    }
    dump_random_chars(page, sizeof(page));
    write(fd, page, sizeof(page));
    sleep(2);
  }
  close(fd);
  remove(argv[1]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    120b <main+0x3b>
lea    0xe06(%rip),%rdi
call   1080 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1396 <main+0x1c6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
or     $0x1b6,%esi
xor    %edx,%edx
call   10a0 <mknod@plt>
cmp    $0x0,%rax
jge    12c9 <main+0xf9>
mov    0x2d9e(%rip),%rax
mov    (%rax),%rax
mov    0x2d9c(%rip),%rcx
cmp    (%rcx),%rax
jne    12ac <main+0xdc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <remove@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
or     $0x1b6,%esi
xor    %edx,%edx
call   10a0 <mknod@plt>
cmp    $0x0,%rax
jge    12a7 <main+0xd7>
lea    0xd85(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1396 <main+0x1c6>
jmp    12c4 <main+0xf4>
lea    0xd68(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1396 <main+0x1c6>
jmp    12c9 <main+0xf9>
mov    $0x0,%al
call   1070 <register_sigterm@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12ff <main+0x12f>
lea    0xd28(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x14(%rbp),%edi
lea    -0x1024(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x1024(%rbp),%rdx
mov    %r8,%rsi
mov    %r8,%rcx
call   10b0 <select@plt>
cmp    $0x0,%rax
jge    1346 <main+0x176>
lea    0xcec(%rip),%rdi
call   10d0 <perror@plt>
jmp    137a <main+0x1aa>
lea    -0x1020(%rbp),%rdi
mov    $0x1000,%esi
call   1050 <dump_random_chars@plt>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <write@plt>
mov    $0x2,%edi
call   10f0 <sleep@plt>
jmp    130e <main+0x13e>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <remove@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
cmp    $0x1,%edi
jg     120d <main+0x3d>
lea    0xe28(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    0x2db5(%rip),%rbx
mov    $0x1b6,%esi
or     (%rbx),%esi
xor    %edx,%edx
call   10a0 <mknod@plt>
test   %rax,%rax
jns    127e <main+0xae>
mov    0x2da3(%rip),%rax
mov    (%rax),%rax
mov    0x2da1(%rip),%rcx
cmp    (%rcx),%rax
jne    126d <main+0x9d>
mov    0x8(%r14),%rdi
call   1040 <remove@plt>
mov    0x8(%r14),%rdi
call   1030 <unlink@plt>
mov    0x8(%r14),%rdi
mov    $0x1b6,%esi
or     (%rbx),%esi
xor    %edx,%edx
call   10a0 <mknod@plt>
test   %rax,%rax
jns    127e <main+0xae>
lea    0xd8c(%rip),%rdi
call   10d0 <perror@plt>
jmp    11f2 <main+0x22>
xor    %eax,%eax
call   1080 <register_sigterm@plt>
mov    0x8(%r14),%rdi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    12aa <main+0xda>
lea    0xd62(%rip),%rdi
call   10d0 <perror@plt>
lea    0xc(%rsp),%rbx
mov    %r12d,%edi
mov    %rbx,%rsi
call   10e0 <FD_SET@plt>
mov    %r12d,%ebp
add    $0x1,%ebp
xor    %r15d,%r15d
mov    %ebp,%edi
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
test   %rax,%rax
js     132d <main+0x15d>
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1060 <dump_random_chars@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1070 <write@plt>
mov    $0x2,%edi
call   10f0 <sleep@plt>
mov    %ebp,%edi
xor    %esi,%esi
mov    %r13,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
test   %rax,%rax
jns    12f0 <main+0x120>
lea    0xcd9(%rip),%rdi
call   10d0 <perror@plt>
mov    %r12d,%edi
call   1090 <close@plt>
mov    0x8(%r14),%rdi
call   1040 <remove@plt>
jmp    11f8 <main+0x28>

LLVM IR (-O0):
@.str = [27 x i8] c"pipe_writer.o <pipe_path>\0A\00"
@S_IFIFO = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [7 x i8] c"mknod:\00"
@O_WRONLY = global i32 0
@.str.2 = [6 x i8] c"open:\00"
@.str.3 = [8 x i8] c"select:\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %82
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @S_IFIFO
%18 = or i32 %17, 438
%19 = call i64 @mknod(i8* %16, i32 %18, i32 0)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %47
21: ; preds = %13
%22 = load i64, i64* @errno
%23 = load i64, i64* @EEXIST
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %44
25: ; preds = %21
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @remove(i8* %28)
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @unlink(i8* %32)
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = load i32, i32* @S_IFIFO
%38 = or i32 %37, 438
%39 = call i64 @mknod(i8* %36, i32 %38, i32 0)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %25
%42 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %82
43: ; preds = %25
br label %46
44: ; preds = %21
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %82
46: ; preds = %43
br label %47
47: ; preds = %46, %13
%48 = call i32 (...) @register_sigterm()
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = load i32, i32* @O_WRONLY
%53 = call i32 @open(i8* %51, i32 %52)
store i32 %53, i32* %6
%54 = load i32, i32* %6
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %47
%57 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %58
58: ; preds = %56, %47
%59 = load i32, i32* %6
%60 = call i32 @FD_SET(i32 %59, i32* %8)
br label %61
61: ; preds = %58, %68
%62 = load i32, i32* %6
%63 = add nsw i32 %62, 1
%64 = call i64 @select(i32 %63, i32* null, i32* %8, i32* null, i32* null)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %75
68: ; preds = %61
%69 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%70 = call i32 @dump_random_chars(i8* %69, i32 4096)
%71 = load i32, i32* %6
%72 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%73 = call i32 @write(i32 %71, i8* %72, i32 4096)
%74 = call i32 @sleep(i32 2)
br label %61
75: ; preds = %66
%76 = load i32, i32* %6
%77 = call i32 @close(i32 %76)
%78 = load i8**, i8*** %5
%79 = getelementptr inbounds i8*, i8** %78, i64 1
%80 = load i8*, i8** %79
%81 = call i32 @remove(i8* %80)
store i32 0, i32* %3
br label %82
82: ; preds = %75, %44, %41, %11
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
@S_IFIFO = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [7 x i8] c"mknod:\00"
@O_WRONLY = global i32 0
@.str.2 = [6 x i8] c"open:\00"
@.str.3 = [8 x i8] c"select:\00"
@str = [26 x i8] c"pipe_writer.o <pipe_path>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %59
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @S_IFIFO
%14 = or i32 %13, 438
%15 = tail call i64 @mknod(i8* %12, i32 %14, i32 0)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %35
17: ; preds = %10
%18 = load i64, i64* @errno
%19 = load i64, i64* @EEXIST
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %33
21: ; preds = %17
%22 = load i8*, i8** %11
%23 = tail call i32 @remove(i8* %22)
%24 = load i8*, i8** %11
%25 = tail call i32 @unlink(i8* %24)
%26 = load i8*, i8** %11
%27 = load i32, i32* @S_IFIFO
%28 = or i32 %27, 438
%29 = tail call i64 @mknod(i8* %26, i32 %28, i32 0)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %21
%32 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %59
33: ; preds = %17
%34 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %59
35: ; preds = %21, %10
%36 = tail call i32 (...) @register_sigterm()
%37 = load i8*, i8** %11
%38 = load i32, i32* @O_WRONLY
%39 = tail call i32 @open(i8* %37, i32 %38)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %35
%44 = call i32 @FD_SET(i32 %39, i32* %4)
%45 = add nsw i32 %39, 1
%46 = call i64 @select(i32 %45, i32* null, i32* %4, i32* null, i32* null)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %53, %43
%49 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @close(i32 %39)
%51 = load i8*, i8** %11
%52 = call i32 @remove(i8* %51)
br label %59
53: ; preds = %43, %53
%54 = call i32 @dump_random_chars(i8* %5, i32 4096)
%55 = call i32 @write(i32 %39, i8* %5, i32 4096)
%56 = call i32 @sleep(i32 2)
%57 = call i64 @select(i32 %45, i32* null, i32* %4, i32* null, i32* null)
%58 = icmp slt i64 %57, 0
br i1 %58, label %48, label %53
59: ; preds = %48, %33, %31, %8
%60 = phi i32 [ -1, %8 ], [ -1, %31 ], [ 0, %48 ], [ -1, %33 ]
ret i32 %60
}