#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;
char *strchr(char *, char);
size_t get_user_input(char *buffer, size_t len, const char **err_msg) {
  char *newline;
  if (fgets(buffer, len, stdin) == NULL) {
    *err_msg = "Cannot read stdin or it is empty";
    return 0;
  }
  if ((newline = strchr(buffer, '\n')) == NULL) {
    int next_char = getchar();
    if (next_char == '\n' || next_char == EOF) {
      return len - 1;
    } else {
      *err_msg = "input too long";
      return 0;
    }
  }
  *newline = '\0';
  return newline - buffer;
}


x86 assembly (clang, -O0):
<get_user_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e95(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    117f <get_user_input+0x4f>
mov    -0x20(%rbp),%rax
lea    0xe91(%rip),%rcx
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1203 <get_user_input+0xd3>
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ed <get_user_input+0xbd>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xa,-0x2c(%rbp)
je     11c1 <get_user_input+0x91>
mov    -0x2c(%rbp),%eax
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%eax
jne    11d2 <get_user_input+0xa2>
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1203 <get_user_input+0xd3>
mov    -0x20(%rbp),%rax
lea    0xe44(%rip),%rcx
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1203 <get_user_input+0xd3>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_user_input>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2ea3(%rip),%rax
mov    (%rax),%edx
call   1040 <fgets@plt>
test   %rax,%rax
je     116e <get_user_input+0x3e>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1177 <get_user_input+0x47>
movb   $0x0,(%rax)
sub    %rbx,%rax
mov    %rax,%r15
jmp    11a1 <get_user_input+0x71>
lea    0xe8b(%rip),%rax
jmp    1195 <get_user_input+0x65>
xor    %eax,%eax
call   1050 <getchar@plt>
cmp    $0xa,%eax
je     119d <get_user_input+0x6d>
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%eax
je     119d <get_user_input+0x6d>
lea    0xe8c(%rip),%rax
mov    %rax,(%r14)
xor    %r15d,%r15d
jmp    11a1 <get_user_input+0x71>
add    $0xffffffffffffffff,%r15
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [33 x i8] c"Cannot read stdin or it is empty\00"
@EOF = global i32 0
@.str.1 = [15 x i8] c"input too long\00"
define i64 @get_user_input(i8* %0, i64 %1, i8** %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8** %2, i8*** %7
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = load i32, i32* @stdin
%13 = call i32* @fgets(i8* %10, i64 %11, i32 %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i8**, i8*** %7
store i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8** %16
store i64 0, i64* %4
br label %41
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = call i8* @strchr(i8* %18, i8 signext 10)
store i8* %19, i8** %8
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %34
21: ; preds = %17
%22 = call i32 (...) @getchar()
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp eq i32 %23, 10
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i32, i32* %9
%27 = load i32, i32* @EOF
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %25, %21
%30 = load i64, i64* %6
%31 = sub i64 %30, 1
store i64 %31, i64* %4
br label %41
32: ; preds = %25
%33 = load i8**, i8*** %7
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8** %33
store i64 0, i64* %4
br label %41
34: ; preds = %17
%35 = load i8*, i8** %8
store i8 0, i8* %35
%36 = load i8*, i8** %8
%37 = load i8*, i8** %5
%38 = ptrtoint i8* %36 to i64
%39 = ptrtoint i8* %37 to i64
%40 = sub i64 %38, %39
store i64 %40, i64* %4
br label %41
41: ; preds = %34, %32, %29, %15
%42 = load i64, i64* %4
ret i64 %42
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [33 x i8] c"Cannot read stdin or it is empty\00"
@EOF = global i32 0
@.str.1 = [15 x i8] c"input too long\00"
define i64 @get_user_input(i8* %0, i64 %1, i8** nocapture writeonly %2) {
%4 = load i32, i32* @stdin
%5 = tail call i32* @fgets(i8* %0, i64 %1, i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %8
7: ; preds = %3
store i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8** %2
br label %24
8: ; preds = %3
%9 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 10)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %20
11: ; preds = %8
%12 = tail call i32 (...) @getchar()
%13 = icmp eq i32 %12, 10
%14 = load i32, i32* @EOF
%15 = icmp eq i32 %12, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = add i64 %1, -1
br label %24
19: ; preds = %11
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %24
20: ; preds = %8
store i8 0, i8* %9
%21 = ptrtoint i8* %9 to i64
%22 = ptrtoint i8* %0 to i64
%23 = sub i64 %21, %22
br label %24
24: ; preds = %17, %19, %20, %7
%25 = phi i64 [ 0, %7 ], [ %23, %20 ], [ %18, %17 ], [ 0, %19 ]
ret i64 %25
}