#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 _head_error(char const *, int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ stdout;
int _head_do(int flgn, char const *filename) {
  FILE *fp;
  int n = 0;
  int c;
  if (filename == NULL)
    fp = stdin;
  else if ((fp = fopen(filename, "r")) == NULL)
    return _head_error(filename, 1);
  while ((c = fgetc(fp)) != EOF && n < flgn) {
    if (c == '\n')
      n++;
    fwrite(&c, sizeof(char), 1, stdout);
  }
  if (filename != NULL && fclose(fp) != 0)
    return _head_error(filename, 1);
  return 0;
}


x86 assembly (clang, -O0):
<_head_do>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1184 <_head_do+0x34>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11bd <_head_do+0x6d>
mov    -0x10(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b8 <_head_do+0x68>
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1050 <_head_error@plt>
mov    %eax,-0x4(%rbp)
jmp    126c <_head_do+0x11c>
jmp    11bd <_head_do+0x6d>
jmp    11c2 <_head_do+0x72>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
xor    %eax,%eax
mov    0x2e07(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x21(%rbp)
je     11f0 <_head_do+0xa0>
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
setl   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1200 <_head_do+0xb0>
jmp    1231 <_head_do+0xe1>
cmpl   $0xa,-0x20(%rbp)
jne    1213 <_head_do+0xc3>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%ecx
lea    -0x20(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
jmp    11c2 <_head_do+0x72>
cmpq   $0x0,-0x10(%rbp)
je     1265 <_head_do+0x115>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     1265 <_head_do+0x115>
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1050 <_head_error@plt>
mov    %eax,-0x4(%rbp)
jmp    126c <_head_do+0x11c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_head_do>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
mov    %edi,%r13d
test   %rsi,%rsi
je     119b <_head_do+0x4b>
lea    0xe90(%rip),%rsi
mov    %rbp,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a5 <_head_do+0x55>
mov    %rbp,%rdi
mov    $0x1,%esi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <_head_error@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%rbx
mov    %rbp,0x10(%rsp)
mov    %rbx,%rdi
call   1040 <fgetc@plt>
mov    %eax,0xc(%rsp)
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%eax
je     1217 <_head_do+0xc7>
test   %r13d,%r13d
jle    1217 <_head_do+0xc7>
xor    %ebp,%ebp
lea    0xc(%rsp),%r15
nopl   (%rax)
xor    %r12d,%r12d
cmp    $0xa,%eax
sete   %r14b
mov    0x2def(%rip),%rax
mov    (%rax),%ecx
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fgetc@plt>
mov    %eax,0xc(%rsp)
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%eax
je     1217 <_head_do+0xc7>
mov    %r14b,%r12b
add    %r12d,%ebp
cmp    %r13d,%ebp
jl     11d0 <_head_do+0x80>
xor    %r14d,%r14d
mov    0x10(%rsp),%rbp
test   %rbp,%rbp
je     1241 <_head_do+0xf1>
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
je     1241 <_head_do+0xf1>
mov    %rbp,%rdi
mov    $0x1,%esi
call   1050 <_head_error@plt>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@stdin = global i32* null
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
@stdout = global i32 0
define i32 @_head_do(i32 %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i8* %1, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = load i32*, i32** @stdin
br label %13
8: ; preds = %2
%9 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @_head_error(i8* %1, i32 1)
br label %39
13: ; preds = %8, %6
%14 = phi i32* [ %7, %6 ], [ %9, %8 ]
%15 = call i32 @fgetc(i32* %14)
store i32 %15, i32* %3
%16 = load i32, i32* @EOF
%17 = icmp ne i32 %15, %16
%18 = icmp sgt i32 %0, 0
%19 = and i1 %17, %18
br i1 %19, label %20, label %33
20: ; preds = %13, %20
%21 = phi i32 [ %28, %20 ], [ %15, %13 ]
%22 = phi i32 [ %25, %20 ], [ 0, %13 ]
%23 = icmp eq i32 %21, 10
%24 = zext i1 %23 to i32
%25 = add nuw nsw i32 %22, %24
%26 = load i32, i32* @stdout
%27 = call i32 @fwrite(i32* %3, i32 1, i32 1, i32 %26)
%28 = call i32 @fgetc(i32* %14)
store i32 %28, i32* %3
%29 = load i32, i32* @EOF
%30 = icmp ne i32 %28, %29
%31 = icmp slt i32 %25, %0
%32 = select i1 %30, i1 %31, i1 false
br i1 %32, label %20, label %33
33: ; preds = %20, %13
br i1 %5, label %39, label %34
34: ; preds = %33
%35 = call i64 @fclose(i32* %14)
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = call i32 @_head_error(i8* %1, i32 1)
br label %39
39: ; preds = %33, %34, %37, %11
%40 = phi i32 [ %38, %37 ], [ %12, %11 ], [ 0, %34 ], [ 0, %33 ]
ret i32 %40
}