#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*/ exit(int);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, int, int);
int read(int, char *, int);
int /*<<< orphan*/ stdin;
int strlen(char *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int len, i, apipe[2];
  char buf[1024];
  if (pipe(apipe) == -1) {
    perror("pipe() failed");
    exit(1);
  }
  printf("Got a pipe, {%d, %d}\n", apipe[0], apipe[1]);
  while (fgets(buf, 1024, stdin)) {
    len = strlen(buf);
    if (write(apipe[1], buf, len) != len) {
      perror("write() failed");
      exit(1);
    }
    for (i = 0; i < len; ++i)
      buf[i] = 'X';
    len = read(apipe[0], buf, 1024);
    if (len == -1) {
      perror("read() failed");
      exit(1);
    }
    if (write(1, buf, len) != len) {
      perror("write() failed");
      exit(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11c1 <main+0x41>
lea    0xe4e(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe40(%rip),%rdi
call   1050 <printf@plt>
lea    -0x420(%rbp),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12d3 <main+0x153>
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
cmp    -0x14(%rbp),%eax
je     1237 <main+0xb7>
lea    0xdfc(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1264 <main+0xe4>
movslq -0x18(%rbp),%rax
movb   $0x58,-0x420(%rbp,%rax,1)
58
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    123e <main+0xbe>
mov    -0x20(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    129b <main+0x11b>
lea    0xda7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x420(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x14(%rbp),%eax
je     12ce <main+0x14e>
lea    0xd65(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    11d3 <main+0x53>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     126c <main+0xdc>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0xe51(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e1f(%rip),%r14
lea    0x10(%rsp),%rbx
xchg   %ax,%ax
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     1248 <main+0xb8>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    123f <main+0xaf>
test   %ebp,%ebp
jle    1214 <main+0x84>
mov    %ebp,%edx
mov    %rbx,%rdi
mov    $0x58,%esi
call   1060 <memset@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     1256 <main+0xc6>
mov    %eax,%ebp
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     11d0 <main+0x40>
lea    0xdde(%rip),%rdi
jmp    125d <main+0xcd>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdd6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd8d(%rip),%rdi
jmp    125d <main+0xcd>

LLVM IR (-O0):
@.str = [14 x i8] c"pipe() failed\00"
@.str.1 = [22 x i8] c"Got a pipe, {%d, %d}\0A\00"
@stdin = global i32 0
@.str.2 = [15 x i8] c"write() failed\00"
@.str.3 = [14 x i8] c"read() failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2 x i32]
%9 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%18 = load i32, i32* %17
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %20)
br label %22
22: ; preds = %71, %16
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%24 = load i32, i32* @stdin
%25 = call i64 @fgets(i8* %23, i32 1024, i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %72
27: ; preds = %22
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%29 = call i32 @strlen(i8* %28)
store i32 %29, i32* %6
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%33 = load i32, i32* %6
%34 = call i32 @write(i32 %31, i8* %32, i32 %33)
%35 = load i32, i32* %6
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %40
37: ; preds = %27
%38 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %27
store i32 0, i32* %7
br label %41
41: ; preds = %49, %40
%42 = load i32, i32* %7
%43 = load i32, i32* %6
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %52
45: ; preds = %41
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %47
store i8 88, i8* %48
br label %49
49: ; preds = %45
%50 = load i32, i32* %7
%51 = add nsw i32 %50, 1
store i32 %51, i32* %7
br label %41
52: ; preds = %41
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%54 = load i32, i32* %53
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%56 = call i32 @read(i32 %54, i8* %55, i32 1024)
store i32 %56, i32* %6
%57 = load i32, i32* %6
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %62
59: ; preds = %52
%60 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %52
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%64 = load i32, i32* %6
%65 = call i32 @write(i32 1, i8* %63, i32 %64)
%66 = load i32, i32* %6
%67 = icmp ne i32 %65, %66
br i1 %67, label %68, label %71
68: ; preds = %62
%69 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %62
br label %22
72: ; preds = %22
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"pipe() failed\00"
@.str.1 = [22 x i8] c"Got a pipe, {%d, %d}\0A\00"
@stdin = global i32 0
@.str.2 = [15 x i8] c"write() failed\00"
@.str.3 = [14 x i8] c"read() failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = bitcast [2 x i32]* %3 to i8*
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i32, i32* %8
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %17)
br label %19
19: ; preds = %42, %14
%20 = load i32, i32* @stdin
%21 = call i64 @fgets(i8* %7, i32 1024, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %48, label %23
23: ; preds = %19
%24 = call i32 @strlen(i8* %7)
%25 = load i32, i32* %16
%26 = call i32 @write(i32 %25, i8* %7, i32 %24)
%27 = icmp eq i32 %26, %24
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = icmp sgt i32 %24, 0
br i1 %29, label %30, label %35
30: ; preds = %28
%31 = zext i32 %24 to i64
br label %35
32: ; preds = %23
%33 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %30, %28
%36 = load i32, i32* %8
%37 = call i32 @read(i32 %36, i8* %7, i32 1024)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %35
%43 = call i32 @write(i32 1, i8* %7, i32 %37)
%44 = icmp eq i32 %43, %37
br i1 %44, label %19, label %45
45: ; preds = %42
%46 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %19
ret i32 0
}