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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, char const *);
scalar_t__ feof(int /*<<< orphan*/ *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
size_t write(int /*<<< orphan*/, char *, size_t);
void emit(FILE *fd, const char *filename, size_t ct) {
  char buf[BUFSIZ];
  while (ct) {
    size_t len, cc;
    cc = ct > sizeof(buf) ? sizeof(buf) : ct;
    len = fread(buf, 1, cc, fd);
    if (!len)
      break;
    ct -= len;
    cc = write(STDOUT_FILENO, buf, len);
    if (cc != len)
      err(EXIT_FAILURE, ("write to stdout failed"));
  }
  if (!ct)
    return;
  if (feof(fd))
    errx(EXIT_FAILURE, ("unexpected end of file on %s"), filename);
  err(EXIT_FAILURE, ("failed to read typescript file %s"), filename);
}


x86 assembly (clang, -O0):
<emit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1247 <emit+0xf7>
mov    -0x50(%rbp),%rax
cmp    %rax,-0x18(%rbp)
jbe    11ba <emit+0x6a>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    11c2 <emit+0x72>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f4 <emit+0xa4>
jmp    1247 <emit+0xf7>
mov    -0x48(%rbp),%rsi
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1242 <emit+0xf2>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
jmp    1194 <emit+0x44>
cmpq   $0x0,-0x18(%rbp)
jne    125e <emit+0x10e>
movl   $0x1,-0x3c(%rbp)
jmp    12ac <emit+0x15c>
mov    -0x8(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     128a <emit+0x13a>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd92(%rip),%rsi
call   1050 <errx@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd96(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<emit>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,-0x30(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%r12
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
test   %rdx,%rdx
je     11ec <emit+0x9c>
mov    %rdx,%rbx
mov    %rdi,%r15
jmp    1195 <emit+0x45>
nopw   0x0(%rax,%rax,1)
test   %rbx,%rbx
je     11ec <emit+0x9c>
cmp    %r13,%rbx
mov    %rbx,%rdx
cmova  %r13,%rdx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11fb <emit+0xab>
mov    %rax,%r14
sub    %rax,%rbx
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
mov    %r14,%rdx
call   1040 <write@plt>
cmp    %r14,%rax
je     1190 <emit+0x40>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
xor    %eax,%eax
call   1070 <err@plt>
jmp    1190 <emit+0x40>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1060 <feof@plt>
test   %rax,%rax
mov    -0x30(%rbp),%rbx
mov    0x2dbf(%rip),%r14
je     1225 <emit+0xd5>
mov    (%r14),%edi
lea    0xdfa(%rip),%rsi
mov    %rbx,%rdx
call   1050 <errx@plt>
mov    (%r14),%edi
lea    0xe05(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <err@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@STDOUT_FILENO = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [23 x i8] c"write to stdout failed\00"
@.str.1 = [29 x i8] c"unexpected end of file on %s\00"
@.str.2 = [34 x i8] c"failed to read typescript file %s\00"
define void @emit(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%12 = load i32, i32* @BUFSIZ
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %7
%15 = alloca i8, i64 %13
store i64 %13, i64* %8
br label %16
16: ; preds = %46, %3
%17 = load i64, i64* %6
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %47
19: ; preds = %16
%20 = load i64, i64* %6
%21 = icmp ugt i64 %20, %13
br i1 %21, label %22, label %23
22: ; preds = %19
br label %25
23: ; preds = %19
%24 = load i64, i64* %6
br label %25
25: ; preds = %23, %22
%26 = phi i64 [ %13, %22 ], [ %24, %23 ]
store i64 %26, i64* %10
%27 = load i64, i64* %10
%28 = load i32*, i32** %4
%29 = call i64 @fread(i8* %15, i32 1, i64 %27, i32* %28)
store i64 %29, i64* %9
%30 = load i64, i64* %9
%31 = icmp ne i64 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %25
br label %47
33: ; preds = %25
%34 = load i64, i64* %9
%35 = load i64, i64* %6
%36 = sub i64 %35, %34
store i64 %36, i64* %6
%37 = load i32, i32* @STDOUT_FILENO
%38 = load i64, i64* %9
%39 = call i64 @write(i32 %37, i8* %15, i64 %38)
store i64 %39, i64* %10
%40 = load i64, i64* %10
%41 = load i64, i64* %9
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %46
43: ; preds = %33
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 (i32, i8*, ...) @err(i32 %44, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %46
46: ; preds = %43, %33
br label %16
47: ; preds = %32, %16
%48 = load i64, i64* %6
%49 = icmp ne i64 %48, 0
br i1 %49, label %51, label %50
50: ; preds = %47
store i32 1, i32* %11
br label %63
51: ; preds = %47
%52 = load i32*, i32** %4
%53 = call i64 @feof(i32* %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %51
%56 = load i32, i32* @EXIT_FAILURE
%57 = load i8*, i8** %5
%58 = call i32 @errx(i32 %56, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %57)
br label %59
59: ; preds = %55, %51
%60 = load i32, i32* @EXIT_FAILURE
%61 = load i8*, i8** %5
%62 = call i32 (i32, i8*, ...) @err(i32 %60, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %61)
store i32 0, i32* %11
br label %63
63: ; preds = %59, %50
%64 = load i8*, i8** %7
%65 = load i32, i32* %11
switch i32 %65, label %67 [
i32 0, label %66
i32 1, label %66
]
66: ; preds = %63, %63
ret void
67: ; preds = %63
unreachable
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@STDOUT_FILENO = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [23 x i8] c"write to stdout failed\00"
@.str.1 = [29 x i8] c"unexpected end of file on %s\00"
@.str.2 = [34 x i8] c"failed to read typescript file %s\00"
define void @emit(i32* nocapture %0, i8* %1, i64 %2) {
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = icmp eq i64 %2, 0
br i1 %7, label %33, label %8
8: ; preds = %3, %22
%9 = phi i64 [ %15, %22 ], [ %2, %3 ]
%10 = icmp ugt i64 %9, %5
%11 = select i1 %10, i64 %5, i64 %9
%12 = call i64 @fread(i8* %6, i32 1, i64 %11, i32* %0)
%13 = icmp eq i64 %12, 0
br i1 %13, label %24, label %14
14: ; preds = %8
%15 = sub i64 %9, %12
%16 = load i32, i32* @STDOUT_FILENO
%17 = call i64 @write(i32 %16, i8* %6, i64 %12)
%18 = icmp eq i64 %17, %12
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 (i32, i8*, ...) @err(i32 %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %14, %19
%23 = icmp eq i64 %15, 0
br i1 %23, label %33, label %8
24: ; preds = %8
%25 = tail call i64 @feof(i32* %0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @errx(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %30
30: ; preds = %27, %24
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 (i32, i8*, ...) @err(i32 %31, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %33
33: ; preds = %22, %3, %30
ret void
}