#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 mgetline(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int readlines(char *lineptr[], char *buff, int maxlines) {
  int len, nlines;
  char *p, line[1000];
  nlines = 0;
  p = buff;
  while ((len = mgetline(line, 1000)) > 0) {
    if (nlines >= maxlines) {
      return -1;
    } else {
      line[len - 1] = '\0';
      strcpy(p, line);
      lineptr[nlines++] = p;
      p += 1000;
    }
  }
  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)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x420(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <mgetline@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    11c7 <readlines+0xa7>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     117a <readlines+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <readlines+0xad>
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    -0x30(%rbp),%rax
add    $0x3e8,%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   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f0,%rsp
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rsp,%rdi
mov    $0x3e8,%esi
call   1040 <mgetline@plt>
xor    %ebx,%ebx
test   %eax,%eax
jle    119e <readlines+0x7e>
test   %r14d,%r14d
cmovle %ebx,%r14d
mov    %rsp,%r13
cs nopw 0x0(%rax,%rax,1)
cmp    %rbx,%r14
je     1199 <readlines+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
add    $0x3e8,%r12
mov    %r13,%rdi
mov    $0x3e8,%esi
call   1040 <mgetline@plt>
test   %eax,%eax
jg     1160 <readlines+0x40>
jmp    119e <readlines+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(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
store i32 0, i32* %9
%12 = load i8*, i8** %6
store i8* %12, i8** %10
br label %13
13: ; preds = %38, %3
%14 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%15 = call i32 @mgetline(i8* %14, i32 1000)
store i32 %15, i32* %8
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %39
17: ; preds = %13
%18 = load i32, i32* %9
%19 = load i32, i32* %7
%20 = icmp sge i32 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 -1, i32* %4
br label %41
22: ; preds = %17
%23 = load i32, i32* %8
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 %25
store i8 0, i8* %26
%27 = load i8*, i8** %10
%28 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%29 = call i32 @strcpy(i8* %27, i8* %28)
%30 = load i8*, i8** %10
%31 = load i8**, i8*** %5
%32 = load i32, i32* %9
%33 = add nsw i32 %32, 1
store i32 %33, i32* %9
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i8*, i8** %31, i64 %34
store i8* %30, i8** %35
%36 = load i8*, i8** %10
%37 = getelementptr inbounds i8, i8* %36, i64 1000
store i8* %37, i8** %10
br label %38
38: ; preds = %22
br label %13
39: ; preds = %13
%40 = load i32, i32* %9
store i32 %40, i32* %4
br label %41
41: ; preds = %39, %21
%42 = load i32, i32* %4
ret i32 %42
}

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 @mgetline(i8* %5, i32 1000)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %28
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, %16
%12 = phi i64 [ 0, %8 ], [ %21, %16 ]
%13 = phi i32 [ %6, %8 ], [ %24, %16 ]
%14 = phi i8* [ %1, %8 ], [ %23, %16 ]
%15 = icmp eq i64 %12, %10
br i1 %15, label %28, label %16
16: ; preds = %11
%17 = add nsw i32 %13, -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 = call i32 @strcpy(i8* %14, i8* %5)
%21 = add nuw nsw i64 %12, 1
%22 = getelementptr inbounds i8*, i8** %0, i64 %12
store i8* %14, i8** %22
%23 = getelementptr inbounds i8, i8* %14, i64 1000
%24 = call i32 @mgetline(i8* %5, i32 1000)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %11, label %26
26: ; preds = %16
%27 = trunc i64 %21 to i32
br label %28
28: ; preds = %11, %26, %3
%29 = phi i32 [ 0, %3 ], [ %27, %26 ], [ -1, %11 ]
ret i32 %29
}