#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*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int readString(char buffer[400], int bufferPosition, int maxLength) {
  char string[maxLength + 1];
  int i;
  fgets(string, maxLength, stdin);
  for (i = 0; string[i]; i++) {
    if (string[i] == '\n')
      break;
    buffer[bufferPosition++] = string[i];
  }
  buffer[bufferPosition++] = '\0';
  return bufferPosition;
}


x86 assembly (clang, -O0):
<readString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%esi
mov    0x2e93(%rip),%rax
mov    (%rax),%edx
call   1030 <fgets@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11bd <readString+0xad>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    118f <readString+0x7f>
jmp    11bd <readString+0xad>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0xc(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1163 <readString+0x53>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0xc(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readString>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
lea    0x1(%rdx),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %rbx,%rdi
mov    %edx,%esi
mov    %eax,%edx
call   1030 <fgets@plt>
movslq %r15d,%rcx
lea    (%r14,%rcx,1),%rdi
shl    $0x20,%rcx
xor    %eax,%eax
movabs $0x100000000,%rsi
movzbl (%rbx,%rax,1),%edx
test   %dl,%dl
je     1179 <readString+0x69>
cmp    $0xa,%dl
je     1179 <readString+0x69>
mov    %dl,(%rdi,%rax,1)
add    $0x1,%rax
add    %rsi,%rcx
jmp    1160 <readString+0x50>
add    %r15d,%eax
add    $0x1,%eax
sar    $0x20,%rcx
movb   $0x0,(%r14,%rcx,1)
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
define i32 @readString(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %6
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
%15 = load i32, i32* %6
%16 = load i32, i32* @stdin
%17 = call i32 @fgets(i8* %14, i32 %15, i32 %16)
store i32 0, i32* %9
br label %18
18: ; preds = %42, %3
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %14, i64 %20
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 0
br i1 %23, label %24, label %45
24: ; preds = %18
%25 = load i32, i32* %9
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %14, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 10
br i1 %30, label %31, label %32
31: ; preds = %24
br label %45
32: ; preds = %24
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %14, i64 %34
%36 = load i8, i8* %35
%37 = load i8*, i8** %4
%38 = load i32, i32* %5
%39 = add nsw i32 %38, 1
store i32 %39, i32* %5
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 %36, i8* %41
br label %42
42: ; preds = %32
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 1
store i32 %44, i32* %9
br label %18
45: ; preds = %31, %18
%46 = load i8*, i8** %4
%47 = load i32, i32* %5
%48 = add nsw i32 %47, 1
store i32 %48, i32* %5
%49 = sext i32 %47 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
store i8 0, i8* %50
%51 = load i32, i32* %5
%52 = load i8*, i8** %7
ret i32 %51
}

LLVM IR (-O2):
@stdin = global i32 0
define i32 @readString(i8* nocapture writeonly %0, i32 %1, i32 %2) {
%4 = add nsw i32 %2, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = load i32, i32* @stdin
%8 = call i32 @fgets(i8* %6, i32 %2, i32 %7)
%9 = sext i32 %1 to i64
br label %10
10: ; preds = %15, %3
%11 = phi i64 [ %18, %15 ], [ 0, %3 ]
%12 = phi i64 [ %16, %15 ], [ %9, %3 ]
%13 = getelementptr inbounds i8, i8* %6, i64 %11
%14 = load i8, i8* %13
switch i8 %14, label %15 [
i8 0, label %19
i8 10, label %19
]
15: ; preds = %10
%16 = add i64 %12, 1
%17 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 %14, i8* %17
%18 = add nuw i64 %11, 1
br label %10
19: ; preds = %10, %10
%20 = trunc i64 %12 to i32
%21 = add nsw i32 %20, 1
%22 = shl i64 %12, 32
%23 = ashr exact i64 %22, 32
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
ret i32 %21
}