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

/* Variables and functions */
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stdout;
int head_stream(FILE *stream, int lines) {
  char *line = NULL;
  size_t n = 0;
  ssize_t bytes_read = 1;
  while (bytes_read >= 0 && lines > 0) {
    bytes_read = getline(&line, &n, stream);
    if (bytes_read > 0 && line != NULL) {
      fputs(line, stdout);
      lines--;
    }
  }
  if (line != NULL) {
    free(line);
  }
  if (bytes_read == -1) {
    perror("head: error reading file");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<head_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0x2d(%rbp)
jl     117f <head_stream+0x3f>
cmpl   $0x0,-0x14(%rbp)
setg   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    118f <head_stream+0x4f>
jmp    11d8 <head_stream+0x98>
mov    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <getline@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    11d3 <head_stream+0x93>
cmpq   $0x0,-0x20(%rbp)
je     11d3 <head_stream+0x93>
mov    -0x20(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1166 <head_stream+0x26>
cmpq   $0x0,-0x20(%rbp)
je     11ec <head_stream+0xac>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    120e <head_stream+0xce>
lea    0xe03(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1215 <head_stream+0xd5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<head_stream>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
xor    %eax,%eax
test   %esi,%esi
jle    11ca <head_stream+0x8a>
mov    %esi,%ebp
mov    %rdi,%r12
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r13
mov    0x2e64(%rip),%r14
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1060 <getline@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    11a9 <head_stream+0x69>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     11a9 <head_stream+0x69>
mov    (%r14),%esi
call   1040 <fputs@plt>
add    $0xffffffff,%ebp
test   %ebx,%ebx
js     11b1 <head_stream+0x71>
test   %ebp,%ebp
jg     1180 <head_stream+0x40>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     11c0 <head_stream+0x80>
call   1030 <free@plt>
cmp    $0xffffffff,%ebx
mov    $0x0,%eax
je     11d9 <head_stream+0x99>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe20(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%eax
jmp    11ca <head_stream+0x8a>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [25 x i8] c"head: error reading file\00"
define i32 @head_stream(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i8* null, i8** %6
store i64 0, i64* %7
store i32 1, i32* %8
br label %9
9: ; preds = %31, %2
%10 = load i32, i32* %8
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* %5
%14 = icmp sgt i32 %13, 0
br label %15
15: ; preds = %12, %9
%16 = phi i1 [ false, %9 ], [ %14, %12 ]
br i1 %16, label %17, label %32
17: ; preds = %15
%18 = load i32*, i32** %4
%19 = call i32 @getline(i8** %6, i64* %7, i32* %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %17
%23 = load i8*, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %31
25: ; preds = %22
%26 = load i8*, i8** %6
%27 = load i32, i32* @stdout
%28 = call i32 @fputs(i8* %26, i32 %27)
%29 = load i32, i32* %5
%30 = add nsw i32 %29, -1
store i32 %30, i32* %5
br label %31
31: ; preds = %25, %22, %17
br label %9
32: ; preds = %15
%33 = load i8*, i8** %6
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i8*, i8** %6
%37 = call i32 @free(i8* %36)
br label %38
38: ; preds = %35, %32
%39 = load i32, i32* %8
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %44
43: ; preds = %38
store i32 0, i32* %3
br label %44
44: ; preds = %43, %41
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [25 x i8] c"head: error reading file\00"
define i32 @head_stream(i32* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %33
8: ; preds = %2, %19
%9 = phi i32 [ %20, %19 ], [ %1, %2 ]
%10 = call i32 @getline(i8** %3, i64* %4, i32* %0)
%11 = icmp sgt i32 %10, 0
%12 = load i8*, i8** %3
%13 = icmp ne i8* %12, null
%14 = select i1 %11, i1 %13, i1 false
br i1 %14, label %15, label %19
15: ; preds = %8
%16 = load i32, i32* @stdout
%17 = call i32 @fputs(i8* %12, i32 %16)
%18 = add nsw i32 %9, -1
br label %19
19: ; preds = %15, %8
%20 = phi i32 [ %18, %15 ], [ %9, %8 ]
%21 = icmp sgt i32 %10, -1
%22 = icmp sgt i32 %20, 0
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %8, label %24
24: ; preds = %19
%25 = load i8*, i8** %3
%26 = icmp eq i8* %25, null
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 @free(i8* %25)
br label %29
29: ; preds = %27, %24
%30 = icmp eq i32 %10, -1
br i1 %30, label %31, label %33
31: ; preds = %29
%32 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %2, %29, %31
%34 = phi i32 [ 1, %31 ], [ 0, %29 ], [ 0, %2 ]
ret i32 %34
}