#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 */
typedef int /*<<< orphan*/ readbuffer;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
void pipetest1() {
  printf("inside pipetest \n");
  int fd[2];
  char string[] = "Hello, world!\n";
  char readbuffer[80];
  pipe(fd);
  printf("pipe fds : %d  %d \n", fd[0], fd[1]);
  int pid = fork();
  if (pid == 0) {
    printf("inside cchild \n");
    write(fd[1], string, (strlen(string) + 1));
    close(fd[0]);
    exit(0);
  }
  read(fd[0], readbuffer, sizeof(readbuffer));
  printf("Received string: %s", readbuffer);
}


x86 assembly (clang, -O0):
<pipetest1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0xe72(%rip),%rax
mov    %rax,-0x17(%rbp)
mov    0xe70(%rip),%eax
mov    %eax,-0xf(%rbp)
mov    0xe6a(%rip),%ax
mov    %ax,-0xb(%rbp)
mov    0xe62(%rip),%al
mov    %al,-0x9(%rbp)
lea    -0x8(%rbp),%rdi
call   1070 <pipe@plt>
mov    -0x8(%rbp),%esi
mov    -0x4(%rbp),%edx
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jne    123f <pipetest1+0xbf>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x84(%rbp)
lea    -0x17(%rbp),%rax
mov    %rax,-0x80(%rbp)
lea    -0x17(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
call   1080 <read@plt>
lea    -0x70(%rbp),%rsi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipetest1>:
push   %r14
push   %rbx
sub    $0x78,%rsp
lea    0xe99(%rip),%rdi
call   1030 <puts@plt>
movabs $0xa21646c726f77,%rax
mov    %rax,0x17(%rsp)
movabs $0x77202c6f6c6c6548,%rax
2c 20 77
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0xe35(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     121b <pipetest1+0x8b>
mov    0x8(%rsp),%edi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x50,%edx
call   1090 <read@plt>
lea    0xe1a(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe26(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <write@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"inside pipetest \0A\00"
@__const.pipetest1.string = [15 x i8] c"Hello, world!\0A\00"
@.str.1 = [20 x i8] c"pipe fds : %d %d \0A\00"
@.str.2 = [16 x i8] c"inside cchild \0A\00"
@.str.3 = [20 x i8] c"Received string: %s\00"
define void @pipetest1() {
%1 = alloca [2 x i32]
%2 = alloca [15 x i8]
%3 = alloca [80 x i8]
%4 = alloca i32
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%6 = bitcast [15 x i8]* %2 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%10 = load i32, i32* %9
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%12 = load i32, i32* %11
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %12)
%14 = call i32 (...) @fork()
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %30
17: ; preds = %0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds [15 x i8], [15 x i8]* %2, i64 0, i64 0
%22 = getelementptr inbounds [15 x i8], [15 x i8]* %2, i64 0, i64 0
%23 = call i64 @strlen(i8* %22)
%24 = add nsw i64 %23, 1
%25 = call i32 @write(i32 %20, i8* %21, i64 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %0
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%32 = load i32, i32* %31
%33 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%34 = call i32 @read(i32 %32, i8* %33, i32 80)
%35 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %35)
ret void
}

LLVM IR (-O2):
@__const.pipetest1.string = [15 x i8] c"Hello, world!\0A\00"
@.str.1 = [20 x i8] c"pipe fds : %d %d \0A\00"
@.str.3 = [20 x i8] c"Received string: %s\00"
@str = [17 x i8] c"inside pipetest \00"
@str.4 = [15 x i8] c"inside cchild \00"
define void @pipetest1() {
%1 = alloca [2 x i32]
%2 = alloca [15 x i8]
%3 = alloca [80 x i8]
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%5 = bitcast [2 x i32]* %1 to i8*
%6 = getelementptr inbounds [15 x i8], [15 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = load i32, i32* %8
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%12 = load i32, i32* %11
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %12)
%14 = call i32 (...) @fork()
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %25
16: ; preds = %0
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.4, i64 0, i64 0))
%18 = load i32, i32* %11
%19 = call i64 @strlen(i8* %6)
%20 = add nsw i64 %19, 1
%21 = call i32 @write(i32 %18, i8* %6, i64 %20)
%22 = load i32, i32* %8
%23 = call i32 @close(i32 %22)
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %0
%26 = load i32, i32* %8
%27 = call i32 @read(i32 %26, i8* %7, i32 80)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %7)
ret void
}