#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int);
int strlen(char *);
int write(int, char *, int);
int main() {
  int rc, filep[2], length;
  char *in_data = "pdat";
  char *out_data;
  length = strlen(in_data) + 1;
  out_data = malloc(length);
  memset(out_data, 0, length);
  if (pipe(filep) == 0) {
    rc = write(filep[1], in_data, 4);
    printf("wrote %d bytes\n", rc);
    rc = read(filep[0], out_data, 4);
    printf("read %d bytes which were %s\n", rc, out_data);
    exit(EXIT_SUCCESS);
  }
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    -0x10(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jne    1232 <main+0xb2>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xe76(%rip),%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%ebp
mov    %ebp,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1060 <memset@plt>
mov    %rsp,%rdi
call   1070 <pipe@plt>
test   %rax,%rax
jne    120b <main+0x8b>
mov    0x4(%rsp),%edi
lea    0xe40(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
lea    0xe34(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
lea    0xe24(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pdat\00"
@.str.1 = [16 x i8] c"wrote %d bytes\0A\00"
@.str.2 = [29 x i8] c"read %d bytes which were %s\0A\00"
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %5
%7 = load i8*, i8** %5
%8 = call i32 @strlen(i8* %7)
%9 = add nsw i32 %8, 1
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = load i32, i32* %4
%14 = call i32 @memset(i8* %12, i32 0, i32 %13)
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%16 = call i64 @pipe(i32* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %34
18: ; preds = %0
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%20 = load i32, i32* %19
%21 = load i8*, i8** %5
%22 = call i32 @write(i32 %20, i8* %21, i32 4)
store i32 %22, i32* %2
%23 = load i32, i32* %2
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%26 = load i32, i32* %25
%27 = load i8*, i8** %6
%28 = call i32 @read(i32 %26, i8* %27, i32 4)
store i32 %28, i32* %2
%29 = load i32, i32* %2
%30 = load i8*, i8** %6
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %29, i8* %30)
%32 = load i32, i32* @EXIT_SUCCESS
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %0
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
}

LLVM IR (-O2):
@.str = [5 x i8] c"pdat\00"
@.str.1 = [16 x i8] c"wrote %d bytes\0A\00"
@.str.2 = [29 x i8] c"read %d bytes which were %s\0A\00"
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = add nsw i32 %3, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = tail call i32 @memset(i8* %5, i32 0, i32 %4)
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %0
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%12 = load i32, i32* %11
%13 = call i32 @write(i32 %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%14 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %7
%16 = call i32 @read(i32 %15, i8* %5, i32 4)
%17 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %16, i8* %5)
%18 = load i32, i32* @EXIT_SUCCESS
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %0
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
}