#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 getc(int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ stdin;
char *getstring(int length) {
  char *buffer = (char *)malloc(255);
  char buff_char;
  for (int i = 0; i < length; i++) {
    buff_char = getc(stdin);
    if (buff_char == '\n') {
      buffer[i] = '\0';
      return buffer;
    } else {
      buffer[i] = buff_char;
    }
  }
  buffer[length] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<getstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    $0xff,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a4 <getstring+0x84>
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
call   1040 <getc@plt>
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    1183 <getstring+0x63>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b8 <getstring+0x98>
mov    -0x19(%rbp),%dl
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1196 <getstring+0x76>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1140 <getstring+0x20>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getstring>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
mov    $0xff,%edi
call   1030 <malloc@plt>
mov    %rax,%r15
test   %r14d,%r14d
jle    116a <getstring+0x4a>
mov    %r14d,%ebx
xor    %ebp,%ebp
mov    0x2e9f(%rip),%r12
nopl   0x0(%rax)
mov    (%r12),%edi
call   1040 <getc@plt>
cmp    $0xa,%al
je     116d <getstring+0x4d>
mov    %al,(%r15,%rbp,1)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1150 <getstring+0x30>
movslq %r14d,%rbp
movb   $0x0,(%r15,%rbp,1)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
define i8* @getstring(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
store i32 %0, i32* %3
%7 = call i64 @malloc(i32 255)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %4
store i32 0, i32* %6
br label %9
9: ; preds = %32, %1
%10 = load i32, i32* %6
%11 = load i32, i32* %3
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %35
13: ; preds = %9
%14 = load i32, i32* @stdin
%15 = call signext i8 @getc(i32 %14)
store i8 %15, i8* %5
%16 = load i8, i8* %5
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 10
br i1 %18, label %19, label %25
19: ; preds = %13
%20 = load i8*, i8** %4
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 0, i8* %23
%24 = load i8*, i8** %4
store i8* %24, i8** %2
br label %41
25: ; preds = %13
%26 = load i8, i8* %5
%27 = load i8*, i8** %4
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 %26, i8* %30
br label %31
31: ; preds = %25
br label %32
32: ; preds = %31
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
store i32 %34, i32* %6
br label %9
35: ; preds = %9
%36 = load i8*, i8** %4
%37 = load i32, i32* %3
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 0, i8* %39
%40 = load i8*, i8** %4
store i8* %40, i8** %2
br label %41
41: ; preds = %35, %19
%42 = load i8*, i8** %2
ret i8* %42
}

LLVM IR (-O2):
@stdin = global i32 0
define i8* @getstring(i32 %0) {
%2 = tail call i64 @malloc(i32 255)
%3 = inttoptr i64 %2 to i8*
%4 = icmp sgt i32 %0, 0
br i1 %4, label %5, label %18
5: ; preds = %1
%6 = zext i32 %0 to i64
br label %7
7: ; preds = %5, %14
%8 = phi i64 [ 0, %5 ], [ %16, %14 ]
%9 = load i32, i32* @stdin
%10 = tail call signext i8 @getc(i32 %9)
%11 = icmp eq i8 %10, 10
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = and i64 %8, 4294967295
br label %20
14: ; preds = %7
%15 = getelementptr inbounds i8, i8* %3, i64 %8
store i8 %10, i8* %15
%16 = add nuw nsw i64 %8, 1
%17 = icmp eq i64 %16, %6
br i1 %17, label %18, label %7
18: ; preds = %14, %1
%19 = sext i32 %0 to i64
br label %20
20: ; preds = %12, %18
%21 = phi i64 [ %13, %12 ], [ %19, %18 ]
%22 = getelementptr inbounds i8, i8* %3, i64 %21
store i8 0, i8* %22
ret i8* %3
}