#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 */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
int read_console_line(const char *prompt, char *buffer, size_t size) {
  if (prompt != NULL) {
    printf("%s", prompt);
    fflush(stdout);
  }
  if (fgets(buffer, size, stdin) == NULL)
    return 0;
  if (buffer[strlen(buffer) - 1] == '\n')
    buffer[strlen(buffer) - 1] = '\0';
  return 1;
}


x86 assembly (clang, -O0):
<read_console_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     117d <read_console_line+0x3d>
mov    -0x10(%rbp),%rsi
lea    0xe96(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    11a9 <read_console_line+0x69>
movl   $0x0,-0x4(%rbp)
jmp    11fb <read_console_line+0xbb>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11f4 <read_console_line+0xb4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_console_line>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
test   %rdi,%rdi
je     116c <read_console_line+0x2c>
mov    %rdi,%rsi
lea    0xea7(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <fgets@plt>
test   %rax,%rax
je     11ac <read_console_line+0x6c>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
mov    $0x1,%ebp
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11ae <read_console_line+0x6e>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
jmp    11ae <read_console_line+0x6e>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@stdout = global i32 0
@stdin = global i32 0
define i32 @read_console_line(i8* %0, i8* %1, i64 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %9, label %5
5: ; preds = %3
%6 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = load i32, i32* @stdout
%8 = tail call i32 @fflush(i32 %7)
br label %9
9: ; preds = %5, %3
%10 = load i32, i32* @stdin
%11 = tail call i32* @fgets(i8* %1, i64 %2, i32 %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %25, label %13
13: ; preds = %9
%14 = tail call i32 @strlen(i8* %1)
%15 = add nsw i32 %14, -1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 10
br i1 %19, label %20, label %25
20: ; preds = %13
%21 = tail call i32 @strlen(i8* %1)
%22 = add nsw i32 %21, -1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %1, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %13, %20, %9
%26 = phi i32 [ 0, %9 ], [ 1, %20 ], [ 1, %13 ]
ret i32 %26
}