#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 *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int fd = open("/dev/reverse", O_RDWR);
  if (argc == 2) {
    printf("Word: %s\t\t", argv[1]);
    write(fd, argv[1], strlen(argv[1]));
    read(fd, argv[1], strlen(argv[1]));
    printf("Read: %s\n", argv[1]);
    return 0;
  }
  char *phrase = "are you as clever as i am";
  int len = strlen(phrase);
  if (fork()) {
    while (1) {
      write(fd, phrase, len);
      sleep(1);
    }
  } else {
    char buf[len + 1];
    while (1) {
      read(fd, buf, len);
      printf("Word: %s?\t\t", phrase);
      printf("Read: %s?\n", buf);
      sleep(1);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1231 <main+0xc1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe56(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1060 <read@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdf3(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret
lea    0xdea(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
je     127c <main+0x10c>
jmp    125e <main+0xee>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    125e <main+0xee>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
jmp    12aa <main+0x13a>
mov    -0x60(%rbp),%rsi
mov    -0x14(%rbp),%edi
mov    -0x24(%rbp),%edx
call   1060 <read@plt>
mov    -0x20(%rbp),%rsi
lea    0xd78(%rip),%rdi
call   1050 <printf@plt>
mov    -0x60(%rbp),%rsi
lea    0xd74(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    12a5 <main+0x135>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r14d
cmp    $0x2,%ebx
jne    1203 <main+0x93>
mov    0x8(%r15),%rsi
lea    0xe62(%rip),%rdi
call   1050 <printf@plt>
mov    0x8(%r15),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x8(%r15),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <read@plt>
mov    0x8(%r15),%rsi
lea    0xe2b(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe18(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     124a <main+0xda>
lea    0xdfd(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    1230 <main+0xc0>
mov    %r15d,%eax
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xdba(%rip),%r13
lea    0xdd9(%rip),%r12
nop
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1060 <read@plt>
lea    0xdb7(%rip),%rdi
mov    %r13,%rsi
call   1050 <printf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    1270 <main+0x100>

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

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