; LLVM IR from smv1999/CompetitiveProgrammingQuestionBank :: Hackerrank solutions/30 days Of Code (Day9-Recursion3)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@stdin = external global ptr, align 8

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @readline() #1 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #6
  store i64 1024, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #6
  store i64 0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #6
  %8 = load i64, ptr %1, align 8, !tbaa !5
  %9 = call noalias ptr @malloc(i64 noundef %8) #7
  store ptr %9, ptr %3, align 8, !tbaa !9
  br label %10

10:                                               ; preds = %57, %0
  br label %11

11:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #6
  %12 = load ptr, ptr %3, align 8, !tbaa !9
  %13 = load i64, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %12, i64 %13
  store ptr %14, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %15 = load ptr, ptr %4, align 8, !tbaa !9
  %16 = load i64, ptr %1, align 8, !tbaa !5
  %17 = load i64, ptr %2, align 8, !tbaa !5
  %18 = sub i64 %16, %17
  %19 = trunc i64 %18 to i32
  %20 = load ptr, ptr @stdin, align 8, !tbaa !9
  %21 = call ptr @fgets(ptr noundef %15, i32 noundef %19, ptr noundef %20)
  store ptr %21, ptr %5, align 8, !tbaa !9
  %22 = load ptr, ptr %5, align 8, !tbaa !9
  %23 = icmp ne ptr %22, null
  br i1 %23, label %25, label %24

24:                                               ; preds = %11
  store i32 3, ptr %6, align 4
  br label %55

25:                                               ; preds = %11
  %26 = load ptr, ptr %4, align 8, !tbaa !9
  %27 = call i64 @strlen(ptr noundef %26) #8
  %28 = load i64, ptr %2, align 8, !tbaa !5
  %29 = add i64 %28, %27
  store i64 %29, ptr %2, align 8, !tbaa !5
  %30 = load i64, ptr %2, align 8, !tbaa !5
  %31 = load i64, ptr %1, align 8, !tbaa !5
  %32 = sub i64 %31, 1
  %33 = icmp ult i64 %30, %32
  br i1 %33, label %42, label %34

34:                                               ; preds = %25
  %35 = load ptr, ptr %3, align 8, !tbaa !9
  %36 = load i64, ptr %2, align 8, !tbaa !5
  %37 = sub i64 %36, 1
  %38 = getelementptr inbounds i8, ptr %35, i64 %37
  %39 = load i8, ptr %38, align 1, !tbaa !11
  %40 = sext i8 %39 to i32
  %41 = icmp eq i32 %40, 10
  br i1 %41, label %42, label %43

42:                                               ; preds = %34, %25
  store i32 3, ptr %6, align 4
  br label %55

43:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #6
  %44 = load i64, ptr %1, align 8, !tbaa !5
  %45 = shl i64 %44, 1
  store i64 %45, ptr %7, align 8, !tbaa !5
  %46 = load ptr, ptr %3, align 8, !tbaa !9
  %47 = load i64, ptr %7, align 8, !tbaa !5
  %48 = call ptr @realloc(ptr noundef %46, i64 noundef %47) #9
  store ptr %48, ptr %3, align 8, !tbaa !9
  %49 = load ptr, ptr %3, align 8, !tbaa !9
  %50 = icmp ne ptr %49, null
  br i1 %50, label %52, label %51

51:                                               ; preds = %43
  store i32 3, ptr %6, align 4
  br label %54

52:                                               ; preds = %43
  %53 = load i64, ptr %7, align 8, !tbaa !5
  store i64 %53, ptr %1, align 8, !tbaa !5
  store i32 0, ptr %6, align 4
  br label %54

54:                                               ; preds = %52, %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  br label %55

55:                                               ; preds = %54, %42, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #6
  %56 = load i32, ptr %6, align 4
  switch i32 %56, label %76 [
    i32 0, label %57
    i32 3, label %58
  ]

57:                                               ; preds = %55
  br label %10

58:                                               ; preds = %55
  %59 = load ptr, ptr %3, align 8, !tbaa !9
  %60 = load i64, ptr %2, align 8, !tbaa !5
  %61 = sub i64 %60, 1
  %62 = getelementptr inbounds i8, ptr %59, i64 %61
  %63 = load i8, ptr %62, align 1, !tbaa !11
  %64 = sext i8 %63 to i32
  %65 = icmp eq i32 %64, 10
  br i1 %65, label %66, label %71

66:                                               ; preds = %58
  %67 = load ptr, ptr %3, align 8, !tbaa !9
  %68 = load i64, ptr %2, align 8, !tbaa !5
  %69 = sub i64 %68, 1
  %70 = getelementptr inbounds i8, ptr %67, i64 %69
  store i8 0, ptr %70, align 1, !tbaa !11
  br label %71

71:                                               ; preds = %66, %58
  %72 = load ptr, ptr %3, align 8, !tbaa !9
  %73 = load i64, ptr %2, align 8, !tbaa !5
  %74 = call ptr @realloc(ptr noundef %72, i64 noundef %73) #9
  store ptr %74, ptr %3, align 8, !tbaa !9
  %75 = load ptr, ptr %3, align 8, !tbaa !9
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #6
  ret ptr %75

76:                                               ; preds = %55
  unreachable
}

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

declare ptr @fgets(ptr noundef, i32 noundef, ptr noundef) #3

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #4

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind allocsize(0) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { nounwind willreturn memory(read) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #5 = { nounwind allocsize(1) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }
attributes #8 = { nounwind willreturn memory(read) }
attributes #9 = { nounwind allocsize(1) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@stdin = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @readline() local_unnamed_addr #0 {
  %1 = tail call noalias dereferenceable_or_null(1024) ptr @malloc(i64 noundef 1024) #5
  br label %2

2:                                                ; preds = %18, %0
  %.029 = phi ptr [ %1, %0 ], [ %20, %18 ]
  %.027 = phi i64 [ 0, %0 ], [ %10, %18 ]
  %.0 = phi i64 [ 1024, %0 ], [ %19, %18 ]
  %3 = getelementptr inbounds i8, ptr %.029, i64 %.027
  %4 = sub i64 %.0, %.027
  %5 = trunc i64 %4 to i32
  %6 = load ptr, ptr @stdin, align 8, !tbaa !5
  %7 = tail call ptr @fgets(ptr noundef %3, i32 noundef %5, ptr noundef %6)
  %.not = icmp eq ptr %7, null
  br i1 %.not, label %select.unfold, label %8

8:                                                ; preds = %2
  %9 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %3) #6
  %10 = add i64 %9, %.027
  %11 = add i64 %.0, -1
  %12 = icmp ult i64 %10, %11
  br i1 %12, label %select.unfold, label %13

13:                                               ; preds = %8
  %14 = add i64 %10, -1
  %15 = getelementptr inbounds i8, ptr %.029, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !9
  %17 = icmp eq i8 %16, 10
  br i1 %17, label %select.unfold, label %18

18:                                               ; preds = %13
  %19 = shl i64 %.0, 1
  %20 = tail call ptr @realloc(ptr noundef nonnull %.029, i64 noundef %19) #7
  %.not32 = icmp eq ptr %20, null
  br i1 %.not32, label %select.unfold, label %2

select.unfold:                                    ; preds = %18, %2, %13, %8
  %.130.ph = phi ptr [ %.029, %8 ], [ %.029, %13 ], [ %.029, %2 ], [ null, %18 ]
  %.128.ph = phi i64 [ %10, %8 ], [ %10, %13 ], [ %.027, %2 ], [ %10, %18 ]
  %21 = add i64 %.128.ph, -1
  %22 = getelementptr inbounds i8, ptr %.130.ph, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !9
  %24 = icmp eq i8 %23, 10
  br i1 %24, label %25, label %26

25:                                               ; preds = %select.unfold
  store i8 0, ptr %22, align 1, !tbaa !9
  br label %26

26:                                               ; preds = %25, %select.unfold
  %27 = tail call ptr @realloc(ptr noundef nonnull %.130.ph, i64 noundef %.128.ph) #7
  ret ptr %27
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef ptr @fgets(ptr noundef, i32 noundef, ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #4

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nofree nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { mustprogress nofree nounwind willreturn memory(argmem: read) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #5 = { nounwind allocsize(0) }
attributes #6 = { nounwind willreturn memory(read) }
attributes #7 = { nounwind allocsize(1) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass