#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 mygetline(char *, int);
int readlines(char *lineptr[], char *line, int maxlines) {
  int len, nlines;
  nlines = 0;
  while ((len = mygetline(line, 1000)) > 0) {
    if (nlines >= maxlines)
      return -1;
    else {
      line[len - 1] = '\0';
      lineptr[nlines++] = line;
      line = line + len;
    }
  }
  return nlines;
}


x86 assembly (clang, -O0):
<readlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x3e8,%esi
call   1030 <mygetline@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    119b <readlines+0x8b>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     115c <readlines+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a1 <readlines+0x91>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%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    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    112a <readlines+0x1a>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlines>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsi,%rdi
mov    $0x3e8,%esi
call   1030 <mygetline@plt>
xor    %r12d,%r12d
test   %eax,%eax
jle    1172 <readlines+0x62>
test   %r14d,%r14d
cmovle %r12d,%r14d
nopl   0x0(%rax)
cmp    %r12,%r14
je     116c <readlines+0x5c>
lea    -0x1(%rax),%ecx
movb   $0x0,(%rbx,%rcx,1)
mov    %rbx,(%r15,%r12,8)
add    $0x1,%r12
mov    %eax,%eax
add    %rax,%rbx
mov    %rbx,%rdi
mov    $0x3e8,%esi
call   1030 <mygetline@plt>
test   %eax,%eax
jg     1140 <readlines+0x30>
jmp    1172 <readlines+0x62>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
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
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
br label %10
10: ; preds = %35, %3
%11 = load i8*, i8** %6
%12 = call i32 @mygetline(i8* %11, i32 1000)
store i32 %12, i32* %8
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %36
14: ; preds = %10
%15 = load i32, i32* %9
%16 = load i32, i32* %7
%17 = icmp sge i32 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 -1, i32* %4
br label %38
19: ; preds = %14
%20 = load i8*, i8** %6
%21 = load i32, i32* %8
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
store i8 0, i8* %24
%25 = load i8*, i8** %6
%26 = load i8**, i8*** %5
%27 = load i32, i32* %9
%28 = add nsw i32 %27, 1
store i32 %28, i32* %9
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i8*, i8** %26, i64 %29
store i8* %25, i8** %30
%31 = load i8*, i8** %6
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8* %34, i8** %6
br label %35
35: ; preds = %19
br label %10
36: ; preds = %10
%37 = load i32, i32* %9
store i32 %37, i32* %4
br label %38
38: ; preds = %36, %18
%39 = load i32, i32* %4
ret i32 %39
}

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