#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
scalar_t__ fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, size_t const);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  const size_t MAX_BUFFER_SIZE = 8192;
  char buf[MAX_BUFFER_SIZE];
  int fd = open("/dev/reverse", O_RDWR);
  const int MAX_RW = 75;
  int reads = 0;
  int writes = 0;
  if (fd != -1) {
    if (fork()) {
      while (writes++ < MAX_RW) {
        write(fd, argv[1], strlen(argv[1]));
      }
    } else {
      while (reads++ < MAX_RW) {
        if (read(fd, buf, MAX_BUFFER_SIZE) > 0) {
          printf("Read(%i): %s\n", reads, buf);
        }
      }
    }
  } else {
    printf("Failed to open /dev/reverse (try sudo?)\n");
  }
  return 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)
movq   $0x2000,-0x18(%rbp)
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
movl   $0x4b,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
je     128a <main+0x12a>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
je     122c <main+0xcc>
jmp    11d5 <main+0x75>
mov    -0x30(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x30(%rbp)
cmp    $0x4b,%eax
jge    1227 <main+0xc7>
mov    -0x24(%rbp),%eax
mov    %eax,-0x203c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x2038(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x203c(%rbp),%edi
mov    -0x2038(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    11d5 <main+0x75>
jmp    1285 <main+0x125>
jmp    1231 <main+0xd1>
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x2c(%rbp)
cmp    $0x4b,%eax
jge    1280 <main+0x120>
mov    -0x24(%rbp),%edi
lea    -0x2030(%rbp),%rsi
mov    $0x2000,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jle    127b <main+0x11b>
mov    -0x2c(%rbp),%esi
lea    0xda0(%rip),%rdi
lea    -0x2030(%rbp),%rdx
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1231 <main+0xd1>
jmp    1285 <main+0x125>
jmp    1298 <main+0x138>
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2000,%rsp
mov    %rsi,%r14
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11d0 <main+0x60>
mov    %eax,%r12d
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11ee <main+0x7e>
mov    $0x4b,%ebx
mov    0x8(%r14),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
add    $0xffffffff,%ebx
jne    11b0 <main+0x40>
jmp    11dc <main+0x6c>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x2000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%ebp
mov    %rsp,%r14
lea    0xe10(%rip),%r15
jmp    1208 <main+0x98>
nop
add    $0x1,%ebp
cmp    $0x4c,%ebp
je     11dc <main+0x6c>
mov    $0x2000,%edx
mov    %r12d,%edi
mov    %r14,%rsi
call   1070 <read@plt>
test   %rax,%rax
jle    1200 <main+0x90>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1200 <main+0x90>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/reverse\00"
@O_RDWR = global i32 0
@.str.1 = [14 x i8] c"Read(%i): %s\0A\00"
@.str.2 = [41 x i8] c"Failed to open /dev/reverse (try sudo?)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 8192, i64* %6
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %7
%13 = alloca i8, i64 8192
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %8
store i32 75, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
%16 = load i32, i32* %8
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %52
18: ; preds = %2
%19 = call i64 (...) @fork()
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %37
21: ; preds = %18
br label %22
22: ; preds = %26, %21
%23 = load i32, i32* %11
%24 = add nsw i32 %23, 1
store i32 %24, i32* %11
%25 = icmp slt i32 %23, 75
br i1 %25, label %26, label %36
26: ; preds = %22
%27 = load i32, i32* %8
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @strlen(i8* %33)
%35 = call i32 @write(i32 %27, i8* %30, i32 %34)
br label %22
36: ; preds = %22
br label %51
37: ; preds = %18
br label %38
38: ; preds = %49, %37
%39 = load i32, i32* %10
%40 = add nsw i32 %39, 1
store i32 %40, i32* %10
%41 = icmp slt i32 %39, 75
br i1 %41, label %42, label %50
42: ; preds = %38
%43 = load i32, i32* %8
%44 = call i64 @read(i32 %43, i8* %13, i64 8192)
%45 = icmp sgt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* %10
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %47, i8* %13)
br label %49
49: ; preds = %46, %42
br label %38
50: ; preds = %38
br label %51
51: ; preds = %50, %36
br label %54
52: ; preds = %2
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %54
54: ; preds = %52, %51
store i32 0, i32* %3
%55 = load i8*, i8** %7
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/reverse\00"
@O_RDWR = global i32 0
@.str.1 = [14 x i8] c"Read(%i): %s\0A\00"
@str = [40 x i8] c"Failed to open /dev/reverse (try sudo?)\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8192 x i8]
%4 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %29, label %8
8: ; preds = %2
%9 = tail call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
br i1 %10, label %20, label %11
11: ; preds = %8
%12 = getelementptr inbounds i8*, i8** %1, i64 1
br label %13
13: ; preds = %11, %13
%14 = phi i32 [ 0, %11 ], [ %15, %13 ]
%15 = add nuw nsw i32 %14, 1
%16 = load i8*, i8** %12
%17 = tail call i32 @strlen(i8* %16)
%18 = tail call i32 @write(i32 %6, i8* %16, i32 %17)
%19 = icmp eq i32 %15, 75
br i1 %19, label %31, label %13
20: ; preds = %8, %26
%21 = phi i32 [ %27, %26 ], [ 1, %8 ]
%22 = call i64 @read(i32 %6, i8* %4, i64 8192)
%23 = icmp sgt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %21, i8* %4)
br label %26
26: ; preds = %24, %20
%27 = add nuw nsw i32 %21, 1
%28 = icmp eq i32 %27, 76
br i1 %28, label %31, label %20
29: ; preds = %2
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
br label %31
31: ; preds = %13, %26, %29
ret i32 0
}