#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 read_lines(char *line_ptr[], int max_lines) {
  char *alloc(int n);
  void afree(char *p);
  int get_line(char *s, int n);
  void str_cpy(char *s, char *t);
  int len;
  int n_lines;
  char *p;
  char line[1000];
  n_lines = 0;
  while ((len = get_line(line, 1000)) > 0) {
    if (n_lines >= max_lines || (p = alloc(len)) == NULL)
      return -1;
    else {
      line[len - 1] = '\0';
      str_cpy(p, line);
      line_ptr[n_lines++] = p;
    }
  }
  return n_lines;
}


x86 assembly (clang, -O0):
<read_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x3e8,%esi
call   1030 <get_line@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    11d3 <read_lines+0xa3>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1188 <read_lines+0x58>
mov    -0x18(%rbp),%edi
call   1040 <alloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1194 <read_lines+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d9 <read_lines+0xa9>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x28(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1050 <str_cpy@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    1149 <read_lines+0x19>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_lines>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f8,%rsp
mov    %esi,%r12d
mov    %rdi,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
call   1030 <get_line@plt>
test   %eax,%eax
jle    11cc <read_lines+0x9c>
mov    %eax,%ebx
xor    %r14d,%r14d
test   %r12d,%r12d
cmovle %r14d,%r12d
lea    0x10(%rsp),%r15
mov    $0xffffffff,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    %r14,%r12
je     11cf <read_lines+0x9f>
mov    %ebx,%edi
call   1040 <alloc@plt>
test   %rax,%rax
je     11cf <read_lines+0x9f>
mov    %rax,%rbp
add    $0xffffffff,%ebx
movb   $0x0,0x10(%rsp,%rbx,1)
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <str_cpy@plt>
mov    0x8(%rsp),%rax
mov    %rbp,(%rax,%r14,8)
add    $0x1,%r14
mov    %r15,%rdi
mov    $0x3e8,%esi
call   1030 <get_line@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     1180 <read_lines+0x50>
mov    %r14d,%r13d
jmp    11cf <read_lines+0x9f>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x3f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_lines(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 i32 @get_line(i8* %11, i32 1000)
store i32 %12, i32* %6
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %10
%15 = load i32, i32* %7
%16 = load i32, i32* %5
%17 = icmp sge 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 %39
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 = 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 = %23
br label %10
37: ; preds = %10
%38 = load i32, i32* %7
store i32 %38, i32* %3
br label %39
39: ; preds = %37, %22
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
define i32 @read_lines(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 @get_line(i8* %4, i32 1000)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %27
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, %17
%11 = phi i64 [ 0, %7 ], [ %21, %17 ]
%12 = phi i32 [ %5, %7 ], [ %23, %17 ]
%13 = icmp eq i64 %11, %9
br i1 %13, label %27, label %14
14: ; preds = %10
%15 = call i8* @alloc(i32 %12)
%16 = icmp eq i8* %15, null
br i1 %16, label %27, label %17
17: ; preds = %14
%18 = add nsw i32 %12, -1
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 %19
store i8 0, i8* %20
%21 = add nuw nsw i64 %11, 1
%22 = getelementptr inbounds i8*, i8** %0, i64 %11
store i8* %15, i8** %22
%23 = call i32 @get_line(i8* %4, i32 1000)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %10, label %25
25: ; preds = %17
%26 = trunc i64 %21 to i32
br label %27
27: ; preds = %10, %14, %25, %2
%28 = phi i32 [ 0, %2 ], [ %26, %25 ], [ -1, %14 ], [ -1, %10 ]
ret i32 %28
}