#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 */
char *alloc(int);
int lgetline(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int readlines(char *lineptr[], int maxlines) {
  int len, nlines;
  char *p, line[1000];
  nlines = 0;
  while ((len = lgetline(line, 1000)) > 0)
    if (nlines >= maxlines || (p = alloc(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,-0x1c(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <lgetline@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   1050 <alloc@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   1040 <lgetline@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   1050 <alloc@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   1040 <lgetline@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 = %37, %2
%11 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%12 = call i32 @lgetline(i8* %11, i32 1000)
store i32 %12, i32* %6
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %38
14: ; preds = %10
%15 = load i32, i32* %7
%16 = load i32, i32* %5
%17 = icmp sge i32 %15, %16
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = load i32, i32* %6
%20 = call i8* @alloc(i32 %19)
store i8* %20, i8** %8
%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* %6
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 %26
store i8 0, i8* %27
%28 = load i8*, i8** %8
%29 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%30 = call i32 @strcpy(i8* %28, i8* %29)
%31 = load i8*, i8** %8
%32 = load i8**, i8*** %4
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
%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* %7
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 @lgetline(i8* %4, i32 1000)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %28
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, %17
%11 = phi i64 [ 0, %7 ], [ %22, %17 ]
%12 = phi i32 [ %5, %7 ], [ %24, %17 ]
%13 = icmp eq i64 %11, %9
br i1 %13, label %28, label %14
14: ; preds = %10
%15 = call i8* @alloc(i32 %12)
%16 = icmp eq i8* %15, null
br i1 %16, label %28, label %17
17: ; preds = %14
%18 = add nsw i32 %12, -1
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i32 @strcpy(i8* %15, i8* %4)
%22 = add nuw nsw i64 %11, 1
%23 = getelementptr inbounds i8*, i8** %0, i64 %11
store i8* %15, i8** %23
%24 = call i32 @lgetline(i8* %4, i32 1000)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %10, label %26
26: ; preds = %17
%27 = trunc i64 %22 to i32
br label %28
28: ; preds = %10, %14, %26, %2
%29 = phi i32 [ 0, %2 ], [ %27, %26 ], [ -1, %14 ], [ -1, %10 ]
ret i32 %29
}