#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[256];
  if (pipe(apipe) == -1) {
    perror("could not make pipe");
    exit(1);
  }
  printf("Got a pipe! It is file descriptors: { %d %d }\n", apipe[0], apipe[1]);
  while (fgets(buf, 256, stdin)) {
    len = strlen(buf);
    if (write(apipe[1], buf, len) != len) {
      perror("writing ti pipe");
      break;
    }
    for (i = 0; i < len; i++) {
      buf[i] = 'X';
    }
    len = read(apipe[0], buf, 256);
    if (len == -1) {
      perror("reading from pipe");
      break;
    }
    if (write(1, buf, len) != len) {
      perror("writeing to stdout");
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%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    0xe46(%rip),%rdi
call   1050 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12c4 <main+0x144>
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
cmp    -0x14(%rbp),%eax
je     1232 <main+0xb2>
lea    0xe1b(%rip),%rdi
call   1090 <perror@plt>
jmp    12c4 <main+0x144>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    125f <main+0xdf>
movslq -0x18(%rbp),%rax
movb   $0x58,-0x120(%rbp,%rax,1)
58
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1239 <main+0xb9>
mov    -0x20(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1070 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1291 <main+0x111>
lea    0xdcc(%rip),%rdi
call   1090 <perror@plt>
jmp    12c4 <main+0x144>
lea    -0x120(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x14(%rbp),%eax
je     12bf <main+0x13f>
lea    0xdb0(%rip),%rdi
call   1090 <perror@plt>
jmp    12c4 <main+0x144>
jmp    11d3 <main+0x53>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     1283 <main+0xf3>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0xe51(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e19(%rip),%r15
lea    0x10(%rsp),%rbx
lea    0xe68(%rip),%r14
lea    0xe71(%rip),%r12
lea    0xe7c(%rip),%r13
nopl   0x0(%rax)
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1090 <fgets@plt>
test   %rax,%rax
je     126f <main+0xdf>
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    1267 <main+0xd7>
test   %ebp,%ebp
jle    1234 <main+0xa4>
mov    %ebp,%edx
mov    %rbx,%rdi
mov    $0x58,%esi
call   1060 <memset@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     1264 <main+0xd4>
mov    %eax,%ebp
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     11f0 <main+0x60>
mov    %r13,%r14
jmp    1267 <main+0xd7>
mov    %r12,%r14
mov    %r14,%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd76(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"could not make pipe\00"
@.str.1 = [47 x i8] c"Got a pipe! It is file descriptors: { %d %d }\0A\00"
@stdin = global i32 0
@.str.2 = [16 x i8] c"writing ti pipe\00"
@.str.3 = [18 x i8] c"reading from pipe\00"
@.str.4 = [19 x i8] c"writeing to stdout\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 [256 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 ([20 x i8], [20 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 ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %20)
br label %22
22: ; preds = %68, %16
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%24 = load i32, i32* @stdin
%25 = call i64 @fgets(i8* %23, i32 256, i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %69
27: ; preds = %22
%28 = getelementptr inbounds [256 x i8], [256 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 [256 x i8], [256 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 %39
37: ; preds = %27
%38 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %69
39: ; preds = %27
store i32 0, i32* %7
br label %40
40: ; preds = %48, %39
%41 = load i32, i32* %7
%42 = load i32, i32* %6
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %51
44: ; preds = %40
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 %46
store i8 88, i8* %47
br label %48
48: ; preds = %44
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
br label %40
51: ; preds = %40
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%53 = load i32, i32* %52
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%55 = call i32 @read(i32 %53, i8* %54, i32 256)
store i32 %55, i32* %6
%56 = load i32, i32* %6
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %51
%59 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %69
60: ; preds = %51
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%62 = load i32, i32* %6
%63 = call i32 @write(i32 1, i8* %61, i32 %62)
%64 = load i32, i32* %6
%65 = icmp ne i32 %63, %64
br i1 %65, label %66, label %68
66: ; preds = %60
%67 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %69
68: ; preds = %60
br label %22
69: ; preds = %66, %58, %37, %22
ret i32 0
}

LLVM IR (-O2):
@.str = [20 x i8] c"could not make pipe\00"
@.str.1 = [47 x i8] c"Got a pipe! It is file descriptors: { %d %d }\0A\00"
@stdin = global i32 0
@.str.2 = [16 x i8] c"writing ti pipe\00"
@.str.3 = [18 x i8] c"reading from pipe\00"
@.str.4 = [19 x i8] c"writeing to stdout\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = bitcast [2 x i32]* %3 to i8*
%7 = getelementptr inbounds [256 x i8], [256 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 ([20 x i8], [20 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 ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %17)
br label %19
19: ; preds = %36, %14
%20 = load i32, i32* @stdin
%21 = call i64 @fgets(i8* %7, i32 256, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %42, 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 %39
28: ; preds = %23
%29 = icmp sgt i32 %24, 0
br i1 %29, label %30, label %32
30: ; preds = %28
%31 = zext i32 %24 to i64
br label %32
32: ; preds = %30, %28
%33 = load i32, i32* %8
%34 = call i32 @read(i32 %33, i8* %7, i32 256)
%35 = icmp eq i32 %34, -1
br i1 %35, label %39, label %36
36: ; preds = %32
%37 = call i32 @write(i32 1, i8* %7, i32 %34)
%38 = icmp eq i32 %37, %34
br i1 %38, label %19, label %39
39: ; preds = %36, %32, %23
%40 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), %23 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), %32 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), %36 ]
%41 = call i32 @perror(i8* %40)
br label %42
42: ; preds = %19, %39
ret i32 0
}