#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*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ combine_letters(char *, int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int main() {
  int failed = 0;
  char buffer[] = {[100] = '\0'};
  int pipefds[2];
  int stdoutfd = dup(STDOUT_FILENO);
  if (pipe(pipefds) != 0) {
    return -1;
  }
  dup2(pipefds[1], STDOUT_FILENO);
  close(pipefds[1]);
  combine_letters("wxyz", 4);
  fflush(stdout);
  char *expect =
      "w\nwx\nwxy\nwxyz\nwxz\nwy\nwyz\nwz\nx\nxy\nxyz\nxz\ny\nyz\nz\n";
  read(pipefds[0], buffer, 100);
  if (strcmp(buffer, expect)) {
    failed = -1;
    printf("error, buffer %s not equal to '%s'\n", buffer, expect);
  }
  dup2(stdoutfd, STDOUT_FILENO);
  return failed;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x65,%edx
call   1070 <memset@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1060 <dup@plt>
mov    %eax,-0x7c(%rbp)
lea    -0x78(%rbp),%rdi
call   1090 <pipe@plt>
cmp    $0x0,%rax
je     11f9 <main+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <main+0xff>
mov    -0x74(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x74(%rbp),%edi
call   1080 <close@plt>
lea    0xde7(%rip),%rdi
mov    $0x4,%esi
call   1030 <combine_letters@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
lea    0xdcd(%rip),%rax
mov    %rax,-0x88(%rbp)
mov    -0x78(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x64,%edx
call   10a0 <read@plt>
lea    -0x70(%rbp),%rdi
mov    -0x88(%rbp),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
je     1288 <main+0xe8>
movl   $0xffffffff,-0x8(%rbp)
lea    -0x70(%rbp),%rsi
mov    -0x88(%rbp),%rdx
lea    0xdb2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x7c(%rbp),%edi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x6d(%rsp)
mov    0x2e24(%rip),%r14
mov    (%r14),%edi
call   1060 <dup@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1277 <main+0xe7>
mov    0xc(%rsp),%edi
mov    (%r14),%esi
call   1040 <dup2@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
lea    0xdf6(%rip),%rdi
mov    $0x4,%esi
call   1030 <combine_letters@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbp
mov    %rbp,%rsi
mov    $0x64,%edx
call   1090 <read@plt>
lea    0xdc6(%rip),%rsi
mov    %rbp,%rdi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     126b <main+0xdb>
lea    0xde2(%rip),%rdi
lea    0xdab(%rip),%rdx
lea    0x10(%rsp),%rsi
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
jmp    126d <main+0xdd>
xor    %ebp,%ebp
mov    (%r14),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    %ebp,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@.str = [5 x i8] c"wxyz\00"
@stdout = global i32 0
@.str.1 = [48 x i8] c"w\0Awx\0Awxy\0Awxyz\0Awxz\0Awy\0Awyz\0Awz\0Ax\0Axy\0Axyz\0Axz\0Ay\0Ayz\0Az\0A\00"
@.str.2 = [36 x i8] c"error, buffer %s not equal to '%s'\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [101 x i8]
%4 = alloca [2 x i32]
%5 = alloca i32
%6 = alloca i8*
store i32 0, i32* %1
store i32 0, i32* %2
%7 = bitcast [101 x i8]* %3 to i8*
%8 = load i32, i32* @STDOUT_FILENO
%9 = call i32 @dup(i32 %8)
store i32 %9, i32* %5
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %0
store i32 -1, i32* %1
br label %42
14: ; preds = %0
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%16 = load i32, i32* %15
%17 = load i32, i32* @STDOUT_FILENO
%18 = call i32 @dup2(i32 %16, i32 %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = call i32 @combine_letters(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%23 = load i32, i32* @stdout
%24 = call i32 @fflush(i32 %23)
store i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8** %6
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds [101 x i8], [101 x i8]* %3, i64 0, i64 0
%28 = call i32 @read(i32 %26, i8* %27, i32 100)
%29 = getelementptr inbounds [101 x i8], [101 x i8]* %3, i64 0, i64 0
%30 = load i8*, i8** %6
%31 = call i64 @strcmp(i8* %29, i8* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %14
store i32 -1, i32* %2
%34 = getelementptr inbounds [101 x i8], [101 x i8]* %3, i64 0, i64 0
%35 = load i8*, i8** %6
%36 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %34, i8* %35)
br label %37
37: ; preds = %33, %14
%38 = load i32, i32* %5
%39 = load i32, i32* @STDOUT_FILENO
%40 = call i32 @dup2(i32 %38, i32 %39)
%41 = load i32, i32* %2
store i32 %41, i32* %1
br label %42
42: ; preds = %37, %13
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [5 x i8] c"wxyz\00"
@stdout = global i32 0
@.str.1 = [48 x i8] c"w\0Awx\0Awxy\0Awxyz\0Awxz\0Awy\0Awyz\0Awz\0Ax\0Axy\0Axyz\0Axz\0Ay\0Ayz\0Az\0A\00"
@.str.2 = [36 x i8] c"error, buffer %s not equal to '%s'\0A\00"
define i32 @main() {
%1 = alloca [101 x i8]
%2 = alloca [2 x i32]
%3 = getelementptr inbounds [101 x i8], [101 x i8]* %1, i64 0, i64 0
%4 = bitcast [2 x i32]* %2 to i8*
%5 = load i32, i32* @STDOUT_FILENO
%6 = tail call i32 @dup(i32 %5)
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %30
10: ; preds = %0
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%12 = load i32, i32* %11
%13 = load i32, i32* @STDOUT_FILENO
%14 = call i32 @dup2(i32 %12, i32 %13)
%15 = load i32, i32* %11
%16 = call i32 @close(i32 %15)
%17 = call i32 @combine_letters(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%18 = load i32, i32* @stdout
%19 = call i32 @fflush(i32 %18)
%20 = load i32, i32* %7
%21 = call i32 @read(i32 %20, i8* %3, i32 100)
%22 = call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %10
%25 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %3, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %10
%27 = phi i32 [ -1, %24 ], [ 0, %10 ]
%28 = load i32, i32* @STDOUT_FILENO
%29 = call i32 @dup2(i32 %6, i32 %28)
br label %30
30: ; preds = %0, %26
%31 = phi i32 [ %27, %26 ], [ -1, %0 ]
ret i32 %31
}