#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*/ dup2(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
void testpipe() {
  char string[] = "Hello, world!\n";
  char readbuffer[80];
  int pipefd[2];
  pipe(pipefd);
  int pid = fork();
  if (pid == 0) {
    printf("child \n");
    dup2(pipefd[0], 0);
    scanf("%s", readbuffer);
    printf("Received string: %s", readbuffer);
    exit(0);
  } else {
    dup2(pipefd[1], 1);
    printf("%s \n", string);
  }
}


x86 assembly (clang, -O0):
<testpipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0xe91(%rip),%rax
mov    %rax,-0xf(%rbp)
mov    0xe8f(%rip),%eax
mov    %eax,-0x7(%rbp)
mov    0xe89(%rip),%ax
mov    %ax,-0x3(%rbp)
mov    0xe81(%rip),%al
mov    %al,-0x1(%rbp)
lea    -0x68(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
jne    11ee <testpipe+0x8e>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x68(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
lea    -0x60(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   1060 <scanf@plt>
lea    -0x60(%rbp),%rsi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x64(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
lea    -0xf(%rbp),%rsi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testpipe>:
push   %rbx
sub    $0x70,%rsp
movabs $0xa21646c726f77,%rax
mov    %rax,0x17(%rsp)
movabs $0x77202c6f6c6c6548,%rax
2c 20 77
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11cf <testpipe+0x5f>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
lea    0xe66(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x70,%rsp
pop    %rbx
ret
lea    0xe52(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
lea    0xe33(%rip),%rdi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
call   1070 <scanf@plt>
lea    0xe0e(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@__const.testpipe.string = [15 x i8] c"Hello, world!\0A\00"
@.str = [8 x i8] c"child \0A\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [20 x i8] c"Received string: %s\00"
@.str.3 = [5 x i8] c"%s \0A\00"
define void @testpipe() {
%1 = alloca [15 x i8]
%2 = alloca [80 x i8]
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = bitcast [15 x i8]* %1 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = call i32 (...) @fork()
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%14 = load i32, i32* %13
%15 = call i32 @dup2(i32 %14, i32 0)
%16 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%17 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %0
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @dup2(i32 %23, i32 1)
%25 = getelementptr inbounds [15 x i8], [15 x i8]* %1, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %21
ret void
}

LLVM IR (-O2):
@__const.testpipe.string = [15 x i8] c"Hello, world!\0A\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [20 x i8] c"Received string: %s\00"
@.str.3 = [5 x i8] c"%s \0A\00"
@str = [7 x i8] c"child \00"
define void @testpipe() {
%1 = alloca [15 x i8]
%2 = alloca [80 x i8]
%3 = alloca [2 x i32]
%4 = getelementptr inbounds [15 x i8], [15 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%6 = bitcast [2 x i32]* %3 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i32 (...) @fork()
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %0
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%13 = load i32, i32* %7
%14 = call i32 @dup2(i32 %13, i32 0)
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %0
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @dup2(i32 %20, i32 1)
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %4)
ret void
}