#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 getline_my(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int readlines(char *lineptr[], int maxlines) {
  int len = 0;
  int nlines = 0;
  char *p;
  char line[1000];
  while ((len = getline_my(line)) > 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,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <getline_my@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    11d5 <readlines+0xa5>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     118a <readlines+0x5a>
mov    -0x18(%rbp),%edi
call   1050 <alloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1196 <readlines+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <readlines+0xab>
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    1150 <readlines+0x20>
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,%r13d
mov    %rdi,%r14
mov    %rsp,%rdi
call   1040 <getline_my@plt>
test   %eax,%eax
jle    11b8 <readlines+0x88>
mov    %eax,%ebp
test   %r13d,%r13d
mov    $0xffffffff,%eax
cmovs  %eax,%r13d
add    $0x1,%r13d
xor    %r15d,%r15d
mov    %rsp,%r12
nopl   0x0(%rax,%rax,1)
cmp    %r15,%r13
je     11b1 <readlines+0x81>
mov    %ebp,%edi
call   1050 <alloc@plt>
test   %rax,%rax
je     11b1 <readlines+0x81>
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
call   1040 <getline_my@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1170 <readlines+0x40>
mov    %r15d,%eax
jmp    11ba <readlines+0x8a>
mov    $0xffffffff,%eax
jmp    11ba <readlines+0x8a>
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 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* %6
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 @getline_my(i8* %11)
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 sgt 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 @getline_my(i8* %4)
%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* @alloc(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_my(i8* %4)
%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
}