#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(int);
char fgetc(int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ stdin;
char *readCli(int *bufferSize) {
  printf("Enter your text: ");
  int size_b = 40;
  char *buffer;
  buffer = (char *)malloc(size_b * sizeof(char));
  if (buffer == NULL) {
    exit(1);
  }
  char c;
  int i = 0;
  while ((c = fgetc(stdin)) != '\n') {
    if (((i % 40) == 0) && i != 0) {
      buffer = (char *)realloc(buffer, (size_b * 2) * sizeof(char));
      if (buffer == NULL) {
        exit(3);
      }
    }
    if (i >= 80) {
      printf("Maximum buffer size exceeded! Maximum is: %d characters.", 80);
      break;
    }
    buffer[i++] = c;
  }
  buffer[i] = '\0';
  buffer = (char *)realloc(buffer, (i + 1) * sizeof(char));
  if (buffer == NULL) {
    exit(4);
  }
  *bufferSize = i + 1;
  return buffer;
}


x86 assembly (clang, -O0):
<readCli>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x28,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1199 <readCli+0x49>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x20(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   1040 <fgetc@plt>
mov    %al,-0x19(%rbp)
movsbl %al,%eax
cmp    $0xa,%eax
je     124f <readCli+0xff>
mov    -0x20(%rbp),%eax
mov    $0x28,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1210 <readCli+0xc0>
cmpl   $0x0,-0x20(%rbp)
je     1210 <readCli+0xc0>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    120b <readCli+0xbb>
mov    $0x3,%edi
call   1070 <exit@plt>
jmp    1210 <readCli+0xc0>
cmpl   $0x50,-0x20(%rbp)
jl     1232 <readCli+0xe2>
lea    0xdf1(%rip),%rdi
mov    $0x50,%esi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    124f <readCli+0xff>
mov    -0x19(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x20(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11a0 <readCli+0x50>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    128b <readCli+0x13b>
mov    $0x4,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readCli>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbp
lea    0xe9b(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x28,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1254 <readCli+0x104>
mov    %rax,%r15
mov    0x2e5d(%rip),%r12
mov    (%r12),%edi
call   1040 <fgetc@plt>
cmp    $0xa,%al
je     121f <readCli+0xcf>
mov    %eax,%r13d
mov    %rbp,(%rsp)
xor    %ebp,%ebp
mov    $0xcccccccd,%r14d
xor    %ebx,%ebx
nopl   (%rax)
test   %rbx,%rbx
je     11e0 <readCli+0x90>
mov    %ebp,%eax
imul   %r14,%rax
shr    $0x25,%rax
shl    $0x3,%eax
lea    (%rax,%rax,4),%eax
mov    %ebx,%ecx
sub    %eax,%ecx
jne    11e0 <readCli+0x90>
mov    %r15,%rdi
mov    $0x50,%esi
call   1060 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     124a <readCli+0xfa>
cmp    $0x50,%rbx
je     1203 <readCli+0xb3>
mov    %r13b,(%r15,%rbx,1)
add    $0x1,%rbx
mov    (%r12),%edi
call   1040 <fgetc@plt>
mov    %eax,%r13d
add    $0x1,%ebp
cmp    $0xa,%al
jne    11b0 <readCli+0x60>
jmp    121b <readCli+0xcb>
lea    0xe08(%rip),%rdi
mov    $0x50,%ebx
mov    $0x50,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%rsp),%rbp
mov    %ebx,%eax
movb   $0x0,(%r15,%rax,1)
add    $0x1,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     125e <readCli+0x10e>
mov    %ebx,0x0(%rbp)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x3,%edi
call   1070 <exit@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x4,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"Enter your text: \00"
@stdin = global i32 0
@.str.1 = [57 x i8] c"Maximum buffer size exceeded! Maximum is: %d characters.\00"
define i8* @readCli(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
store i32* %0, i32** %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 40, i32* %3
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = mul i64 %9, 1
%11 = trunc i64 %10 to i32
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %1
store i32 0, i32* %6
br label %19
19: ; preds = %50, %18
%20 = load i32, i32* @stdin
%21 = call signext i8 @fgetc(i32 %20)
store i8 %21, i8* %5
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 10
br i1 %23, label %24, label %57
24: ; preds = %19
%25 = load i32, i32* %6
%26 = srem i32 %25, 40
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %45
28: ; preds = %24
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %45
31: ; preds = %28
%32 = load i8*, i8** %4
%33 = load i32, i32* %3
%34 = mul nsw i32 %33, 2
%35 = sext i32 %34 to i64
%36 = mul i64 %35, 1
%37 = trunc i64 %36 to i32
%38 = call i64 @realloc(i8* %32, i32 %37)
%39 = inttoptr i64 %38 to i8*
store i8* %39, i8** %4
%40 = load i8*, i8** %4
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %44
42: ; preds = %31
%43 = call i32 @exit(i32 3)
unreachable
44: ; preds = %31
br label %45
45: ; preds = %44, %28, %24
%46 = load i32, i32* %6
%47 = icmp sge i32 %46, 80
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i32 80)
br label %57
50: ; preds = %45
%51 = load i8, i8* %5
%52 = load i8*, i8** %4
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
%55 = sext i32 %53 to i64
%56 = getelementptr inbounds i8, i8* %52, i64 %55
store i8 %51, i8* %56
br label %19
57: ; preds = %48, %19
%58 = load i8*, i8** %4
%59 = load i32, i32* %6
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
store i8 0, i8* %61
%62 = load i8*, i8** %4
%63 = load i32, i32* %6
%64 = add nsw i32 %63, 1
%65 = sext i32 %64 to i64
%66 = mul i64 %65, 1
%67 = trunc i64 %66 to i32
%68 = call i64 @realloc(i8* %62, i32 %67)
%69 = inttoptr i64 %68 to i8*
store i8* %69, i8** %4
%70 = load i8*, i8** %4
%71 = icmp eq i8* %70, null
br i1 %71, label %72, label %74
72: ; preds = %57
%73 = call i32 @exit(i32 4)
unreachable
74: ; preds = %57
%75 = load i32, i32* %6
%76 = add nsw i32 %75, 1
%77 = load i32*, i32** %2
store i32 %76, i32* %77
%78 = load i8*, i8** %4
ret i8* %78
}

LLVM IR (-O2):
@.str = [18 x i8] c"Enter your text: \00"
@stdin = global i32 0
@.str.1 = [57 x i8] c"Maximum buffer size exceeded! Maximum is: %d characters.\00"
define i8* @readCli(i32* nocapture writeonly %0) {
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%3 = tail call i64 @malloc(i32 40)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %1
%8 = inttoptr i64 %3 to i8*
%9 = load i32, i32* @stdin
%10 = tail call signext i8 @fgetc(i32 %9)
%11 = icmp eq i8 %10, 10
br i1 %11, label %40, label %12
12: ; preds = %7, %32
%13 = phi i64 [ %33, %32 ], [ 0, %7 ]
%14 = phi i8 [ %36, %32 ], [ %10, %7 ]
%15 = phi i8* [ %28, %32 ], [ %8, %7 ]
%16 = trunc i64 %13 to i32
%17 = urem i32 %16, 40
%18 = icmp eq i32 %17, 0
%19 = icmp ne i64 %13, 0
%20 = and i1 %19, %18
br i1 %20, label %21, label %27
21: ; preds = %12
%22 = tail call i64 @realloc(i8* %15, i32 80)
%23 = inttoptr i64 %22 to i8*
%24 = icmp eq i64 %22, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 @exit(i32 3)
unreachable
27: ; preds = %21, %12
%28 = phi i8* [ %23, %21 ], [ %15, %12 ]
%29 = icmp eq i64 %13, 80
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.1, i64 0, i64 0), i32 80)
br label %40
32: ; preds = %27
%33 = add nuw nsw i64 %13, 1
%34 = getelementptr inbounds i8, i8* %28, i64 %13
store i8 %14, i8* %34
%35 = load i32, i32* @stdin
%36 = tail call signext i8 @fgetc(i32 %35)
%37 = icmp eq i8 %36, 10
br i1 %37, label %38, label %12
38: ; preds = %32
%39 = trunc i64 %33 to i32
br label %40
40: ; preds = %38, %7, %30
%41 = phi i32 [ 80, %30 ], [ 0, %7 ], [ %39, %38 ]
%42 = phi i8* [ %28, %30 ], [ %8, %7 ], [ %28, %38 ]
%43 = zext i32 %41 to i64
%44 = getelementptr inbounds i8, i8* %42, i64 %43
store i8 0, i8* %44
%45 = add nuw nsw i32 %41, 1
%46 = tail call i64 @realloc(i8* %42, i32 %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %40
%49 = tail call i32 @exit(i32 4)
unreachable
50: ; preds = %40
%51 = inttoptr i64 %46 to i8*
store i32 %45, i32* %0
ret i8* %51
}