#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ feof(int /*<<< orphan*/);
scalar_t__ ferror(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/);
size_t fwrite(unsigned char *, int, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
size_t copy_kernel(void) {
  unsigned char buf[1024];
  size_t n;
  size_t count = 0;
  while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) {
    if (fwrite(buf, 1, n, stdout) != n) {
      perror("copy_kernel write");
      exit(1);
    }
    count += n;
  }
  if (!feof(stdin) || ferror(stdin)) {
    perror("copy_kernel read");
    exit(1);
  }
  fflush(stdout);
  return count;
}


x86 assembly (clang, -O0):
<copy_kernel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movq   $0x0,-0x410(%rbp)
lea    -0x400(%rbp),%rdi
mov    0x2e54(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %rax,-0x408(%rbp)
cmp    $0x0,%rax
jbe    1214 <copy_kernel+0xa4>
lea    -0x400(%rbp),%rdi
mov    -0x408(%rbp),%rdx
mov    0x2e0d(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1090 <fwrite@plt>
cmp    -0x408(%rbp),%rax
je     11fa <copy_kernel+0x8a>
lea    0xe15(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x408(%rbp),%rax
add    -0x410(%rbp),%rax
mov    %rax,-0x410(%rbp)
jmp    1186 <copy_kernel+0x16>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1050 <feof@plt>
cmp    $0x0,%eax
je     1243 <copy_kernel+0xd3>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1259 <copy_kernel+0xe9>
lea    0xdc8(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    -0x410(%rbp),%rax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_kernel>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    0x2e61(%rip),%r12
mov    (%r12),%ecx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
test   %rax,%rax
je     11fc <copy_kernel+0x8c>
mov    %rax,%rbx
xor    %r15d,%r15d
mov    0x2e29(%rip),%r13
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%r13),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
call   1090 <fwrite@plt>
cmp    %rbx,%rax
jne    123c <copy_kernel+0xcc>
add    %rbx,%r15
mov    (%r12),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c0 <copy_kernel+0x50>
jmp    11ff <copy_kernel+0x8f>
xor    %r15d,%r15d
mov    (%r12),%edi
call   1050 <feof@plt>
test   %eax,%eax
je     1252 <copy_kernel+0xe2>
mov    (%r12),%edi
call   1030 <ferror@plt>
test   %rax,%rax
jne    1252 <copy_kernel+0xe2>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    %r15,%rax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xdbd(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdb9(%rip),%rdi
jmp    1243 <copy_kernel+0xd3>

LLVM IR (-O0):
@stdin = global i32 0
@stdout = global i32 0
@.str = [18 x i8] c"copy_kernel write\00"
@.str.1 = [17 x i8] c"copy_kernel read\00"
define i64 @copy_kernel() {
%1 = alloca [1024 x i8]
%2 = alloca i64
%3 = alloca i64
store i64 0, i64* %3
br label %4
4: ; preds = %19, %0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%6 = load i32, i32* @stdin
%7 = call i64 @fread(i8* %5, i32 1, i32 1024, i32 %6)
store i64 %7, i64* %2
%8 = icmp ugt i64 %7, 0
br i1 %8, label %9, label %23
9: ; preds = %4
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%11 = load i64, i64* %2
%12 = load i32, i32* @stdout
%13 = call i64 @fwrite(i8* %10, i32 1, i64 %11, i32 %12)
%14 = load i64, i64* %2
%15 = icmp ne i64 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %9
%17 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %9
%20 = load i64, i64* %2
%21 = load i64, i64* %3
%22 = add i64 %21, %20
store i64 %22, i64* %3
br label %4
23: ; preds = %4
%24 = load i32, i32* @stdin
%25 = call i32 @feof(i32 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* @stdin
%29 = call i64 @ferror(i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27, %23
%32 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = load i32, i32* @stdout
%36 = call i32 @fflush(i32 %35)
%37 = load i64, i64* %3
ret i64 %37
}

LLVM IR (-O2):
@stdin = global i32 0
@stdout = global i32 0
@.str = [18 x i8] c"copy_kernel write\00"
@.str.1 = [17 x i8] c"copy_kernel read\00"
define i64 @copy_kernel() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stdin
%4 = call i64 @fread(i8* %2, i32 1, i32 1024, i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %20, label %10
6: ; preds = %10
%7 = load i32, i32* @stdin
%8 = call i64 @fread(i8* %2, i32 1, i32 1024, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %0, %6
%11 = phi i64 [ %8, %6 ], [ %4, %0 ]
%12 = phi i64 [ %16, %6 ], [ 0, %0 ]
%13 = load i32, i32* @stdout
%14 = call i64 @fwrite(i8* %2, i32 1, i64 %11, i32 %13)
%15 = icmp eq i64 %14, %11
%16 = add i64 %11, %12
br i1 %15, label %6, label %17
17: ; preds = %10
%18 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %6, %0
%21 = phi i64 [ 0, %0 ], [ %16, %6 ]
%22 = load i32, i32* @stdin
%23 = call i32 @feof(i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %20
%26 = load i32, i32* @stdin
%27 = call i64 @ferror(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %25, %20
%30 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25
%33 = load i32, i32* @stdout
%34 = call i32 @fflush(i32 %33)
ret i64 %21
}