#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 *malloc(int);
int mygetline(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int readlines(char *lines[], int numlines) {
  int i = 0;
  int min = 0;
  int j;
  int len = 0;
  char line[1000];
  long lineno = 0;
  while ((len = mygetline(line, 1000)) > 0) {
    lineno++;
    i = (i + 1) % numlines;
    lines[i] = malloc(len);
    strcpy(lines[i], line);
  }
  printf("Found lines %d || %d\n", lineno, numlines);
  if (lineno <= numlines) {
    for (i = 0; i < lineno; i++) {
      printf("%s", lines[i]);
    }
  } else {
    j = numlines;
    while (j-- > 0) {
      printf("***i is[%d]: (((%s)))\n", i, lines[i]);
      printf("%s", lines[++i % numlines]);
    }
  }
  return lineno;
}


x86 assembly (clang, -O0):
<readlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x418(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x3e8,%esi
call   1060 <mygetline@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    11e2 <readlines+0xa2>
mov    -0x418(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x418(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cltd
idivl  -0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,%rdx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    -0x410(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    1172 <readlines+0x32>
mov    -0x418(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x418(%rbp),%rax
movslq -0xc(%rbp),%rcx
cmp    %rcx,%rax
jg     1253 <readlines+0x113>
movl   $0x0,-0x10(%rbp)
movslq -0x10(%rbp),%rax
cmp    -0x418(%rbp),%rax
jge    124e <readlines+0x10e>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1215 <readlines+0xd5>
jmp    12c8 <readlines+0x188>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x18(%rbp)
cmp    $0x0,%eax
jle    12c3 <readlines+0x183>
mov    -0x10(%rbp),%esi
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x420(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
cltd
idivl  -0xc(%rbp)
mov    -0x420(%rbp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1259 <readlines+0x119>
jmp    12c8 <readlines+0x188>
mov    -0x418(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlines>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3e8,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x3e8,%esi
call   1060 <mygetline@plt>
xor    %ebx,%ebx
test   %eax,%eax
jle    11bb <readlines+0x7b>
mov    %eax,%edi
mov    %rsp,%rbp
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%r12
add    $0x1,%ebx
mov    %ebx,%eax
cltd
idiv   %r15d
mov    %edx,%ebx
call   1050 <malloc@plt>
movslq %ebx,%r13
mov    %rax,(%r14,%r13,8)
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    $0x3e8,%esi
call   1060 <mygetline@plt>
mov    %eax,%edi
test   %eax,%eax
jg     1180 <readlines+0x40>
jmp    11c1 <readlines+0x81>
xor    %r13d,%r13d
xor    %r12d,%r12d
lea    0xe38(%rip),%rdi
mov    %r12,%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
movslq %r15d,%rax
cmp    %rax,%r12
jle    122b <readlines+0xeb>
test   %r15d,%r15d
jle    1257 <readlines+0x117>
lea    0x1(%r15),%ebx
lea    0xe29(%rip),%rbp
nopl   (%rax)
mov    (%r14,%r13,8),%rdx
lea    0xe1e(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r13
mov    %r13d,%eax
cltd
idiv   %r15d
movslq %edx,%rax
mov    (%r14,%rax,8),%rsi
mov    %rbp,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
ja     11f0 <readlines+0xb0>
jmp    1257 <readlines+0x117>
test   %r12,%r12
je     1257 <readlines+0x117>
lea    0xddf(%rip),%rbx
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    (%r14,%rbp,8),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%r12
jne    1240 <readlines+0x100>
mov    %r12d,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [22 x i8] c"Found lines %d || %d\0A\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [23 x i8] c"***i is[%d]: (((%s)))\0A\00"
define i32 @readlines(i8** nocapture %0, i32 %1) {
%3 = alloca [1000 x i8]
%4 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%5 = call i32 @mygetline(i8* %4, i32 1000)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %22
7: ; preds = %2, %7
%8 = phi i32 [ %18, %7 ], [ %5, %2 ]
%9 = phi i64 [ %11, %7 ], [ 0, %2 ]
%10 = phi i32 [ %13, %7 ], [ 0, %2 ]
%11 = add nuw nsw i64 %9, 1
%12 = add nsw i32 %10, 1
%13 = srem i32 %12, %1
%14 = call i8* @malloc(i32 %8)
%15 = sext i32 %13 to i64
%16 = getelementptr inbounds i8*, i8** %0, i64 %15
store i8* %14, i8** %16
%17 = call i32 @strcpy(i8* %14, i8* %4)
%18 = call i32 @mygetline(i8* %4, i32 1000)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %7, label %20
20: ; preds = %7
%21 = sext i32 %13 to i64
br label %22
22: ; preds = %20, %2
%23 = phi i64 [ 0, %2 ], [ %21, %20 ]
%24 = phi i64 [ 0, %2 ], [ %11, %20 ]
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i64 %24, i32 %1)
%26 = sext i32 %1 to i64
%27 = icmp sgt i64 %24, %26
br i1 %27, label %30, label %28
28: ; preds = %22
%29 = icmp eq i64 %24, 0
br i1 %29, label %55, label %32
30: ; preds = %22
%31 = icmp sgt i32 %1, 0
br i1 %31, label %39, label %55
32: ; preds = %28, %32
%33 = phi i64 [ %37, %32 ], [ 0, %28 ]
%34 = getelementptr inbounds i8*, i8** %0, i64 %33
%35 = load i8*, i8** %34
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = add nuw i64 %33, 1
%38 = icmp eq i64 %37, %24
br i1 %38, label %55, label %32
39: ; preds = %30, %39
%40 = phi i64 [ %47, %39 ], [ %23, %30 ]
%41 = phi i32 [ %42, %39 ], [ %1, %30 ]
%42 = add nsw i32 %41, -1
%43 = getelementptr inbounds i8*, i8** %0, i64 %40
%44 = load i8*, i8** %43
%45 = trunc i64 %40 to i32
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %45, i8* %44)
%47 = add nsw i64 %40, 1
%48 = trunc i64 %47 to i32
%49 = srem i32 %48, %1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8*, i8** %0, i64 %50
%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 %41, 1
br i1 %54, label %39, label %55
55: ; preds = %32, %39, %28, %30
%56 = trunc i64 %24 to i32
ret i32 %56
}