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


x86 assembly (clang, -O0):
<readlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
add    $0x4c4b40,%rax
mov    %rax,-0x428(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x420(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <get_line@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    11eb <readlines+0xcb>
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    118f <readlines+0x6f>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
add    %rcx,%rax
cmp    -0x428(%rbp),%rax
jb     119b <readlines+0x7b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <readlines+0xd1>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x18(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1030 <strcpy@plt>
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    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    114e <readlines+0x2e>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%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    %edx,%r13d
mov    %rsi,%r14
mov    %rdi,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
call   1040 <get_line@plt>
test   %eax,%eax
jle    11c1 <readlines+0xa1>
xor    %ebp,%ebp
test   %r13d,%r13d
cmovle %ebp,%r13d
lea    0x10(%rsp),%r12
mov    %r14,%rbx
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0xffffffff,%ecx
cmp    %rbp,%r13
je     11c3 <readlines+0xa3>
mov    %eax,%edx
add    %rdx,%r15
cmp    $0x4c4b3f,%r15
ja     11c3 <readlines+0xa3>
add    $0xffffffff,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    0x8(%rsp),%rax
mov    %rbx,(%rax,%rbp,8)
add    $0x1,%rbp
lea    (%r14,%r15,1),%rbx
mov    %r12,%rdi
mov    $0x3e8,%esi
call   1040 <get_line@plt>
test   %eax,%eax
jg     1170 <readlines+0x50>
mov    %ebp,%ecx
jmp    11c3 <readlines+0xa3>
xor    %ecx,%ecx
mov    %ecx,%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, 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]
%12 = alloca i8*
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%13 = load i8*, i8** %6
%14 = getelementptr inbounds i8, i8* %13, i64 5000000
store i8* %14, i8** %12
store i32 0, i32* %9
br label %15
15: ; preds = %49, %3
%16 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%17 = call i32 @get_line(i8* %16, i32 1000)
store i32 %17, i32* %8
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %50
19: ; preds = %15
%20 = load i32, i32* %9
%21 = load i32, i32* %7
%22 = icmp sge i32 %20, %21
br i1 %22, label %30, label %23
23: ; preds = %19
%24 = load i8*, i8** %6
%25 = load i32, i32* %8
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8*, i8** %12
%29 = icmp uge i8* %27, %28
br i1 %29, label %30, label %31
30: ; preds = %23, %19
store i32 -1, i32* %4
br label %52
31: ; preds = %23
%32 = load i32, i32* %8
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %6
%37 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%38 = call i32 @strcpy(i8* %36, i8* %37)
%39 = load i8*, i8** %6
%40 = load i8**, i8*** %5
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
%43 = sext i32 %41 to i64
%44 = getelementptr inbounds i8*, i8** %40, i64 %43
store i8* %39, i8** %44
%45 = load i32, i32* %8
%46 = load i8*, i8** %6
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8* %48, i8** %6
br label %49
49: ; preds = %31
br label %15
50: ; preds = %15
%51 = load i32, i32* %9
store i32 %51, i32* %4
br label %52
52: ; preds = %50, %30
%53 = load i32, i32* %4
ret i32 %53
}

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