#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 dup(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ permute_letters(char *, int);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int main() {
  int failed = 0;
  char buffer[] = {[40] = '\0'};
  int pipefds[2];
  int stdoutfd = dup(STDOUT_FILENO);
  if (pipe(pipefds) != 0) {
    return -1;
  }
  dup2(pipefds[1], STDOUT_FILENO);
  close(pipefds[1]);
  permute_letters("hat", 3);
  fflush(stdout);
  read(pipefds[0], buffer, 40);
  if (strcmp(buffer, "hat\nhta\naht\nath\ntha\ntah\n")) {
    failed = -1;
    printf("error, buffer %s not equal to 'hat\nhta\naht\nath\ntha\ntah\n'",
           buffer);
  }
  dup2(stdoutfd, STDOUT_FILENO);
  return failed;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x29,%edx
call   1060 <memset@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1050 <dup@plt>
mov    %eax,-0x4c(%rbp)
lea    -0x48(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
je     11f6 <main+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <main+0xe7>
mov    -0x44(%rbp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
lea    0xdea(%rip),%rdi
mov    $0x3,%esi
call   10b0 <permute_letters@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    -0x48(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x28,%edx
call   1090 <read@plt>
lea    -0x40(%rbp),%rdi
lea    0xdba(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     1270 <main+0xd0>
movl   $0xffffffff,-0x8(%rbp)
lea    -0x40(%rbp),%rsi
lea    0xdb2(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4c(%rbp),%edi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
xorps  %xmm0,%xmm0
movups %xmm0,0x29(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
mov    0x2e3f(%rip),%r14
mov    (%r14),%edi
call   1050 <dup@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1255 <main+0xc5>
mov    0xc(%rsp),%edi
mov    (%r14),%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
lea    0xe11(%rip),%rdi
mov    $0x3,%esi
call   10a0 <permute_letters@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbp
mov    %rbp,%rsi
mov    $0x28,%edx
call   1080 <read@plt>
lea    0xde0(%rip),%rsi
mov    %rbp,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1249 <main+0xb9>
lea    0xde5(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
jmp    124b <main+0xbb>
xor    %ebp,%ebp
mov    (%r14),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@.str = [4 x i8] c"hat\00"
@stdout = global i32 0
@.str.1 = [25 x i8] c"hat\0Ahta\0Aaht\0Aath\0Atha\0Atah\0A\00"
@.str.2 = [57 x i8] c"error, buffer %s not equal to 'hat\0Ahta\0Aaht\0Aath\0Atha\0Atah\0A'\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [41 x i8]
%4 = alloca [2 x i32]
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%6 = bitcast [41 x i8]* %3 to i8*
%7 = load i32, i32* @STDOUT_FILENO
%8 = call i32 @dup(i32 %7)
store i32 %8, i32* %5
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%10 = call i64 @pipe(i32* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %0
store i32 -1, i32* %1
br label %39
13: ; preds = %0
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%15 = load i32, i32* %14
%16 = load i32, i32* @STDOUT_FILENO
%17 = call i32 @dup2(i32 %15, i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32 @close(i32 %19)
%21 = call i32 @permute_letters(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%22 = load i32, i32* @stdout
%23 = call i32 @fflush(i32 %22)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%25 = load i32, i32* %24
%26 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 40)
%28 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 0
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %13
store i32 -1, i32* %2
%32 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 0
%33 = call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %31, %13
%35 = load i32, i32* %5
%36 = load i32, i32* @STDOUT_FILENO
%37 = call i32 @dup2(i32 %35, i32 %36)
%38 = load i32, i32* %2
store i32 %38, i32* %1
br label %39
39: ; preds = %34, %12
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [4 x i8] c"hat\00"
@stdout = global i32 0
@.str.1 = [25 x i8] c"hat\0Ahta\0Aaht\0Aath\0Atha\0Atah\0A\00"
@.str.2 = [57 x i8] c"error, buffer %s not equal to 'hat\0Ahta\0Aaht\0Aath\0Atha\0Atah\0A'\00"
define i32 @main() {
%1 = alloca [41 x i8]
%2 = alloca [2 x i32]
%3 = getelementptr inbounds [41 x i8], [41 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 @permute_letters(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%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 40)
%22 = call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([25 x i8], [25 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 ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i8* %3)
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
}