#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 */

/* Variables and functions */
char *strchr(char *, char);
size_t strlen(char *);
char *read_line(char **buffer) {
  char *result = *buffer;
  char *eol = strchr(*buffer, '\n');
  if (eol) {
    *eol = '\0';
    *buffer = eol + 1;
  } else {
    size_t len = strlen(*buffer);
    *buffer = *buffer + len;
  }
  return result;
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1172 <read_line+0x52>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rcx
add    $0x1,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
jmp    1194 <read_line+0x74>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    (%rdi),%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1145 <read_line+0x25>
movb   $0x0,(%rax)
add    $0x1,%rax
jmp    1150 <read_line+0x30>
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    %rbx,%rax
mov    %rax,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @read_line(i8** %0) {
%2 = alloca i8**
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8** %0, i8*** %2
%6 = load i8**, i8*** %2
%7 = load i8*, i8** %6
store i8* %7, i8** %3
%8 = load i8**, i8*** %2
%9 = load i8*, i8** %8
%10 = call i8* @strchr(i8* %9, i8 signext 10)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i8*, i8** %4
store i8 0, i8* %14
%15 = load i8*, i8** %4
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = load i8**, i8*** %2
store i8* %16, i8** %17
br label %27
18: ; preds = %1
%19 = load i8**, i8*** %2
%20 = load i8*, i8** %19
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %5
%22 = load i8**, i8*** %2
%23 = load i8*, i8** %22
%24 = load i64, i64* %5
%25 = getelementptr inbounds i8, i8* %23, i64 %24
%26 = load i8**, i8*** %2
store i8* %25, i8** %26
br label %27
27: ; preds = %18, %13
%28 = load i8*, i8** %3
ret i8* %28
}

LLVM IR (-O2):
define i8* @read_line(i8** nocapture %0) {
%2 = load i8*, i8** %0
%3 = tail call i8* @strchr(i8* dereferenceable(1) %2, i8 signext 10)
%4 = icmp eq i8* %3, null
br i1 %4, label %7, label %5
5: ; preds = %1
store i8 0, i8* %3
%6 = getelementptr inbounds i8, i8* %3, i64 1
br label %10
7: ; preds = %1
%8 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%9 = getelementptr inbounds i8, i8* %2, i64 %8
br label %10
10: ; preds = %7, %5
%11 = phi i8* [ %9, %7 ], [ %6, %5 ]
store i8* %11, i8** %0
ret i8* %2
}