#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 *);
size_t read(int, void *, size_t);
int /*<<< orphan*/ write(int, void *, size_t);
ssize_t write_kern(void *readbuf, void *writebuf, size_t count) {
  int pipefd[2];
  ssize_t len;
  pipe(pipefd);
  write(pipefd[1], writebuf, count);
  len = read(pipefd[0], readbuf, count);
  if (len != count) {
    perror("read");
  }
  close(pipefd[0]);
  close(pipefd[1]);
  return len;
}


x86 assembly (clang, -O0):
<write_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    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    -0x20(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <read@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11ab <write_kern+0x5b>
lea    0xe5a(%rip),%rdi
call   1070 <perror@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):
<write_kern>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
mov    0x8(%rsp),%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1060 <read@plt>
mov    %rax,%r14
cmp    %rbx,%rax
jne    11b1 <write_kern+0x61>
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    1192 <write_kern+0x42>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
define i64 @write_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** %5
%14 = load i64, i64* %6
%15 = call i32 @write(i32 %12, i8* %13, i64 %14)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%17 = load i32, i32* %16
%18 = load i8*, i8** %4
%19 = load i64, i64* %6
%20 = call i64 @read(i32 %17, i8* %18, i64 %19)
store i64 %20, i64* %8
%21 = load i64, i64* %8
%22 = load i64, i64* %6
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %3
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %24, %3
%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 = [5 x i8] c"read\00"
define i64 @write_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 i32 @write(i32 %9, i8* %1, i64 %2)
%11 = load i32, i32* %6
%12 = call i64 @read(i32 %11, i8* %0, i64 %2)
%13 = icmp eq i64 %12, %2
br i1 %13, label %16, label %14
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
%17 = load i32, i32* %6
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* %8
%20 = call i32 @close(i32 %19)
ret i64 %12
}