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


x86 assembly (clang, -O0):
<anoreadlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
imul   $0x3e8,%rax,%rax
add    %rax,%rdi
mov    $0x3e8,%esi
call   1030 <mygetline@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1194 <anoreadlines+0x84>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1166 <anoreadlines+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    119a <anoreadlines+0x8a>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
movslq %ecx,%rcx
imul   $0x3e8,%rcx,%rcx
add    %rcx,%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1126 <anoreadlines+0x16>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anoreadlines>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
mov    $0x3e8,%esi
call   1030 <mygetline@plt>
xor    %r15d,%r15d
test   %eax,%eax
jle    1174 <anoreadlines+0x64>
test   %r14d,%r14d
cmovle %r15d,%r14d
add    $0x3e8,%rbx
nopw   0x0(%rax,%rax,1)
cmp    %r15,%r14
je     116e <anoreadlines+0x5e>
add    $0x1,%r15
add    $0xffffffff,%eax
movb   $0x0,-0x3e8(%rbx,%rax,1)
mov    %rbx,%rdi
mov    $0x3e8,%esi
call   1030 <mygetline@plt>
add    $0x3e8,%rbx
test   %eax,%eax
jg     1140 <anoreadlines+0x30>
jmp    1174 <anoreadlines+0x64>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @anoreadlines([1000 x i8]* %0, i32 %1) {
%3 = alloca i32
%4 = alloca [1000 x i8]*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store [1000 x i8]* %0, [1000 x i8]** %4
store i32 %1, i32* %5
store i32 0, i32* %7
br label %8
8: ; preds = %31, %2
%9 = load [1000 x i8]*, [1000 x i8]** %4
%10 = load i32, i32* %7
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 %11
%13 = getelementptr inbounds [1000 x i8], [1000 x i8]* %12, i64 0, i64 0
%14 = call i32 @mygetline(i8* %13, i32 1000)
store i32 %14, i32* %6
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %32
16: ; preds = %8
%17 = load i32, i32* %7
%18 = load i32, i32* %5
%19 = icmp sge i32 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -1, i32* %3
br label %34
21: ; preds = %16
%22 = load [1000 x i8]*, [1000 x i8]** %4
%23 = load i32, i32* %7
%24 = add nsw i32 %23, 1
store i32 %24, i32* %7
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds [1000 x i8], [1000 x i8]* %22, i64 %25
%27 = load i32, i32* %6
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [1000 x i8], [1000 x i8]* %26, i64 0, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %21
br label %8
32: ; preds = %8
%33 = load i32, i32* %7
store i32 %33, i32* %3
br label %34
34: ; preds = %32, %20
%35 = load i32, i32* %3
ret i32 %35
}

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