#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_ptr(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int _readlines(char *lineptr[], int maxlines, char *cache) {
  int len, nlines;
  char *p, line[1000];
  p = cache + strlen(cache);
  nlines = 0;
  while ((len = _getline_ptr(line, 1000)) > 0)
    if (nlines >= maxlines || (strlen(cache) + len) > 10000)
      return -1;
    else {
      line[len - 1] = '\0';
      strcpy(p, line);
      lineptr[nlines++] = p;
      p += len;
    }
  return nlines;
}


x86 assembly (clang, -O0):
<_readlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x428(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x420(%rbp),%rdi
mov    $0x3e8,%esi
call   1050 <_getline_ptr@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    1210 <_readlines+0xe0>
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b4 <_readlines+0x84>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x24(%rbp),%eax
cmp    $0x2710,%eax
jle    11c0 <_readlines+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <_readlines+0xe6>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x30(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%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    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    1174 <_readlines+0x44>
mov    -0x28(%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    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,0x8(%rsp)
mov    %rdx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
call   1050 <_getline_ptr@plt>
test   %eax,%eax
jle    11e2 <_readlines+0xb2>
mov    %eax,%r13d
movslq %ebx,%rbx
add    %r14,%rbx
xor    %ebp,%ebp
test   %r12d,%r12d
cmovle %ebp,%r12d
nopl   0x0(%rax,%rax,1)
mov    $0xffffffff,%r15d
cmp    %rbp,%r12
je     11e5 <_readlines+0xb5>
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %r13d,%eax
cmp    $0x2710,%eax
jg     11e5 <_readlines+0xb5>
lea    -0x1(%r13),%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %rbx,%rdi
lea    0x10(%rsp),%r15
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    0x8(%rsp),%rax
mov    %rbx,(%rax,%rbp,8)
add    $0x1,%rbp
mov    %r13d,%eax
add    %rax,%rbx
mov    %r15,%rdi
mov    $0x3e8,%esi
call   1050 <_getline_ptr@plt>
mov    %eax,%r13d
test   %eax,%eax
jg     1180 <_readlines+0x50>
mov    %ebp,%r15d
jmp    11e5 <_readlines+0xb5>
xor    %r15d,%r15d
mov    %r15d,%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, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [1000 x i8]
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%12 = load i8*, i8** %7
%13 = load i8*, i8** %7
%14 = call i32 @strlen(i8* %13)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %12, i64 %15
store i8* %16, i8** %10
store i32 0, i32* %9
br label %17
17: ; preds = %50, %3
%18 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%19 = call i32 @_getline_ptr(i8* %18, i32 1000)
store i32 %19, i32* %8
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %51
21: ; preds = %17
%22 = load i32, i32* %9
%23 = load i32, i32* %6
%24 = icmp sge i32 %22, %23
br i1 %24, label %31, label %25
25: ; preds = %21
%26 = load i8*, i8** %7
%27 = call i32 @strlen(i8* %26)
%28 = load i32, i32* %8
%29 = add nsw i32 %27, %28
%30 = icmp sgt i32 %29, 10000
br i1 %30, label %31, label %32
31: ; preds = %25, %21
store i32 -1, i32* %4
br label %53
32: ; preds = %25
%33 = load i32, i32* %8
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 %35
store i8 0, i8* %36
%37 = load i8*, i8** %10
%38 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%39 = call i32 @strcpy(i8* %37, i8* %38)
%40 = load i8*, i8** %10
%41 = load i8**, i8*** %5
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
%44 = sext i32 %42 to i64
%45 = getelementptr inbounds i8*, i8** %41, i64 %44
store i8* %40, i8** %45
%46 = load i32, i32* %8
%47 = load i8*, i8** %10
%48 = sext i32 %46 to i64
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8* %49, i8** %10
br label %50
50: ; preds = %32
br label %17
51: ; preds = %17
%52 = load i32, i32* %9
store i32 %52, i32* %4
br label %53
53: ; preds = %51, %31
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
define i32 @_readlines(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 = tail call i32 @strlen(i8* %2)
%7 = call i32 @_getline_ptr(i8* %5, i32 1000)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %36
9: ; preds = %3
%10 = sext i32 %6 to i64
%11 = getelementptr inbounds i8, i8* %2, i64 %10
%12 = call i32 @llvm.smax.i32(i32 %1, i32 0)
%13 = zext i32 %12 to i64
br label %14
14: ; preds = %9, %23
%15 = phi i64 [ 0, %9 ], [ %28, %23 ]
%16 = phi i32 [ %7, %9 ], [ %32, %23 ]
%17 = phi i8* [ %11, %9 ], [ %31, %23 ]
%18 = icmp eq i64 %15, %13
br i1 %18, label %36, label %19
19: ; preds = %14
%20 = call i32 @strlen(i8* %2)
%21 = add nsw i32 %20, %16
%22 = icmp sgt i32 %21, 10000
br i1 %22, label %36, label %23
23: ; preds = %19
%24 = add nsw i32 %16, -1
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 %25
store i8 0, i8* %26
%27 = call i32 @strcpy(i8* %17, i8* %5)
%28 = add nuw nsw i64 %15, 1
%29 = getelementptr inbounds i8*, i8** %0, i64 %15
store i8* %17, i8** %29
%30 = zext i32 %16 to i64
%31 = getelementptr inbounds i8, i8* %17, i64 %30
%32 = call i32 @_getline_ptr(i8* %5, i32 1000)
%33 = icmp sgt i32 %32, 0
br i1 %33, label %14, label %34
34: ; preds = %23
%35 = trunc i64 %28 to i32
br label %36
36: ; preds = %14, %19, %34, %3
%37 = phi i32 [ 0, %3 ], [ %35, %34 ], [ -1, %19 ], [ -1, %14 ]
ret i32 %37
}