#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 */
char *alloc(int);
int atoi(char *);
int mygetline(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
main(int argc, char *argv[]) {
  int n;
  if (argc == 1)
    n = 10;
  else if (argc == 2 && (*++argv)[0] == '-') {
    n = atoi(*argv + 1);
    if (n < 1)
      n = 10;
  } else {
    printf("Usage: tail -n\n");
    return 1;
  }
  char line[10000], *lineptr[50000];
  int len;
  char *p;
  int lines = 0;
  while ((len = mygetline(line, 10000)) > 0) {
    p = alloc(len + 1);
    strcpy(p, line);
    lineptr[lines++] = p;
  }
  int i = lines - n;
  i = (i >= 0) ? i : 0;
  while (n-- > 0 && i <= lines) {
    printf("%s", lineptr[i++]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x641d0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    117f <main+0x2f>
movl   $0xa,-0x14(%rbp)
jmp    11f0 <main+0xa0>
cmpl   $0x2,-0x8(%rbp)
jne    11d1 <main+0x81>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    11d1 <main+0x81>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    11cc <main+0x7c>
movl   $0xa,-0x14(%rbp)
jmp    11eb <main+0x9b>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1328 <main+0x1d8>
jmp    11f0 <main+0xa0>
movl   $0x0,-0x641c4(%rbp)
lea    -0x2730(%rbp),%rdi
mov    $0x2710,%esi
call   1060 <mygetline@plt>
mov    %eax,-0x641b4(%rbp)
cmp    $0x0,%eax
jle    1269 <main+0x119>
mov    -0x641b4(%rbp),%edi
add    $0x1,%edi
call   1050 <alloc@plt>
mov    %rax,-0x641c0(%rbp)
mov    -0x641c0(%rbp),%rdi
lea    -0x2730(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x641c0(%rbp),%rcx
mov    -0x641c4(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x641c4(%rbp)
cltq
mov    %rcx,-0x641b0(%rbp,%rax,8)
ff
jmp    11fa <main+0xaa>
mov    -0x641c4(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x641c8(%rbp)
cmpl   $0x0,-0x641c8(%rbp)
jl     1296 <main+0x146>
mov    -0x641c8(%rbp),%eax
mov    %eax,-0x641cc(%rbp)
jmp    12a3 <main+0x153>
xor    %eax,%eax
mov    %eax,-0x641cc(%rbp)
jmp    12a3 <main+0x153>
mov    -0x641cc(%rbp),%eax
mov    %eax,-0x641c8(%rbp)
mov    -0x14(%rbp),%ecx
mov    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x641cd(%rbp)
jle    12e0 <main+0x190>
mov    -0x641c8(%rbp),%eax
cmp    -0x641c4(%rbp),%eax
setle  %al
mov    %al,-0x641cd(%rbp)
mov    -0x641cd(%rbp),%al
test   $0x1,%al
jne    12f3 <main+0x1a3>
jmp    1321 <main+0x1d1>
mov    -0x641c8(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x641c8(%rbp)
cltq
mov    -0x641b0(%rbp,%rax,8),%rsi
ff
lea    0xcfb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12af <main+0x15f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x641d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x64190,%rsp
cmp    $0x1,%edi
je     11af <main+0x4f>
cmp    $0x2,%edi
jne    1199 <main+0x39>
mov    0x8(%rsi),%rdi
cmpb   $0x2d,(%rdi)
jne    1199 <main+0x39>
add    $0x1,%rdi
call   1080 <atoi@plt>
test   %eax,%eax
mov    $0xa,%r12d
cmovg  %eax,%r12d
jmp    11b5 <main+0x55>
lea    0xe63(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    126a <main+0x10a>
mov    $0xa,%r12d
mov    %rsp,%rdi
mov    $0x2710,%esi
call   1070 <mygetline@plt>
xor    %r15d,%r15d
mov    $0x0,%ebp
test   %eax,%eax
jle    1215 <main+0xb5>
xor    %ebp,%ebp
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%eax
mov    %eax,%edi
call   1060 <alloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,0x2710(%rsp,%rbp,8)
add    $0x1,%rbp
mov    %r14,%rdi
mov    $0x2710,%esi
call   1070 <mygetline@plt>
test   %eax,%eax
jg     11e0 <main+0x80>
mov    %ebp,%eax
sub    %r12d,%eax
cmovns %eax,%r15d
test   %r12d,%r12d
jle    1268 <main+0x108>
cmp    %ebp,%r15d
ja     1268 <main+0x108>
mov    %r15d,%ebx
mov    %ebp,%ebp
lea    0xdcc(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2710(%rsp,%rbx,8),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x2,%r12d
jb     1268 <main+0x108>
add    $0xffffffff,%r12d
lea    0x1(%rbx),%rax
cmp    %rbp,%rbx
mov    %rax,%rbx
jb     1240 <main+0xe0>
xor    %eax,%eax
add    $0x64190,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Usage: tail -n\0A\00"
@.str.1 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [10000 x i8]
%8 = alloca [50000 x i8*]
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 10, i32* %6
br label %39
16: ; preds = %2
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %36
19: ; preds = %16
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i32 1
store i8** %21, i8*** %5
%22 = load i8*, i8** %21
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 45
br i1 %26, label %27, label %36
27: ; preds = %19
%28 = load i8**, i8*** %5
%29 = load i8*, i8** %28
%30 = getelementptr inbounds i8, i8* %29, i64 1
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp slt i32 %32, 1
br i1 %33, label %34, label %35
34: ; preds = %27
store i32 10, i32* %6
br label %35
35: ; preds = %34, %27
br label %38
36: ; preds = %19, %16
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %85
38: ; preds = %35
br label %39
39: ; preds = %38, %15
store i32 0, i32* %11
br label %40
40: ; preds = %44, %39
%41 = getelementptr inbounds [10000 x i8], [10000 x i8]* %7, i64 0, i64 0
%42 = call i32 @mygetline(i8* %41, i32 10000)
store i32 %42, i32* %9
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %56
44: ; preds = %40
%45 = load i32, i32* %9
%46 = add nsw i32 %45, 1
%47 = call i8* @alloc(i32 %46)
store i8* %47, i8** %10
%48 = load i8*, i8** %10
%49 = getelementptr inbounds [10000 x i8], [10000 x i8]* %7, i64 0, i64 0
%50 = call i32 @strcpy(i8* %48, i8* %49)
%51 = load i8*, i8** %10
%52 = load i32, i32* %11
%53 = add nsw i32 %52, 1
store i32 %53, i32* %11
%54 = sext i32 %52 to i64
%55 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* %8, i64 0, i64 %54
store i8* %51, i8** %55
br label %40
56: ; preds = %40
%57 = load i32, i32* %11
%58 = load i32, i32* %6
%59 = sub nsw i32 %57, %58
store i32 %59, i32* %12
%60 = load i32, i32* %12
%61 = icmp sge i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %56
%63 = load i32, i32* %12
br label %65
64: ; preds = %56
br label %65
65: ; preds = %64, %62
%66 = phi i32 [ %63, %62 ], [ 0, %64 ]
store i32 %66, i32* %12
br label %67
67: ; preds = %77, %65
%68 = load i32, i32* %6
%69 = add nsw i32 %68, -1
store i32 %69, i32* %6
%70 = icmp sgt i32 %68, 0
br i1 %70, label %71, label %75
71: ; preds = %67
%72 = load i32, i32* %12
%73 = load i32, i32* %11
%74 = icmp sle i32 %72, %73
br label %75
75: ; preds = %71, %67
%76 = phi i1 [ false, %67 ], [ %74, %71 ]
br i1 %76, label %77, label %84
77: ; preds = %75
%78 = load i32, i32* %12
%79 = add nsw i32 %78, 1
store i32 %79, i32* %12
%80 = sext i32 %78 to i64
%81 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* %8, i64 0, i64 %80
%82 = load i8*, i8** %81
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %82)
br label %67
84: ; preds = %75
store i32 0, i32* %3
br label %85
85: ; preds = %84, %36
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%s\00"
@str = [15 x i8] c"Usage: tail -n\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [10000 x i8]
%4 = alloca [50000 x i8*]
switch i32 %0, label %15 [
i32 1, label %17
i32 2, label %5
]
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 45
br i1 %9, label %10, label %15
10: ; preds = %5
%11 = getelementptr inbounds i8, i8* %7, i64 1
%12 = tail call i32 @atoi(i8* %11)
%13 = icmp slt i32 %12, 1
%14 = select i1 %13, i32 10, i32 %12
br label %17
15: ; preds = %2, %5
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %58
17: ; preds = %2, %10
%18 = phi i32 [ %14, %10 ], [ 10, %2 ]
%19 = getelementptr inbounds [10000 x i8], [10000 x i8]* %3, i64 0, i64 0
%20 = bitcast [50000 x i8*]* %4 to i8*
%21 = call i32 @mygetline(i8* %19, i32 10000)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %35
23: ; preds = %17, %23
%24 = phi i64 [ %29, %23 ], [ 0, %17 ]
%25 = phi i32 [ %31, %23 ], [ %21, %17 ]
%26 = add nuw nsw i32 %25, 1
%27 = call i8* @alloc(i32 %26)
%28 = call i32 @strcpy(i8* %27, i8* %19)
%29 = add nuw i64 %24, 1
%30 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* %4, i64 0, i64 %24
store i8* %27, i8** %30
%31 = call i32 @mygetline(i8* %19, i32 10000)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %23, label %33
33: ; preds = %23
%34 = trunc i64 %29 to i32
br label %35
35: ; preds = %33, %17
%36 = phi i32 [ 0, %17 ], [ %34, %33 ]
%37 = sub nsw i32 %36, %18
%38 = icmp sgt i32 %37, 0
%39 = select i1 %38, i32 %37, i32 0
%40 = icmp sgt i32 %18, 0
%41 = icmp ule i32 %39, %36
%42 = select i1 %40, i1 %41, i1 false
br i1 %42, label %43, label %57
43: ; preds = %35
%44 = zext i32 %39 to i64
%45 = zext i32 %36 to i64
br label %46
46: ; preds = %43, %46
%47 = phi i64 [ %44, %43 ], [ %50, %46 ]
%48 = phi i32 [ %18, %43 ], [ %49, %46 ]
%49 = add nsw i32 %48, -1
%50 = add nuw nsw i64 %47, 1
%51 = getelementptr inbounds [50000 x i8*], [50000 x i8*]* %4, i64 0, i64 %47
%52 = load i8*, i8** %51
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %52)
%54 = icmp ugt i32 %48, 1
%55 = icmp ult i64 %47, %45
%56 = select i1 %54, i1 %55, i1 false
br i1 %56, label %46, label %57
57: ; preds = %46, %35
br label %58
58: ; preds = %57, %15
%59 = phi i32 [ 0, %57 ], [ 1, %15 ]
ret i32 %59
}