#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 */
char *fgets(char *, int, int /*<<< orphan*/);
char getc(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int strlen(char *);
int read_from_user(char *prompt, char *storage, int max_size) {
  char *tmp;
  printf("%s ", prompt);
  tmp = fgets(storage, max_size, stdin);
  if (tmp == NULL) {
    return -1;
  }
  if (storage[strlen(storage) - 1] == '\n') {
    storage[strlen(storage) - 1] = '\0';
  } else {
    while (getc(stdin) != '\n') {
      printf(".");
    }
    return 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_from_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2e75(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1195 <read_from_user+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <read_from_user+0xea>
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
jne    11e5 <read_from_user+0xa5>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1223 <read_from_user+0xe3>
jmp    11ea <read_from_user+0xaa>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1060 <getc@plt>
movsbl %al,%eax
cmp    $0xa,%eax
je     1217 <read_from_user+0xd7>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ea <read_from_user+0xaa>
movl   $0x2,-0x4(%rbp)
jmp    122a <read_from_user+0xea>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_user>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rsi
lea    0xe9c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e76(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     11a9 <read_from_user+0x59>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11b5 <read_from_user+0x65>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
xor    %ebx,%ebx
jmp    11ae <read_from_user+0x5e>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x0(%rbp),%edi
call   1070 <getc@plt>
mov    $0x2,%ebx
cmp    $0xa,%al
je     11ae <read_from_user+0x5e>
cs nopw 0x0(%rax,%rax,1)
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    0x0(%rbp),%edi
call   1070 <getc@plt>
cmp    $0xa,%al
jne    11d0 <read_from_user+0x80>
jmp    11ae <read_from_user+0x5e>

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

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