#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 */
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
unsigned long copy_lines(FILE *in, FILE *out, unsigned long n) {
  char *line = NULL;
  size_t linelen = 0;
  unsigned long count = 0;
  while (n--) {
    if (getline(&line, &linelen, in) == -1)
      break;
    count++;
    fputs(line, out);
  }
  if (line)
    free(line);
  return count;
}


x86 assembly (clang, -O0):
<copy_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
cmp    $0x0,%rax
je     11b2 <copy_lines+0x82>
mov    -0x8(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <getline@plt>
cmp    $0xffffffff,%eax
jne    1194 <copy_lines+0x64>
jmp    11b2 <copy_lines+0x82>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <fputs@plt>
jmp    115c <copy_lines+0x2c>
cmpq   $0x0,-0x20(%rbp)
je     11c6 <copy_lines+0x96>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_lines>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
test   %rdx,%rdx
je     11ad <copy_lines+0x7d>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
xor    %ebx,%ebx
lea    0x8(%rsp),%r13
lea    0x10(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1050 <getline@plt>
cmp    $0xffffffff,%eax
je     119c <copy_lines+0x6c>
add    $0x1,%rbx
mov    0x8(%rsp),%rdi
mov    %r15,%rsi
call   1040 <fputs@plt>
cmp    %rbx,%r14
jne    1170 <copy_lines+0x40>
mov    %r14,%rbx
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     11af <copy_lines+0x7f>
call   1030 <free@plt>
jmp    11af <copy_lines+0x7f>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @copy_lines(i32* %0, i32* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i32* %0, i32** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
store i8* null, i8** %7
store i64 0, i64* %8
store i64 0, i64* %9
br label %10
10: ; preds = %19, %3
%11 = load i64, i64* %6
%12 = add i64 %11, -1
store i64 %12, i64* %6
%13 = icmp ne i64 %11, 0
br i1 %13, label %14, label %25
14: ; preds = %10
%15 = load i32*, i32** %4
%16 = call i32 @getline(i8** %7, i64* %8, i32* %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %14
br label %25
19: ; preds = %14
%20 = load i64, i64* %9
%21 = add i64 %20, 1
store i64 %21, i64* %9
%22 = load i8*, i8** %7
%23 = load i32*, i32** %5
%24 = call i32 @fputs(i8* %22, i32* %23)
br label %10
25: ; preds = %18, %10
%26 = load i8*, i8** %7
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i8*, i8** %7
%30 = call i32 @free(i8* %29)
br label %31
31: ; preds = %28, %25
%32 = load i64, i64* %9
ret i64 %32
}

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