#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);
char *malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
int readlines(char *lineptr[], int maxlines) {
  char line[1000], *p;
  int len, nlines = 0;
  while ((len = _getline(line, 1000)) > 0) {
    if (nlines > maxlines || (p = malloc(len)) == NULL) {
      return -1;
    } else {
      line[len - 1] = '\0';
      strcpy(p, line);
      lineptr[nlines++] = p;
    }
  }
  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,-0x410(%rbp)
lea    -0x400(%rbp),%rdi
mov    $0x3e8,%esi
call   1050 <_getline@plt>
mov    %eax,-0x40c(%rbp)
cmp    $0x0,%eax
jle    11f1 <readlines+0xc1>
mov    -0x410(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     1197 <readlines+0x67>
mov    -0x40c(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x408(%rbp)
cmp    $0x0,%rax
jne    11a3 <readlines+0x73>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <readlines+0xca>
mov    -0x40c(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x400(%rbp,%rax,1)
mov    -0x408(%rbp),%rdi
lea    -0x400(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x408(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x410(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x410(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    114c <readlines+0x1c>
mov    -0x410(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlines>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3e8,%rsp
mov    %esi,%r13d
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x3e8,%esi
call   1050 <_getline@plt>
test   %eax,%eax
jle    11bd <readlines+0x8d>
mov    %eax,%ebp
test   %r13d,%r13d
mov    $0xffffffff,%eax
cmovs  %eax,%r13d
add    $0x1,%r13d
xor    %r15d,%r15d
mov    %rsp,%r12
cmp    %r15,%r13
je     11b6 <readlines+0x86>
mov    %ebp,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11b6 <readlines+0x86>
mov    %rax,%rbx
add    $0xffffffff,%ebp
movb   $0x0,(%rsp,%rbp,1)
mov    %rax,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %rbx,(%r14,%r15,8)
add    $0x1,%r15
mov    %r12,%rdi
mov    $0x3e8,%esi
call   1050 <_getline@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1170 <readlines+0x40>
mov    %r15d,%eax
jmp    11bf <readlines+0x8f>
mov    $0xffffffff,%eax
jmp    11bf <readlines+0x8f>
xor    %eax,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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