#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*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int n;
  int fd[2];
  char buf[1025];
  char *data = "hi this is a test";
  pipe(fd);
  write(fd[1], data, strlen(data));
  if ((n = read(fd[0], buf, 1024)) >= 0) {
    buf[n] = '\0';
    printf("\nread %d bytes from the pipe : %s", n, buf);
  } else
    printf("\nerror in read !");
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe97(%rip),%rax
mov    %rax,-0x428(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <pipe@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x434(%rbp)
mov    -0x428(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x428(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x434(%rbp),%edi
mov    -0x430(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jl     11f9 <main+0xa9>
movslq -0x8(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x8(%rbp),%esi
lea    -0x420(%rbp),%rdx
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1207 <main+0xb7>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x418,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
mov    0xc(%rsp),%ebx
lea    0xe81(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
js     11c8 <main+0x68>
mov    %eax,%ecx
movb   $0x0,0x10(%rsp,%rcx,1)
lea    0xe5a(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11d6 <main+0x76>
lea    0xe65(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [18 x i8] c"hi this is a test\00"
@.str.1 = [34 x i8] c"\0Aread %d bytes from the pipe : %s\00"
@.str.2 = [17 x i8] c"\0Aerror in read !\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca [1025 x i8]
%5 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%9 = load i32, i32* %8
%10 = load i8*, i8** %5
%11 = load i8*, i8** %5
%12 = call i32 @strlen(i8* %11)
%13 = call i32 @write(i32 %9, i8* %10, i32 %12)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 1024)
store i32 %17, i32* %2
%18 = icmp sge i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %0
%20 = load i32, i32* %2
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 %21
store i8 0, i8* %22
%23 = load i32, i32* %2
%24 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %23, i8* %24)
br label %28
26: ; preds = %0
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %19
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [18 x i8] c"hi this is a test\00"
@.str.1 = [34 x i8] c"\0Aread %d bytes from the pipe : %s\00"
@.str.2 = [17 x i8] c"\0Aerror in read !\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [1025 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%8 = load i32, i32* %7
%9 = call i32 @strlen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @write(i32 %8, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %5
%12 = call i32 @read(i32 %11, i8* %4, i32 1024)
%13 = icmp sgt i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = zext i32 %12 to i64
%16 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 %15
store i8 0, i8* %16
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %12, i8* %4)
br label %20
18: ; preds = %0
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = call i32 @putchar(i32 10)
ret i32 0
}