#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 get_line(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int readlines(char *lineptr[], int maxlines) {
  int len, nlines;
  char *p, line[1000];
  nlines = 0;
  while ((len = get_line(line, 1000)) > 0)
    if (nlines >= 5000 || (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 <get_line@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    11d4 <readlines+0xa4>
cmpl   $0x1388,-0x1c(%rbp)
jge    1189 <readlines+0x59>
mov    -0x18(%rbp),%edi
call   1050 <alloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1195 <readlines+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <readlines+0xaa>
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    $0x3e8,%rsp
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x3e8,%esi
call   1040 <get_line@plt>
test   %eax,%eax
jle    11ba <readlines+0x8a>
mov    %eax,%ebx
xor    %r13d,%r13d
mov    %rsp,%r15
mov    $0xffffffff,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0x1388,%r13
je     11bd <readlines+0x8d>
mov    %ebx,%edi
call   1050 <alloc@plt>
test   %rax,%rax
je     11bd <readlines+0x8d>
mov    %rax,%rbp
add    $0xffffffff,%ebx
movb   $0x0,(%rsp,%rbx,1)
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %rbp,(%r14,%r13,8)
add    $0x1,%r13
mov    %r15,%rdi
mov    $0x3e8,%esi
call   1040 <get_line@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     1170 <readlines+0x40>
mov    %r13d,%r12d
jmp    11bd <readlines+0x8d>
xor    %r12d,%r12d
mov    %r12d,%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 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 = %36, %2
%11 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%12 = call i32 @get_line(i8* %11, i32 1000)
store i32 %12, i32* %6
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %10
%15 = load i32, i32* %7
%16 = icmp sge i32 %15, 5000
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = load i32, i32* %6
%19 = call i8* @alloc(i32 %18)
store i8* %19, i8** %8
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %17, %14
store i32 -1, i32* %3
br label %39
22: ; preds = %17
%23 = load i32, i32* %6
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 %25
store i8 0, i8* %26
%27 = load i8*, i8** %8
%28 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%29 = call i32 @strcpy(i8* %27, i8* %28)
%30 = load i8*, i8** %8
%31 = load i8**, i8*** %4
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i8*, i8** %31, i64 %34
store i8* %30, i8** %35
br label %36
36: ; preds = %22
br label %10
37: ; preds = %10
%38 = load i32, i32* %7
store i32 %38, i32* %3
br label %39
39: ; preds = %37, %21
%40 = load i32, i32* %3
ret i32 %40
}

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