#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 */
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
char *strdup(char *);
int strlen(char *);
char *readString() {
  char buffer[1024];
  buffer[0] = '\0';
  fgets(buffer, 1024, stdin);
  uint32_t blen = strlen(buffer);
  if (blen > 0 && buffer[blen - 1] == '\n')
    buffer[blen - 1] = '\0';
  return strdup(buffer);
}


x86 assembly (clang, -O0):
<readString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movb   $0x0,-0x400(%rbp)
lea    -0x400(%rbp),%rdi
mov    0x2e98(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1040 <fgets@plt>
lea    -0x400(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x404(%rbp)
cmpl   $0x0,-0x404(%rbp)
jle    11aa <readString+0x7a>
mov    -0x404(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x400(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    11aa <readString+0x7a>
mov    -0x404(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x400(%rbp,%rax,1)
lea    -0x400(%rbp),%rdi
call   1050 <strdup@plt>
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readString>:
push   %rbx
sub    $0x400,%rsp
movb   $0x0,(%rsp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1040 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    116e <readString+0x3e>
add    $0xffffffff,%eax
cmpb   $0xa,(%rsp,%rax,1)
jne    116e <readString+0x3e>
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
call   1050 <strdup@plt>
add    $0x400,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@stdin = global i32 0
define i8* @readString() {
%1 = alloca [1024 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
store i8 0, i8* %3
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%5 = load i32, i32* @stdin
%6 = call i32 @fgets(i8* %4, i32 1024, i32 %5)
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %0
%12 = load i32, i32* %2
%13 = sub nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 10
br i1 %18, label %19, label %24
19: ; preds = %11
%20 = load i32, i32* %2
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %19, %11, %0
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%26 = call i8* @strdup(i8* %25)
ret i8* %26
}

LLVM IR (-O2):
@stdin = global i32 0
define noalias i8* @readString() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
store i8 0, i8* %2
%3 = load i32, i32* @stdin
%4 = call i32 @fgets(i8* %2, i32 1024, i32 %3)
%5 = call i32 @strlen(i8* %2)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %0
%8 = add nsw i32 %5, -1
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 10
br i1 %12, label %13, label %14
13: ; preds = %7
store i8 0, i8* %10
br label %14
14: ; preds = %13, %7, %0
%15 = call i8* @strdup(i8* %2)
ret i8* %15
}