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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int getline2(char *, int);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
void tail(int n) {
  int count = 0;
  char *lines[n];
  char s[1000];
  int w;
  char *p;
  while (w = getline2(s, 1000)) {
    p = malloc(w);
    if (p == NULL) {
      printf("Memory allocation error.");
      return;
    } else {
      strcpy(p, s);
      if (count >= n) {
        free(lines[count % n]);
      }
      lines[count % n] = p;
      count++;
    }
  }
  int start = 0;
  int end = count;
  if (count >= n) {
    start = count;
    end = count + n;
  }
  printf("---->\n");
  for (; start < end; start++) {
    printf("%s", lines[start % n]);
    free(lines[start % n]);
  }
  printf("<----\n");
}


x86 assembly (clang, -O0):
<tail>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x428(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
lea    -0x400(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <getline2@plt>
mov    %eax,-0x404(%rbp)
cmp    $0x0,%eax
je     125f <tail+0x10f>
mov    -0x404(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x410(%rbp)
cmpq   $0x0,-0x410(%rbp)
jne    11ec <tail+0x9c>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x414(%rbp)
jmp    1322 <tail+0x1d2>
mov    -0x410(%rbp),%rdi
lea    -0x400(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jl     1225 <tail+0xd5>
mov    -0x8(%rbp),%eax
cltd
idivl  -0x4(%rbp)
mov    -0x428(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x410(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x8(%rbp),%eax
cltd
idivl  -0x4(%rbp)
mov    -0x428(%rbp),%rax
mov    %edx,%ecx
mov    -0x430(%rbp),%rdx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    118f <tail+0x3f>
movl   $0x0,-0x418(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x41c(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jl     1293 <tail+0x143>
mov    -0x8(%rbp),%eax
mov    %eax,-0x418(%rbp)
mov    -0x8(%rbp),%eax
add    -0x4(%rbp),%eax
mov    %eax,-0x41c(%rbp)
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x418(%rbp),%eax
cmp    -0x41c(%rbp),%eax
jge    130a <tail+0x1ba>
mov    -0x418(%rbp),%eax
cltd
idivl  -0x4(%rbp)
mov    -0x428(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x418(%rbp),%eax
cltd
idivl  -0x4(%rbp)
mov    -0x428(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x418(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x418(%rbp)
jmp    12a1 <tail+0x151>
lea    0xd12(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x414(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tail>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f8,%rsp
mov    %edi,%r14d
mov    %edi,%eax
mov    %rsp,%rcx
lea    0xf(,%rax,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
lea    -0x420(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <getline2@plt>
mov    $0x0,%r12d
test   %eax,%eax
je     121f <tail+0xbf>
xor    %r12d,%r12d
lea    -0x420(%rbp),%r13
jmp    11e0 <tail+0x80>
nopl   0x0(%rax)
mov    -0x30(%rbp),%rax
mov    %rbx,(%rax,%r13,8)
add    $0x1,%r12d
mov    %r15,%r13
mov    %r15,%rdi
mov    $0x3e8,%esi
call   1040 <getline2@plt>
test   %eax,%eax
je     121f <tail+0xbf>
mov    %eax,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1289 <tail+0x129>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13,%r15
mov    %r13,%rsi
call   1050 <strcpy@plt>
mov    %r12d,%eax
cltd
idiv   %r14d
mov    %edx,%r13d
cmp    %r14d,%r12d
jl     11c0 <tail+0x60>
mov    -0x30(%rbp),%rax
mov    (%rax,%r13,8),%rdi
call   1030 <free@plt>
jmp    11c0 <tail+0x60>
cmp    %r14d,%r12d
mov    %r12d,%ebx
mov    $0x0,%r13d
cmovl  %r13d,%ebx
cmovge %r14d,%r13d
add    %r12d,%r13d
lea    0xddf(%rip),%rdi
call   1060 <puts@plt>
cmp    %r13d,%ebx
mov    -0x30(%rbp),%r15
jge    127b <tail+0x11b>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%eax
cltd
idiv   %r14d
mov    (%r15,%rdx,8),%r12
lea    0xdb8(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
call   1030 <free@plt>
add    $0x1,%ebx
cmp    %ebx,%r13d
jne    1250 <tail+0xf0>
lea    0xda0(%rip),%rdi
call   1060 <puts@plt>
jmp    1297 <tail+0x137>
lea    0xd70(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Memory allocation error.\00"
@.str.1 = [7 x i8] c"---->\0A\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [7 x i8] c"<----\0A\00"
define void @tail(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [1000 x i8]
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%12 = load i32, i32* %2
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %4
%15 = alloca i8*, i64 %13
store i64 %13, i64* %5
br label %16
16: ; preds = %51, %1
%17 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%18 = call i32 @getline2(i8* %17, i32 1000)
store i32 %18, i32* %7
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %52
20: ; preds = %16
%21 = load i32, i32* %7
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %8
%23 = load i8*, i8** %8
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %9
br label %88
27: ; preds = %20
%28 = load i8*, i8** %8
%29 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%30 = call i32 @strcpy(i8* %28, i8* %29)
%31 = load i32, i32* %3
%32 = load i32, i32* %2
%33 = icmp sge i32 %31, %32
br i1 %33, label %34, label %42
34: ; preds = %27
%35 = load i32, i32* %3
%36 = load i32, i32* %2
%37 = srem i32 %35, %36
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8*, i8** %15, i64 %38
%40 = load i8*, i8** %39
%41 = call i32 @free(i8* %40)
br label %42
42: ; preds = %34, %27
%43 = load i8*, i8** %8
%44 = load i32, i32* %3
%45 = load i32, i32* %2
%46 = srem i32 %44, %45
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8*, i8** %15, i64 %47
store i8* %43, i8** %48
%49 = load i32, i32* %3
%50 = add nsw i32 %49, 1
store i32 %50, i32* %3
br label %51
51: ; preds = %42
br label %16
52: ; preds = %16
store i32 0, i32* %10
%53 = load i32, i32* %3
store i32 %53, i32* %11
%54 = load i32, i32* %3
%55 = load i32, i32* %2
%56 = icmp sge i32 %54, %55
br i1 %56, label %57, label %62
57: ; preds = %52
%58 = load i32, i32* %3
store i32 %58, i32* %10
%59 = load i32, i32* %3
%60 = load i32, i32* %2
%61 = add nsw i32 %59, %60
store i32 %61, i32* %11
br label %62
62: ; preds = %57, %52
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %64
64: ; preds = %83, %62
%65 = load i32, i32* %10
%66 = load i32, i32* %11
%67 = icmp slt i32 %65, %66
br i1 %67, label %68, label %86
68: ; preds = %64
%69 = load i32, i32* %10
%70 = load i32, i32* %2
%71 = srem i32 %69, %70
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8*, i8** %15, i64 %72
%74 = load i8*, i8** %73
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %74)
%76 = load i32, i32* %10
%77 = load i32, i32* %2
%78 = srem i32 %76, %77
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i8*, i8** %15, i64 %79
%81 = load i8*, i8** %80
%82 = call i32 @free(i8* %81)
br label %83
83: ; preds = %68
%84 = load i32, i32* %10
%85 = add nsw i32 %84, 1
store i32 %85, i32* %10
br label %64
86: ; preds = %64
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %9
br label %88
88: ; preds = %86, %25
%89 = load i8*, i8** %4
%90 = load i32, i32* %9
switch i32 %90, label %92 [
i32 0, label %91
i32 1, label %91
]
91: ; preds = %88, %88
ret void
92: ; preds = %88
unreachable
}

LLVM IR (-O2):
@.str = [25 x i8] c"Memory allocation error.\00"
@.str.2 = [3 x i8] c"%s\00"
@str = [6 x i8] c"---->\00"
@str.4 = [6 x i8] c"<----\00"
define void @tail(i32 %0) {
%2 = alloca [1000 x i8]
%3 = zext i32 %0 to i64
%4 = alloca i8*, i64 %3
%5 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%6 = call i32 @getline2(i8* %5, i32 1000)
%7 = icmp eq i32 %6, 0
br i1 %7, label %29, label %8
8: ; preds = %1, %24
%9 = phi i32 [ %27, %24 ], [ %6, %1 ]
%10 = phi i32 [ %26, %24 ], [ 0, %1 ]
%11 = call i8* @malloc(i32 %9)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %49
15: ; preds = %8
%16 = call i32 @strcpy(i8* %11, i8* %5)
%17 = icmp slt i32 %10, %0
%18 = srem i32 %10, %0
%19 = zext i32 %18 to i64
br i1 %17, label %24, label %20
20: ; preds = %15
%21 = getelementptr inbounds i8*, i8** %4, i64 %19
%22 = load i8*, i8** %21
%23 = call i32 @free(i8* %22)
br label %24
24: ; preds = %15, %20
%25 = getelementptr inbounds i8*, i8** %4, i64 %19
store i8* %11, i8** %25
%26 = add nuw nsw i32 %10, 1
%27 = call i32 @getline2(i8* %5, i32 1000)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %8
29: ; preds = %24, %1
%30 = phi i32 [ 0, %1 ], [ %26, %24 ]
%31 = icmp slt i32 %30, %0
%32 = select i1 %31, i32 0, i32 %30
%33 = select i1 %31, i32 0, i32 %0
%34 = add i32 %33, %30
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%36 = icmp slt i32 %32, %34
br i1 %36, label %37, label %47
37: ; preds = %29, %37
%38 = phi i32 [ %45, %37 ], [ %32, %29 ]
%39 = srem i32 %38, %0
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %4, i64 %40
%42 = load i8*, i8** %41
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = call i32 @free(i8* %42)
%45 = add i32 %38, 1
%46 = icmp eq i32 %45, %34
br i1 %46, label %47, label %37
47: ; preds = %37, %29
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.4, i64 0, i64 0))
br label %49
49: ; preds = %47, %13
ret void
}