#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 */
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
ssize_t x_getline(char **line, FILE *file) {
  size_t allocated = 0;
  ssize_t len = getline(line, &allocated, file);
  if (len == -1) {
    free(*line);
    return -1;
  } else if ((*line)[len - 1] == '\n') {
    (*line)[--len] = '\0';
  }
  return len;
}


x86 assembly (clang, -O0):
<x_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rsi
call   1040 <getline@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    116e <x_getline+0x4e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ad <x_getline+0x8d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11a2 <x_getline+0x82>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x24(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x24(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a7 <x_getline+0x87>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<x_getline>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rdx
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
call   1040 <getline@plt>
mov    (%rbx),%rdi
cmp    $0xffffffff,%eax
je     115c <x_getline+0x3c>
movslq %eax,%rcx
cmpb   $0xa,-0x1(%rdi,%rcx,1)
jne    1166 <x_getline+0x46>
add    $0xffffffffffffffff,%rcx
movb   $0x0,(%rdi,%rcx,1)
mov    %ecx,%eax
jmp    1166 <x_getline+0x46>
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @x_getline(i8** %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
store i8** %0, i8*** %4
store i32* %1, i32** %5
store i64 0, i64* %6
%8 = load i8**, i8*** %4
%9 = load i32*, i32** %5
%10 = call i32 @getline(i8** %8, i64* %6, i32* %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i8**, i8*** %4
%15 = load i8*, i8** %14
%16 = call i32 @free(i8* %15)
store i32 -1, i32* %3
br label %37
17: ; preds = %2
%18 = load i8**, i8*** %4
%19 = load i8*, i8** %18
%20 = load i32, i32* %7
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 10
br i1 %26, label %27, label %34
27: ; preds = %17
%28 = load i8**, i8*** %4
%29 = load i8*, i8** %28
%30 = load i32, i32* %7
%31 = add nsw i32 %30, -1
store i32 %31, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %27, %17
br label %35
35: ; preds = %34
%36 = load i32, i32* %7
store i32 %36, i32* %3
br label %37
37: ; preds = %35, %13
%38 = load i32, i32* %3
ret i32 %38
}

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