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

/* Variables and functions */
int EOF;
int bail(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int putchar(int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int head(int nl, char *fname) {
  FILE *tf = NULL;
  if ((tf = fopen(fname, "r")) == NULL)
    return bail("head: %s: %s\n", fname, strerror(errno));
  int ch = 0;
  while ((ch != EOF) && (nl > 0)) {
    if ((ch = fgetc(tf)) == EOF)
      break;
    if (ferror(tf)) {
      fclose(tf);
      return bail("head: %s: %s\n", fname, strerror(errno));
    }
    if (ch == '\n')
      nl--;
    if (putchar(ch) == EOF) {
      fclose(tf);
      return bail("head: %s: %s\n", fname, strerror(errno));
    }
  }
  fclose(tf);
  return 0;
}


x86 assembly (clang, -O0):
<head>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d5 <head+0x65>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xe3a(%rip),%rdi
call   1070 <bail@plt>
mov    %eax,-0x4(%rbp)
jmp    12f1 <head+0x181>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
mov    0x2e00(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x29(%rbp)
je     11fd <head+0x8d>
cmpl   $0x0,-0x8(%rbp)
setg   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    120d <head+0x9d>
jmp    12e1 <head+0x171>
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dc8(%rip),%rcx
cmp    (%rcx),%eax
jne    122d <head+0xbd>
jmp    12e1 <head+0x171>
mov    -0x18(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     1279 <head+0x109>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xd96(%rip),%rdi
call   1070 <bail@plt>
mov    %eax,-0x4(%rbp)
jmp    12f1 <head+0x181>
cmpl   $0xa,-0x1c(%rbp)
jne    128c <head+0x11c>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <putchar@plt>
mov    0x2d4d(%rip),%rcx
cmp    (%rcx),%eax
jne    12dc <head+0x16c>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xd33(%rip),%rdi
call   1070 <bail@plt>
mov    %eax,-0x4(%rbp)
jmp    12f1 <head+0x181>
jmp    11dc <head+0x6c>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<head>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe7c(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f6 <head+0x86>
mov    %rax,%r15
mov    0x2e4d(%rip),%r12
mov    (%r12),%ecx
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %ecx,%ebx
je     121d <head+0xad>
test   %ebp,%ebp
jle    121d <head+0xad>
mov    %r15,%rdi
call   1060 <fgetc@plt>
cmp    (%r12),%eax
je     121d <head+0xad>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    11ee <head+0x7e>
xor    %eax,%eax
cmp    $0xa,%ebx
sete   %al
sub    %eax,%ebp
mov    %ebx,%edi
call   1030 <putchar@plt>
mov    (%r12),%ecx
cmp    %ecx,%eax
jne    11b0 <head+0x40>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdf7(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <bail@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [14 x i8] c"head: %s: %s\0A\00"
@errno = global i32 0
@EOF = global i32 0
define i32 @head(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32* null, i32** %6
%8 = load i8*, i8** %5
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8*, i8** %5
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = call i32 @bail(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %12, i32 %14)
store i32 %15, i32* %3
br label %65
16: ; preds = %2
store i32 0, i32* %7
br label %17
17: ; preds = %61, %16
%18 = load i32, i32* %7
%19 = load i32, i32* @EOF
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %4
%23 = icmp sgt i32 %22, 0
br label %24
24: ; preds = %21, %17
%25 = phi i1 [ false, %17 ], [ %23, %21 ]
br i1 %25, label %26, label %62
26: ; preds = %24
%27 = load i32*, i32** %6
%28 = call i32 @fgetc(i32* %27)
store i32 %28, i32* %7
%29 = load i32, i32* @EOF
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %26
br label %62
32: ; preds = %26
%33 = load i32*, i32** %6
%34 = call i64 @ferror(i32* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %32
%37 = load i32*, i32** %6
%38 = call i32 @fclose(i32* %37)
%39 = load i8*, i8** %5
%40 = load i32, i32* @errno
%41 = call i32 @strerror(i32 %40)
%42 = call i32 @bail(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %39, i32 %41)
store i32 %42, i32* %3
br label %65
43: ; preds = %32
%44 = load i32, i32* %7
%45 = icmp eq i32 %44, 10
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i32, i32* %4
%48 = add nsw i32 %47, -1
store i32 %48, i32* %4
br label %49
49: ; preds = %46, %43
%50 = load i32, i32* %7
%51 = call i32 @putchar(i32 %50)
%52 = load i32, i32* @EOF
%53 = icmp eq i32 %51, %52
br i1 %53, label %54, label %61
54: ; preds = %49
%55 = load i32*, i32** %6
%56 = call i32 @fclose(i32* %55)
%57 = load i8*, i8** %5
%58 = load i32, i32* @errno
%59 = call i32 @strerror(i32 %58)
%60 = call i32 @bail(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %57, i32 %59)
store i32 %60, i32* %3
br label %65
61: ; preds = %49
br label %17
62: ; preds = %31, %24
%63 = load i32*, i32** %6
%64 = call i32 @fclose(i32* %63)
store i32 0, i32* %3
br label %65
65: ; preds = %62, %54, %36, %11
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [14 x i8] c"head: %s: %s\0A\00"
@errno = global i32 0
@EOF = global i32 0
define i32 @head(i32 %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = load i32, i32* @EOF
br label %11
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 @bail(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %9)
br label %44
11: ; preds = %5, %30
%12 = phi i32 [ %35, %30 ], [ %6, %5 ]
%13 = phi i32 [ %33, %30 ], [ %0, %5 ]
%14 = phi i32 [ %19, %30 ], [ 0, %5 ]
%15 = icmp ne i32 %14, %12
%16 = icmp sgt i32 %13, 0
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %18, label %42
18: ; preds = %11
%19 = tail call i32 @fgetc(i32* %3)
%20 = load i32, i32* @EOF
%21 = icmp eq i32 %19, %20
br i1 %21, label %42, label %22
22: ; preds = %18
%23 = tail call i64 @ferror(i32* %3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %22
%26 = tail call i32 @fclose(i32* %3)
%27 = load i32, i32* @errno
%28 = tail call i32 @strerror(i32 %27)
%29 = tail call i32 @bail(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %28)
br label %44
30: ; preds = %22
%31 = icmp eq i32 %19, 10
%32 = sext i1 %31 to i32
%33 = add nsw i32 %13, %32
%34 = tail call i32 @putchar(i32 %19)
%35 = load i32, i32* @EOF
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %11
37: ; preds = %30
%38 = tail call i32 @fclose(i32* %3)
%39 = load i32, i32* @errno
%40 = tail call i32 @strerror(i32 %39)
%41 = tail call i32 @bail(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %40)
br label %44
42: ; preds = %18, %11
%43 = tail call i32 @fclose(i32* %3)
br label %44
44: ; preds = %25, %37, %42, %7
%45 = phi i32 [ %10, %7 ], [ 0, %42 ], [ %29, %25 ], [ %41, %37 ]
ret i32 %45
}