#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*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(void) {
  int fd[2];
  pipe(fd);
  if (dup2(fd[1], STDOUT_FILENO) == -1)
    perror("dup2");
  if (dup2(fd[0], STDIN_FILENO) == -1)
    perror("dup2");
  printf("Hi\n");
  fflush(stdout);
  char byte[1];
  while (read(STDIN_FILENO, byte, 1)) {
    write(STDOUT_FILENO, byte, 1);
    fwrite(byte, 1, 1, stderr);
  }
  close(fd[0]);
  close(fd[1]);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
mov    -0x8(%rbp),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11ce <main+0x3e>
lea    0xe37(%rip),%rdi
call   10a0 <perror@plt>
mov    -0xc(%rbp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    11f4 <main+0x64>
lea    0xe11(%rip),%rdi
call   10a0 <perror@plt>
lea    0xe0a(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0x0,%rax
je     1264 <main+0xd4>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    -0xd(%rbp),%rdi
mov    0x2d87(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
mov    %edx,%esi
call   10b0 <fwrite@plt>
jmp    120e <main+0x7e>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
mov    0x14(%rsp),%edi
mov    0x2e40(%rip),%r14
mov    (%r14),%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     127b <main+0xeb>
mov    0x10(%rsp),%edi
mov    0x2dfc(%rip),%r15
mov    (%r15),%esi
call   1050 <dup2@plt>
cmp    $0xffffffff,%eax
je     128c <main+0xfc>
lea    0xe21(%rip),%rdi
call   1030 <puts@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    (%r15),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
je     125b <main+0xcb>
lea    0xf(%rsp),%rbx
mov    0x2dbe(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    (%r12),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
jne    1220 <main+0x90>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0x14(%rsp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd7e(%rip),%rdi
call   10a0 <perror@plt>
jmp    11c1 <main+0x31>
lea    0xd6d(%rip),%rdi
call   10a0 <perror@plt>
jmp    11dd <main+0x4d>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@.str = [5 x i8] c"dup2\00"
@STDIN_FILENO = global i32 0
@.str.1 = [4 x i8] c"Hi\0A\00"
@stdout = global i32 0
@stderr = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [1 x i8]
store i32 0, i32* %1
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%7 = load i32, i32* %6
%8 = load i32, i32* @STDOUT_FILENO
%9 = call i32 @dup2(i32 %7, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%15 = load i32, i32* %14
%16 = load i32, i32* @STDIN_FILENO
%17 = call i32 @dup2(i32 %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @stdout
%24 = call i32 @fflush(i32 %23)
br label %25
25: ; preds = %30, %21
%26 = load i32, i32* @STDIN_FILENO
%27 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
%28 = call i64 @read(i32 %26, i8* %27, i32 1)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %25
%31 = load i32, i32* @STDOUT_FILENO
%32 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
%33 = call i32 @write(i32 %31, i8* %32, i32 1)
%34 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
%35 = load i32, i32* @stderr
%36 = call i32 @fwrite(i8* %34, i32 1, i32 1, i32 %35)
br label %25
37: ; preds = %25
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [5 x i8] c"dup2\00"
@STDIN_FILENO = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@str = [3 x i8] c"Hi\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [1 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%7 = load i32, i32* %6
%8 = load i32, i32* @STDOUT_FILENO
%9 = call i32 @dup2(i32 %7, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = load i32, i32* %4
%15 = load i32, i32* @STDIN_FILENO
%16 = call i32 @dup2(i32 %14, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
%22 = load i32, i32* @stdout
%23 = call i32 @fflush(i32 %22)
%24 = getelementptr inbounds [1 x i8], [1 x i8]* %2, i64 0, i64 0
%25 = load i32, i32* @STDIN_FILENO
%26 = call i64 @read(i32 %25, i8* %24, i32 1)
%27 = icmp eq i64 %26, 0
br i1 %27, label %36, label %28
28: ; preds = %20, %28
%29 = load i32, i32* @STDOUT_FILENO
%30 = call i32 @write(i32 %29, i8* %24, i32 1)
%31 = load i32, i32* @stderr
%32 = call i32 @fwrite(i8* %24, i32 1, i32 1, i32 %31)
%33 = load i32, i32* @STDIN_FILENO
%34 = call i64 @read(i32 %33, i8* %24, i32 1)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %28
36: ; preds = %28, %20
%37 = load i32, i32* %4
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %6
%40 = call i32 @close(i32 %39)
ret i32 0
}