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

/* Variables and functions */
char *fgetln(int /*<<< orphan*/ *, size_t *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
  char *ptr;
  ptr = fgetln(stream, n);
  if (ptr == NULL) {
    return -1;
  }
  if (*lineptr != NULL)
    free(*lineptr);
  size_t len = n[0] + 1;
  n[0] = len;
  *lineptr = malloc(len);
  memcpy(*lineptr, ptr, len - 1);
  (*lineptr)[len - 1] = '\0';
  return len;
}


x86 assembly (clang, -O0):
<getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fgetln@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    117c <getline+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <getline+0xb5>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1196 <getline+0x56>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getline>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rdx,%rdi
call   1060 <fgetln@plt>
test   %rax,%rax
je     119b <getline+0x5b>
mov    %rax,%r15
mov    (%r14),%rdi
test   %rdi,%rdi
je     116c <getline+0x2c>
call   1030 <free@plt>
mov    (%r12),%r13
lea    0x1(%r13),%rbx
mov    %rbx,(%r12)
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1040 <memcpy@plt>
mov    (%r14),%rax
movb   $0x0,(%rax,%r13,1)
jmp    11a0 <getline+0x60>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @getline(i8** nocapture %0, i64* %1, i32* %2) {
%4 = tail call i8* @fgetln(i32* %2, i64* %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %19, label %6
6: ; preds = %3
%7 = load i8*, i8** %0
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @free(i8* %7)
br label %11
11: ; preds = %9, %6
%12 = load i64, i64* %1
%13 = add i64 %12, 1
store i64 %13, i64* %1
%14 = tail call i8* @malloc(i64 %13)
store i8* %14, i8** %0
%15 = tail call i32 @memcpy(i8* %14, i8* %4, i64 %12)
%16 = load i8*, i8** %0
%17 = getelementptr inbounds i8, i8* %16, i64 %12
store i8 0, i8* %17
%18 = trunc i64 %13 to i32
br label %19
19: ; preds = %3, %11
%20 = phi i32 [ %18, %11 ], [ -1, %3 ]
ret i32 %20
}