#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 EOF;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int getchar();
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
int utils_get_line(const char *prmpt, char *buff, size_t sz) {
  int ch, extra;
  if (prmpt != NULL) {
    printf("%s", prmpt);
    fflush(stdout);
  }
  if (fgets(buff, (int)sz, stdin) == NULL)
    return 1;
  if (buff[strlen(buff) - 1] != '\n') {
    extra = 0;
    while (((ch = getchar()) != '\n') && (ch != EOF))
      extra = 1;
    return (extra == 1) ? 2 : 0;
  }
  buff[strlen(buff) - 1] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<utils_get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118d <utils_get_line+0x3d>
mov    -0x10(%rbp),%rsi
lea    0xe86(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%esi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    11bb <utils_get_line+0x6b>
movl   $0x1,-0x4(%rbp)
jmp    1274 <utils_get_line+0x124>
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
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     124c <utils_get_line+0xfc>
movl   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x31(%rbp)
je     1218 <utils_get_line+0xc8>
mov    -0x24(%rbp),%eax
mov    0x2dd0(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1228 <utils_get_line+0xd8>
jmp    1234 <utils_get_line+0xe4>
movl   $0x1,-0x28(%rbp)
jmp    11ec <utils_get_line+0x9c>
mov    -0x28(%rbp),%edx
xor    %eax,%eax
mov    $0x2,%ecx
cmp    $0x1,%edx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1274 <utils_get_line+0x124>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utils_get_line>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
test   %rdi,%rdi
je     117c <utils_get_line+0x2c>
mov    %rdi,%rsi
lea    0xe97(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     11b9 <utils_get_line+0x69>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11c0 <utils_get_line+0x70>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %ebx,%ebx
jmp    11f1 <utils_get_line+0xa1>
mov    $0x1,%ebx
jmp    11f1 <utils_get_line+0xa1>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <getchar@plt>
cmp    $0xa,%eax
je     11f1 <utils_get_line+0xa1>
mov    0x2e0b(%rip),%r14
cmp    (%r14),%eax
je     11f1 <utils_get_line+0xa1>
mov    $0x2,%ebx
nop
xor    %eax,%eax
call   1060 <getchar@plt>
cmp    $0xa,%eax
je     11f1 <utils_get_line+0xa1>
cmp    (%r14),%eax
jne    11e0 <utils_get_line+0x90>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@stdout = global i32 0
@stdin = global i32 0
@EOF = global i32 0
define i32 @utils_get_line(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %17
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @stdout
%16 = call i32 @fflush(i32 %15)
br label %17
17: ; preds = %12, %3
%18 = load i8*, i8** %6
%19 = load i64, i64* %7
%20 = trunc i64 %19 to i32
%21 = load i32, i32* @stdin
%22 = call i32* @fgets(i8* %18, i32 %20, i32 %21)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 1, i32* %4
br label %58
25: ; preds = %17
%26 = load i8*, i8** %6
%27 = load i8*, i8** %6
%28 = call i32 @strlen(i8* %27)
%29 = sub nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %26, i64 %30
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 10
br i1 %34, label %35, label %51
35: ; preds = %25
store i32 0, i32* %9
br label %36
36: ; preds = %45, %35
%37 = call i32 (...) @getchar()
store i32 %37, i32* %8
%38 = icmp ne i32 %37, 10
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = load i32, i32* %8
%41 = load i32, i32* @EOF
%42 = icmp ne i32 %40, %41
br label %43
43: ; preds = %39, %36
%44 = phi i1 [ false, %36 ], [ %42, %39 ]
br i1 %44, label %45, label %46
45: ; preds = %43
store i32 1, i32* %9
br label %36
46: ; preds = %43
%47 = load i32, i32* %9
%48 = icmp eq i32 %47, 1
%49 = zext i1 %48 to i64
%50 = select i1 %48, i32 2, i32 0
store i32 %50, i32* %4
br label %58
51: ; preds = %25
%52 = load i8*, i8** %6
%53 = load i8*, i8** %6
%54 = call i32 @strlen(i8* %53)
%55 = sub nsw i32 %54, 1
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %52, i64 %56
store i8 0, i8* %57
store i32 0, i32* %4
br label %58
58: ; preds = %51, %46, %24
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@stdout = global i32 0
@stdin = global i32 0
@EOF = global i32 0
define i32 @utils_get_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 = trunc i64 %2 to i32
%11 = load i32, i32* @stdin
%12 = tail call i32* @fgets(i8* %1, i32 %10, i32 %11)
%13 = icmp eq i32* %12, null
br i1 %13, label %38, label %14
14: ; preds = %9
%15 = tail call i32 @strlen(i8* %1)
%16 = add nsw i32 %15, -1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %1, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 10
br i1 %20, label %33, label %21
21: ; preds = %14
%22 = tail call i32 (...) @getchar()
%23 = icmp ne i32 %22, 10
%24 = load i32, i32* @EOF
%25 = icmp ne i32 %22, %24
%26 = select i1 %23, i1 %25, i1 false
br i1 %26, label %27, label %38
27: ; preds = %21, %27
%28 = tail call i32 (...) @getchar()
%29 = icmp ne i32 %28, 10
%30 = load i32, i32* @EOF
%31 = icmp ne i32 %28, %30
%32 = select i1 %29, i1 %31, i1 false
br i1 %32, label %27, label %38
33: ; preds = %14
%34 = tail call i32 @strlen(i8* %1)
%35 = add nsw i32 %34, -1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %1, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %27, %21, %9, %33
%39 = phi i32 [ 0, %33 ], [ 1, %9 ], [ 0, %21 ], [ 2, %27 ]
ret i32 %39
}