#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
char *malloc(scalar_t__);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int getlines(char *lines[], int nline, FILE *stream) {
  int i = 0;
  char s[1000];
  char *p;
  while (fgets(s, 1000, stream) && i < nline) {
    p = malloc(strlen(s) + 1);
    strcpy(p, s);
    lines[i++] = p;
  }
  return i;
}


x86 assembly (clang, -O0):
<getlines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x3e8,%esi
call   1050 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x419(%rbp)
je     1196 <getlines+0x56>
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
setl   %al
mov    %al,-0x419(%rbp)
mov    -0x419(%rbp),%al
test   $0x1,%al
jne    11a9 <getlines+0x69>
jmp    11fd <getlines+0xbd>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x418(%rbp),%rdx
mov    -0x8(%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    115d <getlines+0x1d>
mov    -0x1c(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getlines>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3e8,%rsp
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x3e8,%esi
call   1050 <fgets@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11c1 <getlines+0x81>
test   %ebp,%ebp
jle    11c1 <getlines+0x81>
mov    %ebp,%r13d
xor    %ebx,%ebx
mov    %rsp,%r12
nopl   0x0(%rax)
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %rbp,(%r14,%rbx,8)
add    $0x1,%rbx
mov    %r12,%rdi
mov    $0x3e8,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11c1 <getlines+0x81>
cmp    %r13,%rbx
jb     1180 <getlines+0x40>
mov    %ebx,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @getlines(i8** %0, i32 %1, i32* %2) {
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [1000 x i8]
%9 = alloca i8*
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
store i32 0, i32* %7
br label %10
10: ; preds = %21, %3
%11 = getelementptr inbounds [1000 x i8], [1000 x i8]* %8, i64 0, i64 0
%12 = load i32*, i32** %6
%13 = call i64 @fgets(i8* %11, i32 1000, i32* %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i32, i32* %7
%17 = load i32, i32* %5
%18 = icmp slt i32 %16, %17
br label %19
19: ; preds = %15, %10
%20 = phi i1 [ false, %10 ], [ %18, %15 ]
br i1 %20, label %21, label %35
21: ; preds = %19
%22 = getelementptr inbounds [1000 x i8], [1000 x i8]* %8, i64 0, i64 0
%23 = call i64 @strlen(i8* %22)
%24 = add nsw i64 %23, 1
%25 = call i8* @malloc(i64 %24)
store i8* %25, i8** %9
%26 = load i8*, i8** %9
%27 = getelementptr inbounds [1000 x i8], [1000 x i8]* %8, i64 0, i64 0
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i8*, i8** %9
%30 = load i8**, i8*** %4
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds i8*, i8** %30, i64 %33
store i8* %29, i8** %34
br label %10
35: ; preds = %19
%36 = load i32, i32* %7
ret i32 %36
}

LLVM IR (-O2):
define i32 @getlines(i8** nocapture writeonly %0, i32 %1, i32* nocapture %2) {
%4 = alloca [1000 x i8]
%5 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%6 = call i64 @fgets(i8* %5, i32 1000, i32* %2)
%7 = icmp ne i64 %6, 0
%8 = icmp sgt i32 %1, 0
%9 = and i1 %7, %8
br i1 %9, label %10, label %26
10: ; preds = %3
%11 = zext i32 %1 to i64
br label %12
12: ; preds = %10, %12
%13 = phi i64 [ 0, %10 ], [ %18, %12 ]
%14 = call i64 @strlen(i8* %5)
%15 = add nsw i64 %14, 1
%16 = call i8* @malloc(i64 %15)
%17 = call i32 @strcpy(i8* %16, i8* %5)
%18 = add nuw nsw i64 %13, 1
%19 = getelementptr inbounds i8*, i8** %0, i64 %13
store i8* %16, i8** %19
%20 = call i64 @fgets(i8* %5, i32 1000, i32* %2)
%21 = icmp ne i64 %20, 0
%22 = icmp ult i64 %18, %11
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %12, label %24
24: ; preds = %12
%25 = trunc i64 %18 to i32
br label %26
26: ; preds = %24, %3
%27 = phi i32 [ 0, %3 ], [ %25, %24 ]
ret i32 %27
}