#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 */
scalar_t__ calloc(size_t, int);
int /*<<< orphan*/ free(char *);
char *readline(int /*<<< orphan*/ *, size_t);
char **tail(FILE *fp, size_t n, size_t buffsize, int *pos) {
  char **lines;
  size_t current = 0;
  lines = (char **)calloc(n, sizeof(char *));
  char *newline;
  while ((newline = readline(fp, buffsize)) != NULL) {
    if (lines[current % n] != NULL) {
      free(lines[current % n]);
    }
    lines[current++ % n] = newline;
  }
  *pos = current % n;
  return lines;
}


x86 assembly (clang, -O0):
<tail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <readline@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     11ef <tail+0xbf>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    -0x40(%rbp),%rax
cmpq   $0x0,(%rax,%rdx,8)
je     11c1 <tail+0x91>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax,%rdx,8),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
xor    %edx,%edx
divq   -0x10(%rbp)
mov    -0x50(%rbp),%rax
mov    %rcx,(%rax,%rdx,8)
jmp    1162 <tail+0x32>
mov    -0x30(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    %edx,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tail>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x8(%rsp)
mov    %rdx,%rbx
mov    %rsi,%r13
mov    %rdi,%rbp
mov    %rsi,%rdi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,%r15
mov    %rbp,0x10(%rsp)
mov    %rbp,%rdi
mov    %rbx,%rbp
mov    %rbx,%rsi
call   1040 <readline@plt>
test   %rax,%rax
je     11da <tail+0xaa>
mov    %rax,%r14
xor    %ebx,%ebx
jmp    119d <tail+0x6d>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    %r14,(%r15,%r12,8)
mov    0x10(%rsp),%rdi
mov    %rbp,%rsi
call   1040 <readline@plt>
mov    %rax,%r14
test   %rax,%rax
je     11dc <tail+0xac>
mov    %rbx,%rax
or     %r13,%rax
shr    $0x20,%rax
je     11c0 <tail+0x90>
mov    %rbx,%rax
xor    %edx,%edx
div    %r13
mov    %rdx,%r12
jmp    11ca <tail+0x9a>
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%eax
xor    %edx,%edx
div    %r13d
mov    %edx,%r12d
mov    (%r15,%r12,8),%rdi
test   %rdi,%rdi
je     1180 <tail+0x50>
call   1030 <free@plt>
jmp    1180 <tail+0x50>
xor    %ebx,%ebx
mov    %rbx,%rax
or     %r13,%rax
shr    $0x20,%rax
je     11f2 <tail+0xc2>
mov    %rbx,%rax
xor    %edx,%edx
div    %r13
jmp    11f9 <tail+0xc9>
mov    %ebx,%eax
xor    %edx,%edx
div    %r13d
mov    0x8(%rsp),%rax
mov    %edx,(%rax)
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8** @tail(i32* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i8**
%10 = alloca i64
%11 = alloca i8*
store i32* %0, i32** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32* %3, i32** %8
store i64 0, i64* %10
%12 = load i64, i64* %6
%13 = call i64 @calloc(i64 %12, i32 8)
%14 = inttoptr i64 %13 to i8**
store i8** %14, i8*** %9
br label %15
15: ; preds = %36, %4
%16 = load i32*, i32** %5
%17 = load i64, i64* %7
%18 = call i8* @readline(i32* %16, i64 %17)
store i8* %18, i8** %11
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %44
20: ; preds = %15
%21 = load i8**, i8*** %9
%22 = load i64, i64* %10
%23 = load i64, i64* %6
%24 = urem i64 %22, %23
%25 = getelementptr inbounds i8*, i8** %21, i64 %24
%26 = load i8*, i8** %25
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %36
28: ; preds = %20
%29 = load i8**, i8*** %9
%30 = load i64, i64* %10
%31 = load i64, i64* %6
%32 = urem i64 %30, %31
%33 = getelementptr inbounds i8*, i8** %29, i64 %32
%34 = load i8*, i8** %33
%35 = call i32 @free(i8* %34)
br label %36
36: ; preds = %28, %20
%37 = load i8*, i8** %11
%38 = load i8**, i8*** %9
%39 = load i64, i64* %10
%40 = add i64 %39, 1
store i64 %40, i64* %10
%41 = load i64, i64* %6
%42 = urem i64 %39, %41
%43 = getelementptr inbounds i8*, i8** %38, i64 %42
store i8* %37, i8** %43
br label %15
44: ; preds = %15
%45 = load i64, i64* %10
%46 = load i64, i64* %6
%47 = urem i64 %45, %46
%48 = trunc i64 %47 to i32
%49 = load i32*, i32** %8
store i32 %48, i32* %49
%50 = load i8**, i8*** %9
ret i8** %50
}

LLVM IR (-O2):
define i8** @tail(i32* %0, i64 %1, i64 %2, i32* nocapture writeonly %3) {
%5 = tail call i64 @calloc(i64 %1, i32 8)
%6 = inttoptr i64 %5 to i8**
%7 = tail call i8* @readline(i32* %0, i64 %2)
%8 = icmp eq i8* %7, null
br i1 %8, label %22, label %9
9: ; preds = %4, %18
%10 = phi i8* [ %20, %18 ], [ %7, %4 ]
%11 = phi i64 [ %19, %18 ], [ 0, %4 ]
%12 = urem i64 %11, %1
%13 = getelementptr inbounds i8*, i8** %6, i64 %12
%14 = load i8*, i8** %13
%15 = icmp eq i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %9
%17 = tail call i32 @free(i8* %14)
br label %18
18: ; preds = %16, %9
%19 = add i64 %11, 1
store i8* %10, i8** %13
%20 = tail call i8* @readline(i32* %0, i64 %2)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %9
22: ; preds = %18, %4
%23 = phi i64 [ 0, %4 ], [ %19, %18 ]
%24 = urem i64 %23, %1
%25 = trunc i64 %24 to i32
store i32 %25, i32* %3
ret i8** %6
}