#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 scalar_t__ pid_t;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
scalar_t__ fork();
int open(char *, int, int);
int /*<<< orphan*/ pipe(int *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  char buf[1024];
  int pipeIn[2];
  int rc;
  int logTxt;
  pid_t p;
  logTxt = open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0600);
  pipe(pipeIn);
  p = fork();
  if (p == 0) {
    pid_t p2;
    int pipe2[2];
    pipe(pipe2);
    p2 = fork();
    if (p2 == 0) {
      dup2(pipeIn[0], 0);
      dup2(pipe2[1], 1);
      close(pipeIn[0]);
      close(pipe2[1]);
      argv[0] = "cat";
      argv[1] = NULL;
      execvp("cat", argv);
    } else {
      char *c = "> ";
      while ((rc = read(pipe2[0], buf, 1024)) != -1) {
        write(1, buf, rc);
        write(logTxt, c, 2);
        write(logTxt, buf, rc);
      }
    }
  } else {
    char *c = "< ";
    while ((rc = read(0, buf, 1024)) != -1) {
      write(pipeIn[1], buf, rc);
      write(logTxt, c, 2);
      write(logTxt, buf, rc);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    0x2e4f(%rip),%rax
or     (%rax),%esi
mov    0x2e3e(%rip),%rax
or     (%rax),%esi
lea    0xe45(%rip),%rdi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,-0x420(%rbp)
lea    -0x418(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    1306 <main+0x186>
lea    -0x438(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    127d <main+0xfd>
mov    -0x418(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x434(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x418(%rbp),%edi
call   1050 <close@plt>
mov    -0x434(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
lea    0xdaf(%rip),%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
movq   $0x0,0x8(%rax)
mov    -0x10(%rbp),%rsi
lea    0xd95(%rip),%rdi
call   1090 <execvp@plt>
jmp    1301 <main+0x181>
lea    0xd88(%rip),%rax
mov    %rax,-0x440(%rbp)
mov    -0x438(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x41c(%rbp)
cmp    $0xffffffff,%eax
je     12fc <main+0x17c>
lea    -0x410(%rbp),%rsi
mov    -0x41c(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x420(%rbp),%edi
mov    -0x440(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x420(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1030 <write@plt>
jmp    128b <main+0x10b>
jmp    1301 <main+0x181>
jmp    1387 <main+0x207>
lea    0xd02(%rip),%rax
mov    %rax,-0x448(%rbp)
lea    -0x410(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x41c(%rbp)
cmp    $0xffffffff,%eax
je     1382 <main+0x202>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1030 <write@plt>
mov    -0x420(%rbp),%edi
mov    -0x448(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x420(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1030 <write@plt>
jmp    1314 <main+0x194>
jmp    1387 <main+0x207>
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    0x2e34(%rip),%rcx
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %rsi,%r14
mov    0x2e47(%rip),%rcx
or     (%rcx),%eax
lea    0xe4e(%rip),%rdi
mov    %eax,%esi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,%r15d
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1246 <main+0xc6>
lea    0x10(%rsp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     131a <main+0x19a>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
lea    0xe10(%rip),%r14
nop
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1200 <main+0x80>
jmp    131a <main+0x19a>
mov    %rsp,%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     12d5 <main+0x155>
mov    (%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     131a <main+0x19a>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
lea    0xd89(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    (%rsp),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1290 <main+0x110>
jmp    131a <main+0x19a>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    0x4(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
lea    0xd01(%rip),%rdi
mov    %rdi,(%r14)
movq   $0x0,0x8(%r14)
mov    %r14,%rsi
call   1090 <execvp@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"log.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [4 x i8] c"cat\00"
@.str.2 = [3 x i8] c"> \00"
@.str.3 = [3 x i8] c"< \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca [2 x i32]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca [2 x i32]
%13 = alloca i8*
%14 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %19, i32 384)
store i32 %20, i32* %9
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%22 = call i32 @pipe(i32* %21)
%23 = call i64 (...) @fork()
store i64 %23, i64* %10
%24 = load i64, i64* %10
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %71
26: ; preds = %2
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%28 = call i32 @pipe(i32* %27)
%29 = call i64 (...) @fork()
store i64 %29, i64* %11
%30 = load i64, i64* %11
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %51
32: ; preds = %26
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @dup2(i32 %34, i32 0)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @dup2(i32 %37, i32 1)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 0
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %46
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
store i8* null, i8** %48
%49 = load i8**, i8*** %5
%50 = call i32 @execvp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %49)
br label %70
51: ; preds = %26
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %13
br label %52
52: ; preds = %58, %51
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%54 = load i32, i32* %53
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%56 = call i32 @read(i32 %54, i8* %55, i32 1024)
store i32 %56, i32* %8
%57 = icmp ne i32 %56, -1
br i1 %57, label %58, label %69
58: ; preds = %52
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%60 = load i32, i32* %8
%61 = call i32 @write(i32 1, i8* %59, i32 %60)
%62 = load i32, i32* %9
%63 = load i8*, i8** %13
%64 = call i32 @write(i32 %62, i8* %63, i32 2)
%65 = load i32, i32* %9
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%67 = load i32, i32* %8
%68 = call i32 @write(i32 %65, i8* %66, i32 %67)
br label %52
69: ; preds = %52
br label %70
70: ; preds = %69, %32
br label %90
71: ; preds = %2
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %14
br label %72
72: ; preds = %76, %71
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%74 = call i32 @read(i32 0, i8* %73, i32 1024)
store i32 %74, i32* %8
%75 = icmp ne i32 %74, -1
br i1 %75, label %76, label %89
76: ; preds = %72
%77 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%78 = load i32, i32* %77
%79 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%80 = load i32, i32* %8
%81 = call i32 @write(i32 %78, i8* %79, i32 %80)
%82 = load i32, i32* %9
%83 = load i8*, i8** %14
%84 = call i32 @write(i32 %82, i8* %83, i32 2)
%85 = load i32, i32* %9
%86 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%87 = load i32, i32* %8
%88 = call i32 @write(i32 %85, i8* %86, i32 %87)
br label %72
89: ; preds = %72
br label %90
90: ; preds = %89, %70
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
@.str = [8 x i8] c"log.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [4 x i8] c"cat\00"
@.str.2 = [3 x i8] c"> \00"
@.str.3 = [3 x i8] c"< \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [1024 x i8]
%4 = alloca [2 x i32]
%5 = alloca [2 x i32]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast [2 x i32]* %4 to i8*
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %9, %8
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %12, i32 384)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%15 = call i32 @pipe(i32* %14)
%16 = call i64 (...) @fork()
%17 = icmp eq i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %2
%19 = call i32 @read(i32 0, i8* %6, i32 1024)
%20 = icmp eq i32 %19, -1
br i1 %20, label %62, label %21
21: ; preds = %18
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
br label %54
23: ; preds = %2
%24 = bitcast [2 x i32]* %5 to i8*
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%26 = call i32 @pipe(i32* %25)
%27 = call i64 (...) @fork()
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %23
%30 = load i32, i32* %25
%31 = call i32 @read(i32 %30, i8* %6, i32 1024)
%32 = icmp eq i32 %31, -1
br i1 %32, label %53, label %45
33: ; preds = %23
%34 = load i32, i32* %14
%35 = call i32 @dup2(i32 %34, i32 0)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @dup2(i32 %37, i32 1)
%39 = load i32, i32* %14
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* %36
%42 = call i32 @close(i32 %41)
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %1
%43 = getelementptr inbounds i8*, i8** %1, i64 1
store i8* null, i8** %43
%44 = call i32 @execvp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %1)
br label %53
45: ; preds = %29, %45
%46 = phi i32 [ %51, %45 ], [ %31, %29 ]
%47 = call i32 @write(i32 1, i8* %6, i32 %46)
%48 = call i32 @write(i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2)
%49 = call i32 @write(i32 %13, i8* %6, i32 %46)
%50 = load i32, i32* %25
%51 = call i32 @read(i32 %50, i8* %6, i32 1024)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %45
53: ; preds = %45, %29, %33
br label %62
54: ; preds = %21, %54
%55 = phi i32 [ %19, %21 ], [ %60, %54 ]
%56 = load i32, i32* %22
%57 = call i32 @write(i32 %56, i8* %6, i32 %55)
%58 = call i32 @write(i32 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
%59 = call i32 @write(i32 %13, i8* %6, i32 %55)
%60 = call i32 @read(i32 0, i8* %6, i32 1024)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %54
62: ; preds = %54, %18, %53
ret i32 0
}