#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 uintmax_t;
typedef size_t off_t;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ err(int, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
void head_bytes(FILE *fp, off_t cnt) {
  char buf[4096];
  size_t readlen;
  while (cnt) {
    if ((uintmax_t)cnt < sizeof(buf))
      readlen = cnt;
    else
      readlen = sizeof(buf);
    readlen = fread(buf, sizeof(char), readlen, fp);
    if (readlen == 0)
      break;
    if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
      err(1, "stdout");
    cnt -= readlen;
  }
}


x86 assembly (clang, -O0):
<head_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1207 <head_bytes+0xd7>
mov    -0x10(%rbp),%rax
cltq
cmp    $0x1000,%rax
jae    1170 <head_bytes+0x40>
mov    -0x10(%rbp),%rax
mov    %rax,-0x1018(%rbp)
jmp    117b <head_bytes+0x4b>
movq   $0x1000,-0x1018(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x1018(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
jne    11b1 <head_bytes+0x81>
jmp    1207 <head_bytes+0xd7>
lea    -0x1010(%rbp),%rdi
mov    -0x1018(%rbp),%rdx
mov    0x2e1a(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1050 <fwrite@plt>
cmp    -0x1018(%rbp),%rax
je     11f0 <head_bytes+0xc0>
mov    $0x1,%edi
lea    0xe15(%rip),%rsi
call   1040 <err@plt>
mov    -0x1018(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1143 <head_bytes+0x13>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<head_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
test   %rsi,%rsi
je     11b4 <head_bytes+0x84>
mov    %rsi,%r13
mov    %rdi,%r14
mov    %rsp,%r15
mov    0x2e8a(%rip),%rbp
lea    0xea3(%rip),%r12
jmp    1165 <head_bytes+0x35>
nop
sub    %rbx,%r13
je     11b4 <head_bytes+0x84>
test   $0xfffff000,%r13d
mov    $0x1000,%edx
cmove  %r13,%rdx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11b4 <head_bytes+0x84>
mov    %rax,%rbx
mov    0x0(%rbp),%ecx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
call   1050 <fwrite@plt>
cmp    %rbx,%rax
je     1160 <head_bytes+0x30>
mov    $0x1,%edi
mov    %r12,%rsi
call   1040 <err@plt>
jmp    1160 <head_bytes+0x30>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [7 x i8] c"stdout\00"
define void @head_bytes(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca [4096 x i8]
%6 = alloca i64
store i32* %0, i32** %3
store i64 %1, i64* %4
br label %7
7: ; preds = %35, %2
%8 = load i64, i64* %4
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %39
10: ; preds = %7
%11 = load i64, i64* %4
%12 = trunc i64 %11 to i32
%13 = sext i32 %12 to i64
%14 = icmp ult i64 %13, 4096
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = load i64, i64* %4
store i64 %16, i64* %6
br label %18
17: ; preds = %10
store i64 4096, i64* %6
br label %18
18: ; preds = %17, %15
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%20 = load i64, i64* %6
%21 = load i32*, i32** %3
%22 = call i64 @fread(i8* %19, i32 1, i64 %20, i32* %21)
store i64 %22, i64* %6
%23 = load i64, i64* %6
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %18
br label %39
26: ; preds = %18
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%28 = load i64, i64* %6
%29 = load i32, i32* @stdout
%30 = call i64 @fwrite(i8* %27, i32 1, i64 %28, i32 %29)
%31 = load i64, i64* %6
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %26
%34 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %35
35: ; preds = %33, %26
%36 = load i64, i64* %6
%37 = load i64, i64* %4
%38 = sub i64 %37, %36
store i64 %38, i64* %4
br label %7
39: ; preds = %25, %7
ret void
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [7 x i8] c"stdout\00"
define void @head_bytes(i32* nocapture %0, i64 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp eq i64 %1, 0
br i1 %5, label %22, label %6
6: ; preds = %2, %19
%7 = phi i64 [ %20, %19 ], [ %1, %2 ]
%8 = and i64 %7, 4294963200
%9 = icmp eq i64 %8, 0
%10 = select i1 %9, i64 %7, i64 4096
%11 = call i64 @fread(i8* %4, i32 1, i64 %10, i32* %0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %22, label %13
13: ; preds = %6
%14 = load i32, i32* @stdout
%15 = call i64 @fwrite(i8* %4, i32 1, i64 %11, i32 %14)
%16 = icmp eq i64 %15, %11
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %13
%20 = sub i64 %7, %11
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %6
22: ; preds = %19, %6, %2
ret void
}