#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*/ *fgets(char *, size_t, int /*<<< orphan*/);
int getchar();
int /*<<< orphan*/ stdin;
int strlen(char *);
void cli_get_line(char *buff, size_t sz) {
  int ch, extra;
  if (fgets(buff, sz, stdin) == NULL) {
    return;
  }
  if (buff[strlen(buff) - 1] != '\n') {
    extra = 0;
    while (((ch = getchar()) != '\n') && (ch != EOF))
      extra = 1;
    return;
  }
  buff[strlen(buff) - 1] = '\0';
}


x86 assembly (clang, -O0):
<cli_get_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e99(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    1165 <cli_get_line+0x35>
jmp    1204 <cli_get_line+0xd4>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     11e3 <cli_get_line+0xb3>
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x21(%rbp)
je     11c2 <cli_get_line+0x92>
mov    -0x14(%rbp),%eax
mov    0x2e26(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11d2 <cli_get_line+0xa2>
jmp    11de <cli_get_line+0xae>
movl   $0x1,-0x18(%rbp)
jmp    1196 <cli_get_line+0x66>
jmp    1204 <cli_get_line+0xd4>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cli_get_line>:
push   %rbx
mov    %rdi,%rbx
mov    0x2ead(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
test   %rax,%rax
je     1180 <cli_get_line+0x50>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1169 <cli_get_line+0x39>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
jmp    1180 <cli_get_line+0x50>
mov    0x2e70(%rip),%rbx
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%eax
je     1180 <cli_get_line+0x50>
cmp    (%rbx),%eax
jne    1170 <cli_get_line+0x40>
pop    %rbx
ret

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

LLVM IR (-O2):
@stdin = global i32 0
@EOF = global i32 0
define void @cli_get_line(i8* %0, i64 %1) {
%3 = load i32, i32* @stdin
%4 = tail call i32* @fgets(i8* %0, i64 %1, i32 %3)
%5 = icmp eq i32* %4, null
br i1 %5, label %24, label %6
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %0)
%8 = add nsw i32 %7, -1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 10
br i1 %12, label %19, label %13
13: ; preds = %6, %13
%14 = tail call i32 (...) @getchar()
%15 = icmp ne i32 %14, 10
%16 = load i32, i32* @EOF
%17 = icmp ne i32 %14, %16
%18 = select i1 %15, i1 %17, i1 false
br i1 %18, label %13, label %24
19: ; preds = %6
%20 = tail call i32 @strlen(i8* %0)
%21 = add nsw i32 %20, -1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %0, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %13, %2, %19
ret void
}