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

/* Variables and functions */
size_t BUFSIZ;
int /*<<< orphan*/ fgets(char *, size_t, int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ strlen(char *);
ssize_t getline(char **line, size_t *linelen, FILE *fp) {
  if (*linelen == 0) {
    *linelen = BUFSIZ;
    *line = malloc(*linelen);
  }
  memset(*line, 0, *linelen);
  fgets(*line, *linelen, fp);
  return (strlen(*line));
}


x86 assembly (clang, -O0):
<getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1189 <getline+0x49>
mov    0x2e77(%rip),%rax
mov    (%rax),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <fgets@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getline>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    (%rsi),%rbx
test   %rbx,%rbx
je     115f <getline+0x1f>
mov    (%r12),%rdi
jmp    117b <getline+0x3b>
mov    0x2e7a(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,(%r15)
mov    %rbx,%rdi
call   1060 <malloc@plt>
mov    %rax,%rdi
mov    %rax,(%r12)
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <memset@plt>
mov    (%r12),%rdi
mov    (%r15),%rsi
mov    %r14,%rdx
call   1050 <fgets@plt>
mov    (%r12),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <strlen@plt>

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

LLVM IR (-O2):
@BUFSIZ = global i64 0
define i32 @getline(i8** nocapture %0, i64* nocapture %1, i32* nocapture %2) {
%4 = load i64, i64* %1
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = load i8*, i8** %0
br label %11
8: ; preds = %3
%9 = load i64, i64* @BUFSIZ
store i64 %9, i64* %1
%10 = tail call i8* @malloc(i64 %9)
store i8* %10, i8** %0
br label %11
11: ; preds = %6, %8
%12 = phi i64 [ %4, %6 ], [ %9, %8 ]
%13 = phi i8* [ %7, %6 ], [ %10, %8 ]
%14 = tail call i32 @memset(i8* %13, i32 0, i64 %12)
%15 = load i8*, i8** %0
%16 = load i64, i64* %1
%17 = tail call i32 @fgets(i8* %15, i64 %16, i32* %2)
%18 = load i8*, i8** %0
%19 = tail call i32 @strlen(i8* %18)
ret i32 %19
}