#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, unsigned int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ readRestOfLine();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int getString(char *string, unsigned length, char *prompt) {
  int finished = 0;
  char *tempString;
  if ((tempString = malloc(sizeof(char) * (length + 2))) == NULL) {
    fprintf(stderr, "Fatal error: malloc() failed in getString().\n");
    exit(EXIT_FAILURE);
  }
  do {
    printf("%s", prompt);
    fgets(tempString, length + 2, stdin);
    if (tempString[strlen(tempString) - 1] != '\n') {
      printf("Input was too long.\n");
      readRestOfLine();
    } else {
      finished = 1;
    }
  } while (finished == 0);
  tempString[strlen(tempString) - 1] = '\0';
  strcpy(string, tempString);
  free(tempString);
  return 1;
}


x86 assembly (clang, -O0):
<getString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
add    $0x2,%eax
mov    %eax,%eax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ee <getString+0x5e>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    11f3 <getString+0x63>
mov    -0x18(%rbp),%rsi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
add    $0x2,%esi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
call   1070 <fgets@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     1261 <getString+0xd1>
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1090 <readRestOfLine@plt>
jmp    1268 <getString+0xd8>
movl   $0x1,-0x1c(%rbp)
jmp    126d <getString+0xdd>
cmpl   $0x0,-0x1c(%rbp)
je     11f3 <getString+0x63>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,(%rsp)
add    $0x2,%ebp
mov    %ebp,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1281 <getString+0xe1>
mov    %rax,%rbx
lea    0xe5d(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2e06(%rip),%r14
mov    (%r14),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
je     124a <getString+0xaa>
lea    0xe2a(%rip),%r12
lea    0xe20(%rip),%r13
xchg   %ax,%ax
mov    %r12,%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   10a0 <readRestOfLine@plt>
mov    %r13,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1210 <getString+0x70>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    (%rsp),%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
lea    0xd6f(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [46 x i8] c"Fatal error: malloc() failed in getString().\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@stdin = global i32 0
@.str.2 = [21 x i8] c"Input was too long.\0A\00"
define i32 @getString(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i32 0, i32* %7
%9 = load i32, i32* %5
%10 = add i32 %9, 2
%11 = zext i32 %10 to i64
%12 = mul i64 1, %11
%13 = trunc i64 %12 to i32
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %8
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %21
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %3
br label %22
22: ; preds = %44, %21
%23 = load i8*, i8** %6
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %8
%26 = load i32, i32* %5
%27 = add i32 %26, 2
%28 = load i32, i32* @stdin
%29 = call i32 @fgets(i8* %25, i32 %27, i32 %28)
%30 = load i8*, i8** %8
%31 = load i8*, i8** %8
%32 = call i32 @strlen(i8* %31)
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %30, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 10
br i1 %38, label %39, label %42
39: ; preds = %22
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 (...) @readRestOfLine()
br label %43
42: ; preds = %22
store i32 1, i32* %7
br label %43
43: ; preds = %42, %39
br label %44
44: ; preds = %43
%45 = load i32, i32* %7
%46 = icmp eq i32 %45, 0
br i1 %46, label %22, label %47
47: ; preds = %44
%48 = load i8*, i8** %8
%49 = load i8*, i8** %8
%50 = call i32 @strlen(i8* %49)
%51 = sub nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %48, i64 %52
store i8 0, i8* %53
%54 = load i8*, i8** %4
%55 = load i8*, i8** %8
%56 = call i32 @strcpy(i8* %54, i8* %55)
%57 = load i8*, i8** %8
%58 = call i32 @free(i8* %57)
ret i32 1
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [46 x i8] c"Fatal error: malloc() failed in getString().\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@stdin = global i32 0
@str = [20 x i8] c"Input was too long.\00"
define i32 @getString(i8* %0, i32 %1, i8* %2) {
%4 = add i32 %1, 2
%5 = tail call i8* @malloc(i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %17, label %7
7: ; preds = %3
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%9 = load i32, i32* @stdin
%10 = tail call i32 @fgets(i8* %5, i32 %4, i32 %9)
%11 = tail call i32 @strlen(i8* %5)
%12 = add nsw i32 %11, -1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %5, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 10
br i1 %16, label %34, label %22
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %7, %22
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%24 = tail call i32 (...) @readRestOfLine()
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%26 = load i32, i32* @stdin
%27 = tail call i32 @fgets(i8* %5, i32 %4, i32 %26)
%28 = tail call i32 @strlen(i8* %5)
%29 = add nsw i32 %28, -1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %5, i64 %30
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 10
br i1 %33, label %34, label %22
34: ; preds = %22, %7
%35 = tail call i32 @strlen(i8* %5)
%36 = add nsw i32 %35, -1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %5, i64 %37
store i8 0, i8* %38
%39 = tail call i32 @strcpy(i8* %0, i8* %5)
%40 = tail call i32 @free(i8* %5)
ret i32 1
}