#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(char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int readlines(char lines[][1000], int maxlen) {
  int len, nlines;
  char line[1000];
  nlines = 0;
  while ((len = getline(line, 1000)) > 0) {
    if (nlines < maxlen) {
      line[len - 1] = '\0';
      strncpy(lines[nlines], line, len);
      nlines++;
    } else {
      return -1;
    }
  }
  return nlines;
}


x86 assembly (clang, -O0):
<readlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <getline@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    11b2 <readlines+0x92>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a1 <readlines+0x81>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
imul   $0x3e8,%rax,%rax
add    %rax,%rdi
lea    -0x410(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ad <readlines+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <readlines+0x98>
jmp    1139 <readlines+0x19>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlines>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x3e8,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    %rsp,%rdi
mov    $0x3e8,%esi
call   1040 <getline@plt>
xor    %ebx,%ebx
test   %eax,%eax
jle    119c <readlines+0x7c>
test   %r14d,%r14d
cmovle %ebx,%r14d
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %rbx,%r14
je     1197 <readlines+0x77>
lea    -0x1(%rax),%ecx
movb   $0x0,(%rsp,%rcx,1)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
add    $0x1,%rbx
mov    %r12,%rdi
mov    $0x3e8,%esi
call   1040 <getline@plt>
add    $0x3e8,%r15
test   %eax,%eax
jg     1160 <readlines+0x40>
jmp    119c <readlines+0x7c>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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