#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 */
char *fgetln(int /*<<< orphan*/ *, size_t *);
scalar_t__ malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char **copy_lines(FILE *snoc_dst, int nlines) {
  int i = 0;
  char *lnptr = NULL;
  char **lines = NULL;
  size_t len;
  rewind(snoc_dst);
  lines = (char **)malloc(sizeof(char *) * nlines);
  while ((lnptr = fgetln(snoc_dst, &len))) {
    lines[i] = (char *)malloc(sizeof(char) * len + 1);
    strncpy(lines[i], lnptr, len);
    i++;
  }
  rewind(snoc_dst);
  return lines;
}


x86 assembly (clang, -O0):
<copy_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <rewind@plt>
movslq -0xc(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <fgetln@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11e6 <copy_lines+0xa6>
mov    -0x28(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1182 <copy_lines+0x42>
mov    -0x8(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_lines>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%r12
call   1040 <rewind@plt>
shl    $0x3,%ebx
mov    %ebx,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
call   1060 <fgetln@plt>
test   %rax,%rax
je     11c6 <copy_lines+0x86>
mov    %rax,%r13
lea    0x8(%rsp),%r15
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,(%rbx)
mov    0x8(%rsp),%rdx
mov    %rax,%rdi
mov    %r13,%rsi
call   1030 <strncpy@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <fgetln@plt>
mov    %rax,%r13
add    $0x8,%rbx
test   %rax,%rax
jne    1190 <copy_lines+0x50>
mov    %r12,%rdi
call   1040 <rewind@plt>
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8** @copy_lines(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i64
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i8* null, i8** %6
store i8** null, i8*** %7
%9 = load i32*, i32** %3
%10 = call i32 @rewind(i32* %9)
%11 = load i32, i32* %4
%12 = sext i32 %11 to i64
%13 = mul i64 8, %12
%14 = trunc i64 %13 to i32
%15 = call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8**
store i8** %16, i8*** %7
br label %17
17: ; preds = %21, %2
%18 = load i32*, i32** %3
%19 = call i8* @fgetln(i32* %18, i64* %8)
store i8* %19, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %42
21: ; preds = %17
%22 = load i64, i64* %8
%23 = mul i64 1, %22
%24 = add i64 %23, 1
%25 = trunc i64 %24 to i32
%26 = call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
%28 = load i8**, i8*** %7
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8*, i8** %28, i64 %30
store i8* %27, i8** %31
%32 = load i8**, i8*** %7
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %32, i64 %34
%36 = load i8*, i8** %35
%37 = load i8*, i8** %6
%38 = load i64, i64* %8
%39 = call i32 @strncpy(i8* %36, i8* %37, i64 %38)
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
store i32 %41, i32* %5
br label %17
42: ; preds = %17
%43 = load i32*, i32** %3
%44 = call i32 @rewind(i32* %43)
%45 = load i8**, i8*** %7
ret i8** %45
}

LLVM IR (-O2):
define i8** @copy_lines(i32* %0, i32 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = tail call i32 @rewind(i32* %0)
%6 = shl i32 %1, 3
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8**
%9 = call i8* @fgetln(i32* %0, i64* %3)
%10 = icmp eq i8* %9, null
br i1 %10, label %25, label %11
11: ; preds = %2, %11
%12 = phi i64 [ %22, %11 ], [ 0, %2 ]
%13 = phi i8* [ %23, %11 ], [ %9, %2 ]
%14 = load i64, i64* %3
%15 = trunc i64 %14 to i32
%16 = add i32 %15, 1
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
%19 = getelementptr inbounds i8*, i8** %8, i64 %12
store i8* %18, i8** %19
%20 = load i64, i64* %3
%21 = call i32 @strncpy(i8* %18, i8* %13, i64 %20)
%22 = add nuw i64 %12, 1
%23 = call i8* @fgetln(i32* %0, i64* %3)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %11
25: ; preds = %11, %2
%26 = call i32 @rewind(i32* %0)
ret i8** %8
}