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


x86 assembly (clang, -O0):
<readlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x3e8,%esi
call   1040 <my_getline@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    120f <readlines+0xef>
mov    -0x28(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1189 <readlines+0x69>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jg     1189 <readlines+0x69>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cmp    $0x3e8,%eax
jle    1195 <readlines+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <readlines+0xf5>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    11be <readlines+0x9e>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x18(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%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
add    $0x1,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1140 <readlines+0x20>
mov    -0x28(%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    $0x3f8,%rsp
mov    %ecx,%r14d
mov    %edx,%r13d
mov    %rsi,%rbx
mov    %rdi,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
call   1040 <my_getline@plt>
test   %eax,%eax
jle    11cd <readlines+0xad>
xor    %ebp,%ebp
test   %r14d,%r14d
cmovle %ebp,%r14d
lea    0x10(%rsp),%r12
jmp    11a2 <readlines+0x82>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    0x8(%rsp),%rax
mov    %rbx,(%rax,%rbp,8)
add    $0x1,%rbp
mov    %r15d,%eax
add    %rax,%rbx
sub    %r15d,%r13d
mov    %r12,%rdi
mov    $0x3e8,%esi
call   1040 <my_getline@plt>
test   %eax,%eax
jle    11d1 <readlines+0xb1>
mov    $0xffffffff,%ecx
cmp    %rbp,%r14
je     11d3 <readlines+0xb3>
cmp    %eax,%r13d
jle    11d3 <readlines+0xb3>
cmp    $0x3e7,%eax
ja     11d3 <readlines+0xb3>
lea    0x1(%rax),%r15d
add    $0xffffffff,%eax
cmpb   $0xa,0x10(%rsp,%rax,1)
jne    1170 <readlines+0x50>
movb   $0x0,0x10(%rsp,%rax,1)
jmp    1170 <readlines+0x50>
xor    %ecx,%ecx
jmp    11d3 <readlines+0xb3>
mov    %ebp,%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, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [1000 x i8]
store i8** %0, i8*** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %11
br label %13
13: ; preds = %63, %4
%14 = getelementptr inbounds [1000 x i8], [1000 x i8]* %12, i64 0, i64 0
%15 = call i32 @my_getline(i8* %14, i32 1000)
store i32 %15, i32* %10
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %64
17: ; preds = %13
%18 = load i32, i32* %11
%19 = load i32, i32* %9
%20 = icmp sge i32 %18, %19
br i1 %20, label %30, label %21
21: ; preds = %17
%22 = load i32, i32* %10
%23 = add nsw i32 %22, 1
%24 = load i32, i32* %8
%25 = icmp sgt i32 %23, %24
br i1 %25, label %30, label %26
26: ; preds = %21
%27 = load i32, i32* %10
%28 = add nsw i32 %27, 1
%29 = icmp sgt i32 %28, 1000
br i1 %29, label %30, label %31
30: ; preds = %26, %21, %17
store i32 -1, i32* %5
br label %66
31: ; preds = %26
%32 = load i32, i32* %10
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1000 x i8], [1000 x i8]* %12, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 10
br i1 %38, label %39, label %44
39: ; preds = %31
%40 = load i32, i32* %10
%41 = sub nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [1000 x i8], [1000 x i8]* %12, i64 0, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %39, %31
%45 = load i8*, i8** %7
%46 = getelementptr inbounds [1000 x i8], [1000 x i8]* %12, i64 0, i64 0
%47 = call i32 @strcpy(i8* %45, i8* %46)
%48 = load i8*, i8** %7
%49 = load i8**, i8*** %6
%50 = load i32, i32* %11
%51 = add nsw i32 %50, 1
store i32 %51, i32* %11
%52 = sext i32 %50 to i64
%53 = getelementptr inbounds i8*, i8** %49, i64 %52
store i8* %48, i8** %53
%54 = load i32, i32* %10
%55 = add nsw i32 %54, 1
%56 = load i8*, i8** %7
%57 = sext i32 %55 to i64
%58 = getelementptr inbounds i8, i8* %56, i64 %57
store i8* %58, i8** %7
%59 = load i32, i32* %10
%60 = add nsw i32 %59, 1
%61 = load i32, i32* %8
%62 = sub nsw i32 %61, %60
store i32 %62, i32* %8
br label %63
63: ; preds = %44
br label %13
64: ; preds = %13
%65 = load i32, i32* %11
store i32 %65, i32* %5
br label %66
66: ; preds = %64, %30
%67 = load i32, i32* %5
ret i32 %67
}

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