#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 getline1(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int readlines(char *lineptr[], int maxlines, char *buf) {
  int len, nlines;
  char *p, line[1000], *bufstop;
  p = buf;
  bufstop = buf + 5000;
  nlines = 0;
  while ((len = getline1(line, 1000)) > 0)
    if (nlines >= maxlines || p + len > bufstop)
      return -1;
    else {
      line[len - 1] = '\0';
      strcpy(p, line);
      lineptr[nlines++] = p;
      p += len;
    }
  return nlines;
}


x86 assembly (clang, -O0):
<readlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1388,%rax
mov    %rax,-0x428(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x420(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <getline1@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    11f3 <readlines+0xd3>
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1197 <readlines+0x77>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
cmp    -0x428(%rbp),%rax
jbe    11a3 <readlines+0x83>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <readlines+0xd9>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x30(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    1156 <readlines+0x36>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%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    %rdx,%r14
mov    %esi,%r13d
mov    %rdi,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
call   1040 <getline1@plt>
test   %eax,%eax
jle    11c1 <readlines+0xa1>
xor    %ebp,%ebp
test   %r13d,%r13d
cmovle %ebp,%r13d
lea    0x10(%rsp),%r12
mov    %r14,%rbx
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0xffffffff,%ecx
cmp    %rbp,%r13
je     11c3 <readlines+0xa3>
mov    %eax,%edx
add    %rdx,%r15
cmp    $0x1388,%r15
ja     11c3 <readlines+0xa3>
add    $0xffffffff,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    0x8(%rsp),%rax
mov    %rbx,(%rax,%rbp,8)
add    $0x1,%rbp
lea    (%r14,%r15,1),%rbx
mov    %r12,%rdi
mov    $0x3e8,%esi
call   1040 <getline1@plt>
test   %eax,%eax
jg     1170 <readlines+0x50>
mov    %ebp,%ecx
jmp    11c3 <readlines+0xa3>
xor    %ecx,%ecx
mov    %ecx,%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, i8* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [1000 x i8]
%12 = alloca i8*
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%13 = load i8*, i8** %7
store i8* %13, i8** %10
%14 = load i8*, i8** %7
%15 = getelementptr inbounds i8, i8* %14, i64 5000
store i8* %15, i8** %12
store i32 0, i32* %9
br label %16
16: ; preds = %50, %3
%17 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%18 = call i32 @getline1(i8* %17, i32 1000)
store i32 %18, i32* %8
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %51
20: ; preds = %16
%21 = load i32, i32* %9
%22 = load i32, i32* %6
%23 = icmp sge i32 %21, %22
br i1 %23, label %31, label %24
24: ; preds = %20
%25 = load i8*, i8** %10
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8*, i8** %12
%30 = icmp ugt i8* %28, %29
br i1 %30, label %31, label %32
31: ; preds = %24, %20
store i32 -1, i32* %4
br label %53
32: ; preds = %24
%33 = load i32, i32* %8
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 %35
store i8 0, i8* %36
%37 = load i8*, i8** %10
%38 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%39 = call i32 @strcpy(i8* %37, i8* %38)
%40 = load i8*, i8** %10
%41 = load i8**, i8*** %5
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
%44 = sext i32 %42 to i64
%45 = getelementptr inbounds i8*, i8** %41, i64 %44
store i8* %40, i8** %45
%46 = load i32, i32* %8
%47 = load i8*, i8** %10
%48 = sext i32 %46 to i64
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8* %49, i8** %10
br label %50
50: ; preds = %32
br label %16
51: ; preds = %16
%52 = load i32, i32* %9
store i32 %52, i32* %4
br label %53
53: ; preds = %51, %31
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
define i32 @readlines(i8** nocapture writeonly %0, i32 %1, i8* %2) {
%4 = alloca [1000 x i8]
%5 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%6 = call i32 @getline1(i8* %5, i32 1000)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %33
8: ; preds = %3
%9 = call i32 @llvm.smax.i32(i32 %1, i32 0)
%10 = zext i32 %9 to i64
br label %11
11: ; preds = %8, %21
%12 = phi i64 [ 0, %8 ], [ %26, %21 ]
%13 = phi i32 [ %6, %8 ], [ %29, %21 ]
%14 = phi i8* [ %2, %8 ], [ %28, %21 ]
%15 = phi i64 [ 0, %8 ], [ %19, %21 ]
%16 = icmp eq i64 %12, %10
br i1 %16, label %33, label %17
17: ; preds = %11
%18 = zext i32 %13 to i64
%19 = add nuw nsw i64 %15, %18
%20 = icmp ugt i64 %19, 5000
br i1 %20, label %33, label %21
21: ; preds = %17
%22 = add nsw i32 %13, -1
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 %23
store i8 0, i8* %24
%25 = call i32 @strcpy(i8* %14, i8* %5)
%26 = add nuw nsw i64 %12, 1
%27 = getelementptr inbounds i8*, i8** %0, i64 %12
store i8* %14, i8** %27
%28 = getelementptr inbounds i8, i8* %2, i64 %19
%29 = call i32 @getline1(i8* %5, i32 1000)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %11, label %31
31: ; preds = %21
%32 = trunc i64 %26 to i32
br label %33
33: ; preds = %11, %17, %31, %3
%34 = phi i32 [ 0, %3 ], [ %32, %31 ], [ -1, %17 ], [ -1, %11 ]
ret i32 %34
}