#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 my_getline(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int readlines_fast(char *lineptr[], int maxlines, char storage[]) {
  int len, nlines, i;
  char line[1000];
  i = 0;
  nlines = 0;
  while ((len = my_getline(line, 1000)) > 0) {
    if (nlines >= maxlines)
      return -1;
    else {
      line[len - 1] = '\0';
      strcpy(&storage[i], line);
      lineptr[nlines++] = &storage[i];
      i += 1000;
    }
  }
  return nlines;
}


x86 assembly (clang, -O0):
<readlines_fast>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x420(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <my_getline@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    11d1 <readlines_fast+0xb1>
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1179 <readlines_fast+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <readlines_fast+0xb7>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x20(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
lea    -0x420(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdx
movslq -0x2c(%rbp),%rax
add    %rax,%rdx
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x3e8,%eax
mov    %eax,-0x2c(%rbp)
jmp    1144 <readlines_fast+0x24>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlines_fast>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f0,%rsp
mov    %rdx,%r12
mov    %esi,%r14d
mov    %rdi,%r15
mov    %rsp,%rdi
mov    $0x3e8,%esi
call   1040 <my_getline@plt>
xor    %ebx,%ebx
test   %eax,%eax
jle    119e <readlines_fast+0x7e>
test   %r14d,%r14d
cmovle %ebx,%r14d
mov    %rsp,%r13
cs nopw 0x0(%rax,%rax,1)
cmp    %rbx,%r14
je     1199 <readlines_fast+0x79>
add    $0xffffffff,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %r12,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    %r12,(%r15,%rbx,8)
add    $0x1,%rbx
mov    %r13,%rdi
mov    $0x3e8,%esi
call   1040 <my_getline@plt>
add    $0x3e8,%r12
test   %eax,%eax
jg     1160 <readlines_fast+0x40>
jmp    119e <readlines_fast+0x7e>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x3f0,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @readlines_fast(i8** %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1000 x i8]
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 0, i32* %10
store i32 0, i32* %9
br label %12
12: ; preds = %43, %3
%13 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%14 = call i32 @my_getline(i8* %13, i32 1000)
store i32 %14, i32* %8
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %44
16: ; preds = %12
%17 = load i32, i32* %9
%18 = load i32, i32* %6
%19 = icmp sge i32 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -1, i32* %4
br label %46
21: ; preds = %16
%22 = load i32, i32* %8
%23 = sub nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 %24
store i8 0, i8* %25
%26 = load i8*, i8** %7
%27 = load i32, i32* %10
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%31 = call i32 @strcpy(i8* %29, i8* %30)
%32 = load i8*, i8** %7
%33 = load i32, i32* %10
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8**, i8*** %5
%37 = load i32, i32* %9
%38 = add nsw i32 %37, 1
store i32 %38, i32* %9
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i8*, i8** %36, i64 %39
store i8* %35, i8** %40
%41 = load i32, i32* %10
%42 = add nsw i32 %41, 1000
store i32 %42, i32* %10
br label %43
43: ; preds = %21
br label %12
44: ; preds = %12
%45 = load i32, i32* %9
store i32 %45, i32* %4
br label %46
46: ; preds = %44, %20
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
define i32 @readlines_fast(i8** nocapture writeonly %0, i32 %1, i8* %2) {
%4 = alloca [1000 x i8]
%5 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%6 = call i32 @my_getline(i8* %5, i32 1000)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %29
8: ; preds = %3
%9 = call i32 @llvm.smax.i32(i32 %1, i32 0)
%10 = zext i32 %9 to i64
br label %11
11: ; preds = %8, %16
%12 = phi i64 [ 0, %8 ], [ %22, %16 ]
%13 = phi i64 [ 0, %8 ], [ %24, %16 ]
%14 = phi i32 [ %6, %8 ], [ %25, %16 ]
%15 = icmp eq i64 %12, %10
br i1 %15, label %29, label %16
16: ; preds = %11
%17 = add nsw i32 %14, -1
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 %18
store i8 0, i8* %19
%20 = getelementptr inbounds i8, i8* %2, i64 %13
%21 = call i32 @strcpy(i8* %20, i8* %5)
%22 = add nuw nsw i64 %12, 1
%23 = getelementptr inbounds i8*, i8** %0, i64 %12
store i8* %20, i8** %23
%24 = add nuw nsw i64 %13, 1000
%25 = call i32 @my_getline(i8* %5, i32 1000)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %11, label %27
27: ; preds = %16
%28 = trunc i64 %22 to i32
br label %29
29: ; preds = %11, %27, %3
%30 = phi i32 [ 0, %3 ], [ %28, %27 ], [ -1, %11 ]
ret i32 %30
}