#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_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
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(void) {
  int len, apipe[2];
  char buf[BUFSIZ];
  if (pipe(apipe) == -1) {
    perror("create a pipe failed");
    exit(EXIT_FAILURE);
  }
  printf("Got a pipe! It is file descriptors: {%d %d}\n", apipe[0], apipe[1]);
  while (fgets(buf, BUFSIZ, stdin)) {
    len = strlen(buf);
    if (write(apipe[1], buf, len) != len) {
      perror("write to a pipe failed");
      break;
    }
    for (int i = 0; i < len; ++i) {
      buf[i] = 'x';
    }
    len = read(apipe[0], buf, len);
    if (len == -1) {
      perror("read from a pipe failed");
      break;
    }
    if (write(1, buf, len) != len) {
      perror("write to stdout failed");
      break;
    }
  }
  return EXIT_SUCCESS;
}


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,-0x18(%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,-0x20(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11e7 <main+0x67>
lea    0xe2c(%rip),%rdi
call   1090 <perror@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe21(%rip),%rdi
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12dd <main+0x15d>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%edx
call   1030 <write@plt>
cmp    -0x8(%rbp),%eax
je     1253 <main+0xd3>
lea    0xdf9(%rip),%rdi
call   1090 <perror@plt>
jmp    12dd <main+0x15d>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1280 <main+0x100>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x78,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    125a <main+0xda>
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%edx
call   1070 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12ad <main+0x12d>
lea    0xdb6(%rip),%rdi
call   1090 <perror@plt>
jmp    12dd <main+0x15d>
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x8(%rbp),%eax
je     12d8 <main+0x158>
lea    0xda3(%rip),%rdi
call   1090 <perror@plt>
jmp    12dd <main+0x15d>
jmp    11f9 <main+0x79>
mov    0x2ce4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x18(%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     1292 <main+0x102>
mov    -0x30(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xe39(%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     1277 <main+0xe7>
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    1262 <main+0xd2>
test   %ebx,%ebx
jle    1232 <main+0xa2>
mov    %ebx,%edx
mov    %r15,%rdi
mov    $0x78,%esi
call   1060 <memset@plt>
mov    -0x30(%rbp),%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     126b <main+0xdb>
mov    %eax,%ebx
mov    $0x1,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     11f0 <main+0x60>
lea    0xe11(%rip),%rdi
jmp    1272 <main+0xe2>
lea    0xdd9(%rip),%rdi
jmp    1272 <main+0xe2>
lea    0xde7(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%eax
mov    %r12,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd67(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [21 x i8] c"create a pipe failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [45 x i8] c"Got a pipe! It is file descriptors: {%d %d}\0A\00"
@stdin = global i32 0
@.str.2 = [23 x i8] c"write to a pipe failed\00"
@.str.3 = [24 x i8] c"read from a pipe failed\00"
@.str.4 = [23 x i8] c"write to stdout failed\00"
@EXIT_SUCCESS = global i32 0
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 %14
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = load i32, i32* %7
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %17)
br label %19
19: ; preds = %37, %14
%20 = load i32, i32* @BUFSIZ
%21 = load i32, i32* @stdin
%22 = call i64 @fgets(i8* %6, i32 %20, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %43, label %24
24: ; preds = %19
%25 = call i32 @strlen(i8* %6)
%26 = load i32, i32* %16
%27 = call i32 @write(i32 %26, i8* %6, i32 %25)
%28 = icmp eq i32 %27, %25
br i1 %28, label %29, label %40
29: ; preds = %24
%30 = icmp sgt i32 %25, 0
br i1 %30, label %31, label %33
31: ; preds = %29
%32 = zext i32 %25 to i64
br label %33
33: ; preds = %31, %29
%34 = load i32, i32* %7
%35 = call i32 @read(i32 %34, i8* %6, i32 %25)
%36 = icmp eq i32 %35, -1
br i1 %36, label %40, label %37
37: ; preds = %33
%38 = call i32 @write(i32 1, i8* %6, i32 %35)
%39 = icmp eq i32 %38, %35
br i1 %39, label %19, label %40
40: ; preds = %37, %33, %24
%41 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), %24 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), %33 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), %37 ]
%42 = call i32 @perror(i8* %41)
br label %43
43: ; preds = %19, %40
%44 = load i32, i32* @EXIT_SUCCESS
ret i32 %44
}