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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    -0x14(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11e3 <main+0x63>
lea    0xe2c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xe24(%rip),%rdi
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12df <main+0x15f>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%edx
call   1030 <write@plt>
cmp    -0x8(%rbp),%eax
je     124f <main+0xcf>
lea    0xdfd(%rip),%rdi
call   1090 <perror@plt>
jmp    12df <main+0x15f>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    127c <main+0xfc>
mov    -0x30(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x58,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1256 <main+0xd6>
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edi
mov    0x2d4e(%rip),%rax
mov    (%rax),%edx
call   1070 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12af <main+0x12f>
lea    0xdad(%rip),%rdi
call   1090 <perror@plt>
jmp    12df <main+0x15f>
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x8(%rbp),%eax
je     12da <main+0x15a>
lea    0xd94(%rip),%rdi
call   1090 <perror@plt>
jmp    12df <main+0x15f>
jmp    11f5 <main+0x75>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsp,%r12
mov    0x2e30(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    -0x30(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     128d <main+0xfd>
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xe38(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e00(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%esi
mov    (%r14),%edx
mov    %r15,%rdi
call   1090 <fgets@plt>
test   %rax,%rax
je     1279 <main+0xe9>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    -0x2c(%rbp),%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jne    1264 <main+0xd4>
test   %ebx,%ebx
jle    1232 <main+0xa2>
mov    %ebx,%edx
mov    %r15,%rdi
mov    $0x58,%esi
call   1060 <memset@plt>
mov    -0x30(%rbp),%edi
mov    0x0(%r13),%edx
mov    %r15,%rsi
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     126d <main+0xdd>
mov    %eax,%ebx
mov    $0x1,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     11f0 <main+0x60>
lea    0xe02(%rip),%rdi
jmp    1274 <main+0xe4>
lea    0xdd7(%rip),%rdi
jmp    1274 <main+0xe4>
lea    0xdde(%rip),%rdi
call   10a0 <perror@plt>
mov    %r12,%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [20 x i8] c"could not make pipe\00"
@.str.1 = [46 x i8] c"Got a pipe! It is file description : {%d %d}\0A\00"
@stdin = global i32 0
@.str.2 = [16 x i8] c"writing to pipe\00"
@.str.3 = [18 x i8] c"reading from pipe\00"
@.str.4 = [18 x i8] c"writing to stdout\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = tail call i8* @llvm.stacksave()
%6 = alloca i8, i64 %4
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* %7
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%16 = load i32, i32* %15
%17 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %16)
br label %18
18: ; preds = %37, %13
%19 = load i32, i32* @BUFSIZ
%20 = load i32, i32* @stdin
%21 = call i64 @fgets(i8* %6, i32 %19, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %43, label %23
23: ; preds = %18
%24 = call i32 @strlen(i8* %6)
%25 = load i32, i32* %15
%26 = call i32 @write(i32 %25, i8* %6, i32 %24)
%27 = icmp eq i32 %26, %24
br i1 %27, label %28, label %40
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* %7
%34 = load i32, i32* @BUFSIZ
%35 = call i32 @read(i32 %33, i8* %6, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %40, label %37
37: ; preds = %32
%38 = call i32 @write(i32 1, i8* %6, i32 %35)
%39 = icmp eq i32 %38, %35
br i1 %39, label %18, label %40
40: ; preds = %37, %32, %23
%41 = 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 ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), %37 ]
%42 = call i32 @perror(i8* %41)
br label %43
43: ; preds = %18, %40
ret i32 0
}