#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*/ Close(int);
scalar_t__ Malloc(size_t const);
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int Open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ Pipe(int *);
int /*<<< orphan*/ Read(int, char *, size_t const);
int /*<<< orphan*/ Write(int, char *, size_t const);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ free(char *);
scalar_t__ memcmp(char *, char *, size_t const);
void test_rdwr() {
  const size_t buffer_size = 40000;
  char *buffer = (char *)Malloc(buffer_size);
  int rfd = Open("/dev/urandom", O_RDONLY);
  Read(rfd, buffer, buffer_size);
  Close(rfd);
  int dnfd = Open("/dev/null", O_WRONLY);
  Write(dnfd, buffer, buffer_size);
  Close(dnfd);
  int rdwr[2];
  Pipe(rdwr);
  Write(rdwr[1], buffer, buffer_size);
  char *buffer2 = (char *)Malloc(buffer_size);
  Read(rdwr[0], buffer2, buffer_size);
  assert(memcmp(buffer, buffer2, buffer_size) == 0);
  Close(rdwr[0]);
  Close(rdwr[1]);
  free(buffer);
  free(buffer2);
}


x86 assembly (clang, -O0):
<test_rdwr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x9c40,-0x8(%rbp)
mov    $0x9c40,%edi
call   1040 <Malloc@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0xe42(%rip),%rdi
call   1050 <Open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x9c40,%edx
call   1060 <Read@plt>
mov    -0x14(%rbp),%edi
call   10b0 <Close@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    0xe1e(%rip),%rdi
call   1050 <Open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x9c40,%edx
call   1070 <Write@plt>
mov    -0x18(%rbp),%edi
call   10b0 <Close@plt>
lea    -0x20(%rbp),%rdi
call   10a0 <Pipe@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x9c40,%edx
call   1070 <Write@plt>
mov    $0x9c40,%edi
call   1040 <Malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x9c40,%edx
call   1060 <Read@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x9c40,%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x20(%rbp),%edi
call   10b0 <Close@plt>
mov    -0x1c(%rbp),%edi
call   10b0 <Close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_rdwr>:
push   %r14
push   %rbx
push   %rax
mov    $0x9c40,%edi
call   1040 <Malloc@plt>
mov    %rax,%r14
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1050 <Open@plt>
mov    %eax,%ebx
mov    $0x9c40,%edx
mov    %eax,%edi
mov    %r14,%rsi
call   1060 <Read@plt>
mov    %ebx,%edi
call   10b0 <Close@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
lea    0xe2f(%rip),%rdi
call   1050 <Open@plt>
mov    %eax,%ebx
mov    $0x9c40,%edx
mov    %eax,%edi
mov    %r14,%rsi
call   1070 <Write@plt>
mov    %ebx,%edi
call   10b0 <Close@plt>
mov    %rsp,%rdi
call   10a0 <Pipe@plt>
mov    0x4(%rsp),%edi
mov    $0x9c40,%edx
mov    %r14,%rsi
call   1070 <Write@plt>
mov    $0x9c40,%edi
call   1040 <Malloc@plt>
mov    %rax,%rbx
mov    (%rsp),%edi
mov    $0x9c40,%edx
mov    %rax,%rsi
call   1060 <Read@plt>
mov    $0x9c40,%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <memcmp@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1090 <assert@plt>
mov    (%rsp),%edi
call   10b0 <Close@plt>
mov    0x4(%rsp),%edi
call   10b0 <Close@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
define void @test_rdwr() {
%1 = alloca i64
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i32]
%6 = alloca i8*
store i64 40000, i64* %1
%7 = call i64 @Malloc(i64 40000)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %2
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @Open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = load i8*, i8** %2
%13 = call i32 @Read(i32 %11, i8* %12, i64 40000)
%14 = load i32, i32* %3
%15 = call i32 @Close(i32 %14)
%16 = load i32, i32* @O_WRONLY
%17 = call i32 @Open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = load i8*, i8** %2
%20 = call i32 @Write(i32 %18, i8* %19, i64 40000)
%21 = load i32, i32* %4
%22 = call i32 @Close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%24 = call i32 @Pipe(i32* %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%26 = load i32, i32* %25
%27 = load i8*, i8** %2
%28 = call i32 @Write(i32 %26, i8* %27, i64 40000)
%29 = call i64 @Malloc(i64 40000)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %6
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%32 = load i32, i32* %31
%33 = load i8*, i8** %6
%34 = call i32 @Read(i32 %32, i8* %33, i64 40000)
%35 = load i8*, i8** %2
%36 = load i8*, i8** %6
%37 = call i64 @memcmp(i8* %35, i8* %36, i64 40000)
%38 = icmp eq i64 %37, 0
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i32 @Close(i32 %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @Close(i32 %45)
%47 = load i8*, i8** %2
%48 = call i32 @free(i8* %47)
%49 = load i8*, i8** %6
%50 = call i32 @free(i8* %49)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
define void @test_rdwr() {
%1 = alloca [2 x i32]
%2 = tail call i64 @Malloc(i64 40000)
%3 = inttoptr i64 %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @Open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 @Read(i32 %5, i8* %3, i64 40000)
%7 = tail call i32 @Close(i32 %5)
%8 = load i32, i32* @O_WRONLY
%9 = tail call i32 @Open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = tail call i32 @Write(i32 %9, i8* %3, i64 40000)
%11 = tail call i32 @Close(i32 %9)
%12 = bitcast [2 x i32]* %1 to i8*
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%14 = call i32 @Pipe(i32* %13)
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%16 = load i32, i32* %15
%17 = call i32 @Write(i32 %16, i8* %3, i64 40000)
%18 = call i64 @Malloc(i64 40000)
%19 = inttoptr i64 %18 to i8*
%20 = load i32, i32* %13
%21 = call i32 @Read(i32 %20, i8* %19, i64 40000)
%22 = call i64 @memcmp(i8* %3, i8* %19, i64 40000)
%23 = icmp eq i64 %22, 0
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = load i32, i32* %13
%27 = call i32 @Close(i32 %26)
%28 = load i32, i32* %15
%29 = call i32 @Close(i32 %28)
%30 = call i32 @free(i8* %3)
%31 = call i32 @free(i8* %19)
ret void
}