#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 _getline(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int readlines(char *lineptr[], char *buf, int maxlines) {
  int len, nlines;
  char *p, line[1000];
  size_t bufsize = 1000 * 100;
  int index = 0;
  nlines = 0;
  p = buf;
  while ((len = _getline(line, 1000)) > 0 && index + len < bufsize) {
    if (nlines >= maxlines)
      return -1;
    else {
      line[len - 1] = '\0';
      strcpy(p + index, line);
      lineptr[nlines++] = p + index;
      index += len;
    }
  }
  return nlines;
}


x86 assembly (clang, -O0):
<readlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x186a0,-0x428(%rbp)
movl   $0x0,-0x42c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x420(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <_getline@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x42d(%rbp)
jle    119c <readlines+0x7c>
mov    -0x42c(%rbp),%eax
add    -0x20(%rbp),%eax
cltq
cmp    -0x428(%rbp),%rax
setb   %al
mov    %al,-0x42d(%rbp)
mov    -0x42d(%rbp),%al
test   $0x1,%al
jne    11af <readlines+0x8f>
jmp    1229 <readlines+0x109>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     11c7 <readlines+0xa7>
movl   $0xffffffff,-0x4(%rbp)
jmp    122f <readlines+0x10f>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x30(%rbp),%rdi
movslq -0x42c(%rbp),%rax
add    %rax,%rdi
lea    -0x420(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rdx
movslq -0x42c(%rbp),%rax
add    %rax,%rdx
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%eax
add    -0x42c(%rbp),%eax
mov    %eax,-0x42c(%rbp)
jmp    115a <readlines+0x3a>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlines>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f8,%rsp
mov    %edx,%r14d
mov    %rsi,0x8(%rsp)
mov    %rdi,%r12
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
call   1040 <_getline@plt>
xor    %ebx,%ebx
test   %eax,%eax
jle    11b0 <readlines+0x90>
test   %r14d,%r14d
cmovle %ebx,%r14d
lea    0x10(%rsp),%r13
xor    %ecx,%ecx
nop
lea    (%rax,%rcx,1),%r15d
cmp    $0x1869f,%r15d
ja     11b0 <readlines+0x90>
cmp    %rbx,%r14
je     11ab <readlines+0x8b>
add    $0xffffffff,%eax
movb   $0x0,0x10(%rsp,%rax,1)
movslq %ecx,%rbp
add    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
mov    %rbp,(%r12,%rbx,8)
add    $0x1,%rbx
mov    %r13,%rdi
mov    $0x3e8,%esi
call   1040 <_getline@plt>
mov    %r15d,%ecx
test   %eax,%eax
jg     1160 <readlines+0x40>
jmp    11b0 <readlines+0x90>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x3f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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