#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 lgetline(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int readlines(char *lineptr[], char *alloc_lines, int maxlines) {
  int len, nlines;
  char *p, line[1000];
  p = alloc_lines;
  nlines = 0;
  while ((len = lgetline(line, 1000)) > 0)
    if (nlines >= maxlines || (10000 + alloc_lines - p < len))
      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    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x420(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <lgetline@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    11e8 <readlines+0xc8>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    118c <readlines+0x6c>
mov    -0x18(%rbp),%rax
add    $0x2710,%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
movslq -0x20(%rbp),%rcx
cmp    %rcx,%rax
jge    1198 <readlines+0x78>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <readlines+0xce>
mov    -0x20(%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    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    1145 <readlines+0x25>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%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,%r13
mov    %rdi,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
call   1040 <lgetline@plt>
test   %eax,%eax
jle    11c0 <readlines+0xa0>
lea    0x2710(%r13),%rbp
xor    %ebx,%ebx
test   %r14d,%r14d
cmovle %ebx,%r14d
lea    0x10(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0xffffffff,%ecx
cmp    %rbx,%r14
je     11c2 <readlines+0xa2>
mov    %rbp,%rdx
sub    %r13,%rdx
mov    %eax,%r15d
cmp    %r15,%rdx
jl     11c2 <readlines+0xa2>
add    $0xffffffff,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r13,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    0x8(%rsp),%rax
mov    %r13,(%rax,%rbx,8)
add    $0x1,%rbx
add    %r15,%r13
mov    %r12,%rdi
mov    $0x3e8,%esi
call   1040 <lgetline@plt>
test   %eax,%eax
jg     1170 <readlines+0x50>
mov    %ebx,%ecx
jmp    11c2 <readlines+0xa2>
xor    %ecx,%ecx
mov    %ecx,%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]
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i8*, i8** %6
store i8* %12, i8** %10
store i32 0, i32* %9
br label %13
13: ; preds = %50, %3
%14 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%15 = call i32 @lgetline(i8* %14, i32 1000)
store i32 %15, i32* %8
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %51
17: ; preds = %13
%18 = load i32, i32* %9
%19 = load i32, i32* %7
%20 = icmp sge i32 %18, %19
br i1 %20, label %31, label %21
21: ; preds = %17
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i64 10000
%24 = load i8*, i8** %10
%25 = ptrtoint i8* %23 to i64
%26 = ptrtoint i8* %24 to i64
%27 = sub i64 %25, %26
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = icmp slt i64 %27, %29
br i1 %30, label %31, label %32
31: ; preds = %21, %17
store i32 -1, i32* %4
br label %53
32: ; preds = %21
%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 %13
51: ; preds = %13
%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, i8* %1, i32 %2) {
%4 = alloca [1000 x i8]
%5 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%6 = getelementptr inbounds i8, i8* %1, i64 10000
%7 = ptrtoint i8* %6 to i64
%8 = call i32 @lgetline(i8* %5, i32 1000)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %35
10: ; preds = %3
%11 = call i32 @llvm.smax.i32(i32 %2, i32 0)
%12 = zext i32 %11 to i64
br label %13
13: ; preds = %10, %23
%14 = phi i64 [ 0, %10 ], [ %28, %23 ]
%15 = phi i32 [ %8, %10 ], [ %31, %23 ]
%16 = phi i8* [ %1, %10 ], [ %30, %23 ]
%17 = icmp eq i64 %14, %12
br i1 %17, label %35, label %18
18: ; preds = %13
%19 = ptrtoint i8* %16 to i64
%20 = sub i64 %7, %19
%21 = zext i32 %15 to i64
%22 = icmp slt i64 %20, %21
br i1 %22, label %35, label %23
23: ; preds = %18
%24 = add nsw i32 %15, -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* %16, i8* %5)
%28 = add nuw nsw i64 %14, 1
%29 = getelementptr inbounds i8*, i8** %0, i64 %14
store i8* %16, i8** %29
%30 = getelementptr inbounds i8, i8* %16, i64 %21
%31 = call i32 @lgetline(i8* %5, i32 1000)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %13, label %33
33: ; preds = %23
%34 = trunc i64 %28 to i32
br label %35
35: ; preds = %13, %18, %33, %3
%36 = phi i32 [ 0, %3 ], [ %34, %33 ], [ -1, %18 ], [ -1, %13 ]
ret i32 %36
}