#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*/ *);
int /*<<< orphan*/ printf(char *, char *);
int strlen(char *);
char *read_line(FILE *stream) {
  char *line = NULL;
  size_t length = 0;
  ssize_t chars_read = getline(&line, &length, stream);
  if (chars_read > 1) {
    if (line[strlen(line) - 1] == '\n') {
      line[strlen(line) - 1] = '\0';
    }
    printf("line: %s\n", line);
    return line;
  } else if (line[0] == '\n') {
    free(line);
    return NULL;
  }
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <getline@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jle    11e2 <read_line+0xa2>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11c5 <read_line+0x85>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rsi
lea    0xe30(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    120d <read_line+0xcd>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    1208 <read_line+0xc8>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    120d <read_line+0xcd>
jmp    120d <read_line+0xcd>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rdx
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
call   1060 <getline@plt>
mov    (%rsp),%rbx
cmp    $0x2,%eax
jl     11a9 <read_line+0x69>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1193 <read_line+0x53>
mov    (%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    (%rsp),%rsi
lea    0xe62(%rip),%rdi
call   1050 <printf@plt>
mov    (%rsp),%rax
jmp    11b8 <read_line+0x78>
cmpb   $0xa,(%rbx)
jne    11b8 <read_line+0x78>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"line: %s\0A\00"
define i8* @read_line(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i32* %0, i32** %3
store i8* null, i8** %4
store i64 0, i64* %5
%7 = load i32*, i32** %3
%8 = call i32 @getline(i8** %4, i64* %5, i32* %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %32
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = load i8*, i8** %4
%14 = call i32 @strlen(i8* %13)
%15 = sub nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %12, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 10
br i1 %20, label %21, label %28
21: ; preds = %11
%22 = load i8*, i8** %4
%23 = load i8*, i8** %4
%24 = call i32 @strlen(i8* %23)
%25 = sub nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %22, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %21, %11
%29 = load i8*, i8** %4
%30 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %4
store i8* %31, i8** %2
br label %42
32: ; preds = %1
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 10
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = load i8*, i8** %4
%40 = call i32 @free(i8* %39)
store i8* null, i8** %2
br label %42
41: ; preds = %32
br label %42
42: ; preds = %28, %38, %41
%43 = load i8*, i8** %2
ret i8* %43
}

LLVM IR (-O2):
@.str = [10 x i8] c"line: %s\0A\00"
define i8* @read_line(i32* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%5 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%6 = call i32 @getline(i8** %2, i64* %3, i32* %0)
%7 = icmp sgt i32 %6, 1
%8 = load i8*, i8** %2
br i1 %7, label %9, label %26
9: ; preds = %1
%10 = call i32 @strlen(i8* %8)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 10
br i1 %15, label %16, label %22
16: ; preds = %9
%17 = load i8*, i8** %2
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %18, -1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %16, %9
%23 = load i8*, i8** %2
%24 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %2
br label %31
26: ; preds = %1
%27 = load i8, i8* %8
%28 = icmp eq i8 %27, 10
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @free(i8* %8)
br label %31
31: ; preds = %26, %29, %22
%32 = phi i8* [ %25, %22 ], [ null, %29 ], [ undef, %26 ]
ret i8* %32
}