#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);
scalar_t__ gets(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int readlines(char *lineptr[], int maxlines) {
  int len, nlines = 0;
  char *p, line[1000];
  while (gets(line)) {
    len = strlen(line);
    if (nlines >= maxlines || !(p = alloc(len + 1))) {
      return -1;
    } else {
      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
call   1050 <gets@plt>
cmp    $0x0,%rax
je     11de <readlines+0x9e>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a3 <readlines+0x63>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1060 <alloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11af <readlines+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <readlines+0xa4>
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    1159 <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    %esi,%r15d
mov    %rdi,%r14
mov    %rsp,%rdi
call   1050 <gets@plt>
test   %rax,%rax
je     11c4 <readlines+0x84>
xor    %ebp,%ebp
test   %r15d,%r15d
cmovle %ebp,%r15d
mov    %rsp,%r12
mov    $0xffffffff,%r13d
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1040 <strlen@plt>
cmp    %rbp,%r15
je     11c7 <readlines+0x87>
add    $0x1,%eax
mov    %eax,%edi
call   1060 <alloc@plt>
test   %rax,%rax
je     11c7 <readlines+0x87>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %rbx,(%r14,%rbp,8)
add    $0x1,%rbp
mov    %r12,%rdi
call   1050 <gets@plt>
test   %rax,%rax
jne    1180 <readlines+0x40>
mov    %ebp,%r13d
jmp    11c7 <readlines+0x87>
xor    %r13d,%r13d
mov    %r13d,%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 i64 @gets(i8* %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %10
%15 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %6
%17 = load i32, i32* %7
%18 = load i32, i32* %5
%19 = icmp sge i32 %17, %18
br i1 %19, label %25, label %20
20: ; preds = %14
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
%23 = call i8* @alloc(i32 %22)
store i8* %23, i8** %8
%24 = icmp ne i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %20, %14
store i32 -1, i32* %3
br label %39
26: ; preds = %20
%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 = %26
br label %10
37: ; preds = %10
%38 = load i32, i32* %7
store i32 %38, i32* %3
br label %39
39: ; preds = %37, %25
%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 i64 @gets(i8* %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %26, label %7
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 ], [ %20, %18 ]
%12 = call i32 @strlen(i8* %4)
%13 = icmp eq i64 %11, %9
br i1 %13, label %26, label %14
14: ; preds = %10
%15 = add nsw i32 %12, 1
%16 = call i8* @alloc(i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %26, label %18
18: ; preds = %14
%19 = call i32 @strcpy(i8* %16, i8* %4)
%20 = add nuw nsw i64 %11, 1
%21 = getelementptr inbounds i8*, i8** %0, i64 %11
store i8* %16, i8** %21
%22 = call i64 @gets(i8* %4)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %10
24: ; preds = %18
%25 = trunc i64 %20 to i32
br label %26
26: ; preds = %10, %14, %24, %2
%27 = phi i32 [ 0, %2 ], [ %25, %24 ], [ -1, %14 ], [ -1, %10 ]
ret i32 %27
}