#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 ggetline(char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
int readlines(char *linep[], int maxlines) {
  int len, nlines;
  char *p, line[1000];
  nlines = 0;
  while ((len = ggetline(line, 1000)) > 0)
    if (nlines >= maxlines || (p = (char *)malloc(len)) == NULL)
      return -1;
    else {
      line[len - 1] = '\0';
      strcpy(p, line);
      linep[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,-0x1c(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x3e8,%esi
call   1050 <ggetline@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    11d3 <readlines+0xa3>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1188 <readlines+0x58>
mov    -0x18(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1194 <readlines+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d9 <readlines+0xa9>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x28(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    1149 <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   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f8,%rsp
mov    %esi,%r12d
mov    %rdi,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
call   1050 <ggetline@plt>
test   %eax,%eax
jle    11cc <readlines+0x9c>
mov    %eax,%ebx
xor    %r14d,%r14d
test   %r12d,%r12d
cmovle %r14d,%r12d
lea    0x10(%rsp),%r15
mov    $0xffffffff,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    %r14,%r12
je     11cf <readlines+0x9f>
mov    %ebx,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11cf <readlines+0x9f>
mov    %rax,%rbp
add    $0xffffffff,%ebx
movb   $0x0,0x10(%rsp,%rbx,1)
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    0x8(%rsp),%rax
mov    %rbp,(%rax,%r14,8)
add    $0x1,%r14
mov    %r15,%rdi
mov    $0x3e8,%esi
call   1050 <ggetline@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     1180 <readlines+0x50>
mov    %r14d,%r13d
jmp    11cf <readlines+0x9f>
xor    %r13d,%r13d
mov    %r13d,%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) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [1000 x i8]
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i32 0, i32* %7
br label %10
10: ; preds = %38, %2
%11 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%12 = call i32 @ggetline(i8* %11, i32 1000)
store i32 %12, i32* %6
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %39
14: ; preds = %10
%15 = load i32, i32* %7
%16 = load i32, i32* %5
%17 = icmp sge i32 %15, %16
br i1 %17, label %23, label %18
18: ; preds = %14
%19 = load i32, i32* %6
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %8
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %18, %14
store i32 -1, i32* %3
br label %41
24: ; preds = %18
%25 = load i32, i32* %6
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 %27
store i8 0, i8* %28
%29 = load i8*, i8** %8
%30 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%31 = call i32 @strcpy(i8* %29, i8* %30)
%32 = load i8*, i8** %8
%33 = load i8**, i8*** %4
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
store i32 %35, i32* %7
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8*, i8** %33, i64 %36
store i8* %32, i8** %37
br label %38
38: ; preds = %24
br label %10
39: ; preds = %10
%40 = load i32, i32* %7
store i32 %40, i32* %3
br label %41
41: ; preds = %39, %23
%42 = load i32, i32* %3
ret i32 %42
}

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 @ggetline(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 0)
%9 = zext i32 %8 to i64
br label %10
10: ; preds = %7, %18
%11 = phi i64 [ 0, %7 ], [ %23, %18 ]
%12 = phi i32 [ %5, %7 ], [ %25, %18 ]
%13 = icmp eq i64 %11, %9
br i1 %13, label %29, label %14
14: ; preds = %10
%15 = call i64 @malloc(i32 %12)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i64 %15, 0
br i1 %17, label %29, label %18
18: ; preds = %14
%19 = add nsw i32 %12, -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 %11, 1
%24 = getelementptr inbounds i8*, i8** %0, i64 %11
store i8* %16, i8** %24
%25 = call i32 @ggetline(i8* %4, i32 1000)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %10, label %27
27: ; preds = %18
%28 = trunc i64 %23 to i32
br label %29
29: ; preds = %10, %14, %27, %2
%30 = phi i32 [ 0, %2 ], [ %28, %27 ], [ -1, %14 ], [ -1, %10 ]
ret i32 %30
}