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


x86 assembly (clang, -O0):
<readlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x420(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <get_line@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    11e2 <readlines+0xc2>
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    117a <readlines+0x5a>
cmpl   $0x7a120,-0x2c(%rbp)
jl     1186 <readlines+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <readlines+0xc8>
mov    -0x24(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1144 <readlines+0x24>
mov    -0x28(%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    %rdx,%r12
mov    %esi,%ebp
mov    %rdi,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
call   1040 <get_line@plt>
test   %eax,%eax
jle    11b4 <readlines+0x94>
mov    %eax,%ebx
movslq %ebp,%r13
xor    %ebp,%ebp
lea    0x10(%rsp),%r15
xor    %r14d,%r14d
nopl   (%rax)
mov    $0xffffffff,%eax
cmp    %r13,%rbp
jge    11b6 <readlines+0x96>
cmp    $0x7a11f,%r14d
jg     11b6 <readlines+0x96>
add    %ebx,%r14d
lea    -0x1(%rbx),%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    0x8(%rsp),%rax
mov    %r12,(%rax,%rbp,8)
add    $0x1,%rbp
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
mov    %ebx,%eax
add    %rax,%r12
mov    %r15,%rdi
mov    $0x3e8,%esi
call   1040 <get_line@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     1160 <readlines+0x40>
mov    %ebp,%eax
jmp    11b6 <readlines+0x96>
xor    %eax,%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, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1000 x i8]
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 0, i32* %9
store i32 0, i32* %10
br label %12
12: ; preds = %46, %3
%13 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%14 = call i32 @get_line(i8* %13, i32 1000)
store i32 %14, i32* %8
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %47
16: ; preds = %12
%17 = load i32, i32* %9
%18 = load i32, i32* %6
%19 = icmp sge i32 %17, %18
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i32, i32* %10
%22 = icmp sge i32 %21, 500000
br i1 %22, label %23, label %24
23: ; preds = %20, %16
store i32 -1, i32* %4
br label %49
24: ; preds = %20
%25 = load i32, i32* %8
%26 = load i32, i32* %10
%27 = add nsw i32 %26, %25
store i32 %27, i32* %10
%28 = load i32, i32* %8
%29 = sub nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %7
%33 = load i8**, i8*** %5
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
store i32 %35, i32* %9
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8*, i8** %33, i64 %36
store i8* %32, i8** %37
%38 = load i8*, i8** %7
%39 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%40 = load i32, i32* %8
%41 = call i32 @strncpy(i8* %38, i8* %39, i32 %40)
%42 = load i32, i32* %8
%43 = load i8*, i8** %7
%44 = sext i32 %42 to i64
%45 = getelementptr inbounds i8, i8* %43, i64 %44
store i8* %45, i8** %7
br label %46
46: ; preds = %24
br label %12
47: ; preds = %12
%48 = load i32, i32* %9
store i32 %48, i32* %4
br label %49
49: ; preds = %47, %23
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
define i32 @readlines(i8** nocapture writeonly %0, i32 %1, i8* %2) {
%4 = alloca [1000 x i8]
%5 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%6 = call i32 @get_line(i8* %5, i32 1000)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %32
8: ; preds = %3
%9 = sext i32 %1 to i64
br label %10
10: ; preds = %8, %18
%11 = phi i64 [ 0, %8 ], [ %23, %18 ]
%12 = phi i32 [ %6, %8 ], [ %28, %18 ]
%13 = phi i32 [ 0, %8 ], [ %19, %18 ]
%14 = phi i8* [ %2, %8 ], [ %27, %18 ]
%15 = icmp sge i64 %11, %9
%16 = icmp sgt i32 %13, 499999
%17 = select i1 %15, i1 true, i1 %16
br i1 %17, label %32, label %18
18: ; preds = %10
%19 = add nsw i32 %12, %13
%20 = add nsw i32 %12, -1
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 %21
store i8 0, i8* %22
%23 = add nuw nsw i64 %11, 1
%24 = getelementptr inbounds i8*, i8** %0, i64 %11
store i8* %14, i8** %24
%25 = call i32 @strncpy(i8* %14, i8* %5, i32 %12)
%26 = zext i32 %12 to i64
%27 = getelementptr inbounds i8, i8* %14, i64 %26
%28 = call i32 @get_line(i8* %5, i32 1000)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %10, label %30
30: ; preds = %18
%31 = trunc i64 %23 to i32
br label %32
32: ; preds = %10, %30, %3
%33 = phi i32 [ 0, %3 ], [ %31, %30 ], [ -1, %10 ]
ret i32 %33
}