#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 size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ read(int, void *, size_t);
size_t write(int, void *, size_t);
ssize_t read_kern(void *writebuf, void *readbuf, size_t count) {
  int pipefd[2];
  ssize_t len;
  pipe(pipefd);
  len = write(pipefd[1], writebuf, count);
  if (len != count) {
    perror("write");
  }
  read(pipefd[0], readbuf, count);
  close(pipefd[0]);
  close(pipefd[1]);
  return len;
}


x86 assembly (clang, -O0):
<read_kern>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     119b <read_kern+0x4b>
lea    0xe6a(%rip),%rdi
call   1070 <perror@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <read@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_kern>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
mov    %rax,%r14
cmp    %rbx,%rax
jne    11b1 <read_kern+0x61>
mov    0x8(%rsp),%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1060 <read@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe48(%rip),%rdi
call   1070 <perror@plt>
jmp    1183 <read_kern+0x33>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
define i64 @read_kern(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [2 x i32]
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%12 = load i32, i32* %11
%13 = load i8*, i8** %4
%14 = load i64, i64* %6
%15 = call i64 @write(i32 %12, i8* %13, i64 %14)
store i64 %15, i64* %8
%16 = load i64, i64* %8
%17 = load i64, i64* %6
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %3
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%23 = load i32, i32* %22
%24 = load i8*, i8** %5
%25 = load i64, i64* %6
%26 = call i32 @read(i32 %23, i8* %24, i64 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = load i64, i64* %8
ret i64 %33
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
define i64 @read_kern(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%9 = load i32, i32* %8
%10 = call i64 @write(i32 %9, i8* %0, i64 %2)
%11 = icmp eq i64 %10, %2
br i1 %11, label %14, label %12
12: ; preds = %3
%13 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %3
%15 = load i32, i32* %6
%16 = call i32 @read(i32 %15, i8* %1, i64 %2)
%17 = load i32, i32* %6
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* %8
%20 = call i32 @close(i32 %19)
ret i64 %10
}