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

/* Variables and functions */
char EOF;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
char fgetc(int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ updateLogBuffer(char *);
void print_last_lines(FILE *fd, int n) {
  int eolcount = 0;
  int printcount = 0;
  char c;
  fseek(fd, -2, SEEK_END);
  char lastLineBuffer[10000];
  memset(lastLineBuffer, 0, sizeof(lastLineBuffer));
  int atStart = 0;
  while (eolcount < n && !atStart) {
    while ((c = fgetc(fd)) != '\n' && !atStart) {
      if (fseek(fd, -2, SEEK_CUR) != 0) {
        atStart = 1;
      }
    }
    if (c == '\n' && !atStart) {
      eolcount++;
      fseek(fd, -2, SEEK_CUR);
    }
  }
  if (fseek(fd, 1, SEEK_CUR) != 0 && !atStart) {
    perror("seek error");
  }
  if (atStart) {
    fseek(fd, 0, SEEK_SET);
  } else {
    fseek(fd, 1, SEEK_CUR);
  }
  int j = 0;
  while (printcount < eolcount) {
    while ((c = fgetc(fd)) != EOF) {
      lastLineBuffer[j] = c;
      j++;
      if (c == '\n') {
        updateLogBuffer(lastLineBuffer);
        memset(lastLineBuffer, 0, sizeof(lastLineBuffer));
        j = 0;
        printcount++;
        break;
      }
    }
  }
}


x86 assembly (clang, -O0):
<print_last_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2740,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffe,%esi
call   1060 <fseek@plt>
lea    -0x2730(%rbp),%rdi
xor    %esi,%esi
mov    $0x2710,%edx
call   1030 <memset@plt>
movl   $0x0,-0x2734(%rbp)
mov    -0x10(%rbp),%ecx
xor    %eax,%eax
cmp    -0xc(%rbp),%ecx
mov    %al,-0x2739(%rbp)
jge    11ca <print_last_lines+0x7a>
cmpl   $0x0,-0x2734(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x2739(%rbp)
mov    -0x2739(%rbp),%al
test   $0x1,%al
jne    11dd <print_last_lines+0x8d>
jmp    1296 <print_last_lines+0x146>
jmp    11e2 <print_last_lines+0x92>
mov    -0x8(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x15(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x273a(%rbp)
je     1214 <print_last_lines+0xc4>
cmpl   $0x0,-0x2734(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x273a(%rbp)
mov    -0x273a(%rbp),%al
test   $0x1,%al
jne    1227 <print_last_lines+0xd7>
jmp    1257 <print_last_lines+0x107>
mov    -0x8(%rbp),%rdi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffe,%esi
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     1252 <print_last_lines+0x102>
movl   $0x1,-0x2734(%rbp)
jmp    11e2 <print_last_lines+0x92>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    1291 <print_last_lines+0x141>
cmpl   $0x0,-0x2734(%rbp)
jne    1291 <print_last_lines+0x141>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffe,%esi
call   1060 <fseek@plt>
jmp    11a4 <print_last_lines+0x54>
mov    -0x8(%rbp),%rdi
mov    0x2d3f(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     12d0 <print_last_lines+0x180>
cmpl   $0x0,-0x2734(%rbp)
jne    12d0 <print_last_lines+0x180>
lea    0xd35(%rip),%rdi
call   1070 <perror@plt>
cmpl   $0x0,-0x2734(%rbp)
je     12f6 <print_last_lines+0x1a6>
mov    -0x8(%rbp),%rdi
mov    0x2ce0(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
jmp    130d <print_last_lines+0x1bd>
mov    -0x8(%rbp),%rdi
mov    0x2cdf(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1060 <fseek@plt>
movl   $0x0,-0x2738(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    13b7 <print_last_lines+0x267>
jmp    1328 <print_last_lines+0x1d8>
mov    -0x8(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x15(%rbp)
movsbl %al,%eax
mov    0x2c9a(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     13b2 <print_last_lines+0x262>
mov    -0x15(%rbp),%cl
movslq -0x2738(%rbp),%rax
mov    %cl,-0x2730(%rbp,%rax,1)
mov    -0x2738(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2738(%rbp)
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    13ad <print_last_lines+0x25d>
lea    -0x2730(%rbp),%rdi
call   1050 <updateLogBuffer@plt>
lea    -0x2730(%rbp),%rdi
xor    %esi,%esi
mov    $0x2710,%edx
call   1030 <memset@plt>
movl   $0x0,-0x2738(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    13b2 <print_last_lines+0x262>
jmp    1328 <print_last_lines+0x1d8>
jmp    1317 <print_last_lines+0x1c7>
add    $0x2740,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_last_lines>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2728,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffe,%esi
call   1060 <fseek@plt>
lea    0x10(%rsp),%rdi
xor    %r15d,%r15d
xor    %esi,%esi
mov    $0x2710,%edx
call   1030 <memset@plt>
mov    %ebp,0xc(%rsp)
test   %ebp,%ebp
jle    1210 <print_last_lines+0xc0>
xor    %ebp,%ebp
mov    0x2e42(%rip),%r12
mov    $0x1,%r13d
xor    %r15d,%r15d
jmp    11c8 <print_last_lines+0x78>
nopl   0x0(%rax)
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0xfffffffe,%esi
call   1060 <fseek@plt>
test   %rax,%rax
cmovne %r13d,%ebp
mov    %rbx,%rdi
call   1040 <fgetc@plt>
test   %ebp,%ebp
setne  %cl
cmp    $0xa,%al
setne  %al
je     11e0 <print_last_lines+0x90>
test   %ebp,%ebp
je     11b0 <print_last_lines+0x60>
test   %al,%al
jne    11fd <print_last_lines+0xad>
test   %cl,%cl
jne    11fd <print_last_lines+0xad>
add    $0x1,%r15d
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0xfffffffe,%esi
call   1060 <fseek@plt>
test   %ebp,%ebp
setne  %r14b
cmp    0xc(%rsp),%r15d
jge    1213 <print_last_lines+0xc3>
test   %ebp,%ebp
je     11c8 <print_last_lines+0x78>
jmp    1213 <print_last_lines+0xc3>
xor    %r14d,%r14d
mov    0x2dc6(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1060 <fseek@plt>
test   %rax,%rax
je     1238 <print_last_lines+0xe8>
test   %r14b,%r14b
je     12cb <print_last_lines+0x17b>
mov    %r14d,%eax
xor    $0x1,%al
movzbl %al,%esi
test   %r14b,%r14b
je     124c <print_last_lines+0xfc>
mov    0x2d7c(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
call   1060 <fseek@plt>
test   %r15d,%r15d
jle    12b9 <print_last_lines+0x169>
xor    %ebp,%ebp
mov    0x2d73(%rip),%r13
lea    0x10(%rsp),%r14
xor    %r12d,%r12d
jmp    1275 <print_last_lines+0x125>
nop
cmp    %r15d,%r12d
jge    12b9 <print_last_lines+0x169>
movslq %ebp,%rbp
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <fgetc@plt>
cmp    0x0(%r13),%al
je     1270 <print_last_lines+0x120>
mov    %al,0x10(%rsp,%rbp,1)
add    $0x1,%rbp
cmp    $0xa,%al
jne    1280 <print_last_lines+0x130>
mov    %r14,%rdi
call   1050 <updateLogBuffer@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x2710,%edx
call   1030 <memset@plt>
add    $0x1,%r12d
xor    %ebp,%ebp
jmp    1270 <print_last_lines+0x120>
add    $0x2728,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2e(%rip),%rdi
call   1070 <perror@plt>
jmp    1238 <print_last_lines+0xe8>

LLVM IR (-O0):
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@.str = [11 x i8] c"seek error\00"
@SEEK_SET = global i32 0
@EOF = global i8 0
define void @print_last_lines(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca [10000 x i8]
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%11 = load i32*, i32** %3
%12 = load i32, i32* @SEEK_END
%13 = call i64 @fseek(i32* %11, i32 -2, i32 %12)
%14 = getelementptr inbounds [10000 x i8], [10000 x i8]* %8, i64 0, i64 0
%15 = call i32 @memset(i8* %14, i32 0, i32 10000)
store i32 0, i32* %9
br label %16
16: ; preds = %58, %2
%17 = load i32, i32* %5
%18 = load i32, i32* %4
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i32, i32* %9
%22 = icmp ne i32 %21, 0
%23 = xor i1 %22, true
br label %24
24: ; preds = %20, %16
%25 = phi i1 [ false, %16 ], [ %23, %20 ]
br i1 %25, label %26, label %59
26: ; preds = %24
br label %27
27: ; preds = %44, %26
%28 = load i32*, i32** %3
%29 = call signext i8 @fgetc(i32* %28)
store i8 %29, i8* %7
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 10
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i32, i32* %9
%34 = icmp ne i32 %33, 0
%35 = xor i1 %34, true
br label %36
36: ; preds = %32, %27
%37 = phi i1 [ false, %27 ], [ %35, %32 ]
br i1 %37, label %38, label %45
38: ; preds = %36
%39 = load i32*, i32** %3
%40 = load i32, i32* @SEEK_CUR
%41 = call i64 @fseek(i32* %39, i32 -2, i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %38
store i32 1, i32* %9
br label %44
44: ; preds = %43, %38
br label %27
45: ; preds = %36
%46 = load i8, i8* %7
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 10
br i1 %48, label %49, label %58
49: ; preds = %45
%50 = load i32, i32* %9
%51 = icmp ne i32 %50, 0
br i1 %51, label %58, label %52
52: ; preds = %49
%53 = load i32, i32* %5
%54 = add nsw i32 %53, 1
store i32 %54, i32* %5
%55 = load i32*, i32** %3
%56 = load i32, i32* @SEEK_CUR
%57 = call i64 @fseek(i32* %55, i32 -2, i32 %56)
br label %58
58: ; preds = %52, %49, %45
br label %16
59: ; preds = %24
%60 = load i32*, i32** %3
%61 = load i32, i32* @SEEK_CUR
%62 = call i64 @fseek(i32* %60, i32 1, i32 %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %69
64: ; preds = %59
%65 = load i32, i32* %9
%66 = icmp ne i32 %65, 0
br i1 %66, label %69, label %67
67: ; preds = %64
%68 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %69
69: ; preds = %67, %64, %59
%70 = load i32, i32* %9
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %69
%73 = load i32*, i32** %3
%74 = load i32, i32* @SEEK_SET
%75 = call i64 @fseek(i32* %73, i32 0, i32 %74)
br label %80
76: ; preds = %69
%77 = load i32*, i32** %3
%78 = load i32, i32* @SEEK_CUR
%79 = call i64 @fseek(i32* %77, i32 1, i32 %78)
br label %80
80: ; preds = %76, %72
store i32 0, i32* %10
br label %81
81: ; preds = %111, %80
%82 = load i32, i32* %6
%83 = load i32, i32* %5
%84 = icmp slt i32 %82, %83
br i1 %84, label %85, label %112
85: ; preds = %81
br label %86
86: ; preds = %110, %85
%87 = load i32*, i32** %3
%88 = call signext i8 @fgetc(i32* %87)
store i8 %88, i8* %7
%89 = sext i8 %88 to i32
%90 = load i8, i8* @EOF
%91 = sext i8 %90 to i32
%92 = icmp ne i32 %89, %91
br i1 %92, label %93, label %111
93: ; preds = %86
%94 = load i8, i8* %7
%95 = load i32, i32* %10
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds [10000 x i8], [10000 x i8]* %8, i64 0, i64 %96
store i8 %94, i8* %97
%98 = load i32, i32* %10
%99 = add nsw i32 %98, 1
store i32 %99, i32* %10
%100 = load i8, i8* %7
%101 = sext i8 %100 to i32
%102 = icmp eq i32 %101, 10
br i1 %102, label %103, label %110
103: ; preds = %93
%104 = getelementptr inbounds [10000 x i8], [10000 x i8]* %8, i64 0, i64 0
%105 = call i32 @updateLogBuffer(i8* %104)
%106 = getelementptr inbounds [10000 x i8], [10000 x i8]* %8, i64 0, i64 0
%107 = call i32 @memset(i8* %106, i32 0, i32 10000)
store i32 0, i32* %10
%108 = load i32, i32* %6
%109 = add nsw i32 %108, 1
store i32 %109, i32* %6
br label %111
110: ; preds = %93
br label %86
111: ; preds = %103, %86
br label %81
112: ; preds = %81
ret void
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@.str = [11 x i8] c"seek error\00"
@SEEK_SET = global i32 0
@EOF = global i8 0
define void @print_last_lines(i32* nocapture %0, i32 %1) {
%3 = alloca [10000 x i8]
%4 = load i32, i32* @SEEK_END
%5 = tail call i64 @fseek(i32* %0, i32 -2, i32 %4)
%6 = getelementptr inbounds [10000 x i8], [10000 x i8]* %3, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 10000)
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %43
9: ; preds = %2, %37
%10 = phi i32 [ %29, %37 ], [ 0, %2 ]
%11 = phi i32 [ %38, %37 ], [ 0, %2 ]
%12 = call signext i8 @fgetc(i32* %0)
%13 = icmp ne i8 %12, 10
%14 = icmp ne i32 %10, 0
%15 = xor i1 %14, true
%16 = select i1 %13, i1 %15, i1 false
br i1 %16, label %17, label %28
17: ; preds = %9, %17
%18 = phi i32 [ %22, %17 ], [ %10, %9 ]
%19 = load i32, i32* @SEEK_CUR
%20 = call i64 @fseek(i32* %0, i32 -2, i32 %19)
%21 = icmp eq i64 %20, 0
%22 = select i1 %21, i32 %18, i32 1
%23 = call signext i8 @fgetc(i32* %0)
%24 = icmp ne i8 %23, 10
%25 = icmp ne i32 %22, 0
%26 = xor i1 %25, true
%27 = select i1 %24, i1 %26, i1 false
br i1 %27, label %17, label %28
28: ; preds = %17, %9
%29 = phi i32 [ %10, %9 ], [ %22, %17 ]
%30 = phi i1 [ %13, %9 ], [ %24, %17 ]
%31 = phi i1 [ %14, %9 ], [ %25, %17 ]
%32 = select i1 %30, i1 true, i1 %31
br i1 %32, label %37, label %33
33: ; preds = %28
%34 = add nsw i32 %11, 1
%35 = load i32, i32* @SEEK_CUR
%36 = call i64 @fseek(i32* %0, i32 -2, i32 %35)
br label %37
37: ; preds = %33, %28
%38 = phi i32 [ %11, %28 ], [ %34, %33 ]
%39 = icmp slt i32 %38, %1
%40 = icmp ne i32 %29, 0
%41 = xor i1 %40, true
%42 = select i1 %39, i1 %41, i1 false
br i1 %42, label %9, label %43
43: ; preds = %37, %2
%44 = phi i32 [ 0, %2 ], [ %38, %37 ]
%45 = phi i1 [ false, %2 ], [ %40, %37 ]
%46 = load i32, i32* @SEEK_CUR
%47 = call i64 @fseek(i32* %0, i32 1, i32 %46)
%48 = icmp eq i64 %47, 0
%49 = select i1 %48, i1 true, i1 %45
br i1 %49, label %52, label %50
50: ; preds = %43
%51 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %52
52: ; preds = %50, %43
%53 = xor i1 %45, true
%54 = zext i1 %53 to i32
%55 = load i32, i32* @SEEK_SET
%56 = load i32, i32* @SEEK_CUR
%57 = select i1 %45, i32 %55, i32 %56
%58 = call i64 @fseek(i32* %0, i32 %54, i32 %57)
%59 = icmp sgt i32 %44, 0
br i1 %59, label %60, label %82
60: ; preds = %52, %78
%61 = phi i64 [ %80, %78 ], [ 0, %52 ]
%62 = phi i32 [ %79, %78 ], [ 0, %52 ]
%63 = shl i64 %61, 32
%64 = ashr exact i64 %63, 32
br label %65
65: ; preds = %60, %70
%66 = phi i64 [ %64, %60 ], [ %72, %70 ]
%67 = call signext i8 @fgetc(i32* %0)
%68 = load i8, i8* @EOF
%69 = icmp eq i8 %67, %68
br i1 %69, label %78, label %70
70: ; preds = %65
%71 = getelementptr inbounds [10000 x i8], [10000 x i8]* %3, i64 0, i64 %66
store i8 %67, i8* %71
%72 = add i64 %66, 1
%73 = icmp eq i8 %67, 10
br i1 %73, label %74, label %65
74: ; preds = %70
%75 = call i32 @updateLogBuffer(i8* %6)
%76 = call i32 @memset(i8* %6, i32 0, i32 10000)
%77 = add nsw i32 %62, 1
br label %78
78: ; preds = %65, %74
%79 = phi i32 [ %77, %74 ], [ %62, %65 ]
%80 = phi i64 [ 0, %74 ], [ %66, %65 ]
%81 = icmp slt i32 %79, %44
br i1 %81, label %60, label %82
82: ; preds = %78, %52
ret void
}