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


x86 assembly (clang, -O0):
<readlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x420(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <getline@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jle    11e0 <readlines+0xc0>
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    117e <readlines+0x5e>
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
cmp    $0x186a0,%eax
jle    118a <readlines+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <readlines+0xc6>
mov    -0x28(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x20(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
lea    -0x420(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdx
movslq -0x24(%rbp),%rax
add    %rax,%rdx
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x2c(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1144 <readlines+0x24>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%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,0x8(%rsp)
mov    %esi,%r13d
mov    %rdi,%r15
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
call   1040 <getline@plt>
test   %eax,%eax
jle    11b2 <readlines+0x92>
xor    %ebp,%ebp
test   %r13d,%r13d
cmovle %ebp,%r13d
lea    0x10(%rsp),%r12
xor    %edx,%edx
nop
mov    $0xffffffff,%ecx
cmp    %rbp,%r13
je     11b4 <readlines+0x94>
lea    (%rax,%rdx,1),%r14d
cmp    $0x186a0,%r14d
jg     11b4 <readlines+0x94>
add    $0xffffffff,%eax
movb   $0x0,0x10(%rsp,%rax,1)
movslq %edx,%rbx
add    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %rbx,(%r15,%rbp,8)
add    $0x1,%rbp
mov    %r12,%rdi
mov    $0x3e8,%esi
call   1040 <getline@plt>
mov    %r14d,%edx
test   %eax,%eax
jg     1160 <readlines+0x40>
mov    %ebp,%ecx
jmp    11b4 <readlines+0x94>
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 i32
%11 = alloca [1000 x i8]
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 0, i32* %8
store i32 0, i32* %10
br label %12
12: ; preds = %49, %3
%13 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%14 = call i32 @getline(i8* %13, i32 1000)
store i32 %14, i32* %9
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %50
16: ; preds = %12
%17 = load i32, i32* %10
%18 = load i32, i32* %6
%19 = icmp sge i32 %17, %18
br i1 %19, label %25, label %20
20: ; preds = %16
%21 = load i32, i32* %8
%22 = load i32, i32* %9
%23 = add nsw i32 %21, %22
%24 = icmp sgt i32 %23, 100000
br i1 %24, label %25, label %26
25: ; preds = %20, %16
store i32 -1, i32* %4
br label %52
26: ; preds = %20
%27 = load i32, i32* %9
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 %29
store i8 0, i8* %30
%31 = load i8*, i8** %7
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = getelementptr inbounds [1000 x i8], [1000 x i8]* %11, i64 0, i64 0
%36 = call i32 @strcpy(i8* %34, i8* %35)
%37 = load i8*, i8** %7
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i8**, i8*** %5
%42 = load i32, i32* %10
%43 = add nsw i32 %42, 1
store i32 %43, i32* %10
%44 = sext i32 %42 to i64
%45 = getelementptr inbounds i8*, i8** %41, i64 %44
store i8* %40, i8** %45
%46 = load i32, i32* %9
%47 = load i32, i32* %8
%48 = add nsw i32 %47, %46
store i32 %48, i32* %8
br label %49
49: ; preds = %26
br label %12
50: ; preds = %12
%51 = load i32, i32* %10
store i32 %51, i32* %4
br label %52
52: ; preds = %50, %25
%53 = load i32, i32* %4
ret i32 %53
}

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