; LLVM IR from chirag-singhal/Coding-Interview-101 :: Miscellaneous/Labs/Lab8 (hash)

; 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"

@__const.inputParser.filename = external hidden unnamed_addr constant [22 x i8], align 16
@.str = external hidden unnamed_addr constant [2 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; 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 @inputParser(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca [22 x i8], align 16
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 22, ptr %3) #6
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %3, ptr align 16 @__const.inputParser.filename, i64 22, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #6
  store i32 0, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #6
  store i32 10, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  %10 = call noalias ptr @malloc(i64 noundef 80) #7
  store ptr %10, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #6
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %21, %1
  %12 = load i32, ptr %7, align 4, !tbaa !9
  %13 = icmp slt i32 %12, 10
  br i1 %13, label %15, label %14

14:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #6
  br label %24

15:                                               ; preds = %11
  %16 = call noalias ptr @malloc(i64 noundef 40) #7
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = load i32, ptr %7, align 4, !tbaa !9
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds ptr, ptr %17, i64 %19
  store ptr %16, ptr %20, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %15
  %22 = load i32, ptr %7, align 4, !tbaa !9
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr %7, align 4, !tbaa !9
  br label %11, !llvm.loop !11

24:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  %25 = getelementptr inbounds [22 x i8], ptr %3, i64 0, i64 0
  %26 = call noalias ptr @fopen(ptr noundef %25, ptr noundef @.str)
  store ptr %26, ptr %8, align 8, !tbaa !5
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = icmp eq ptr %27, null
  br i1 %28, label %29, label %31

29:                                               ; preds = %24
  %30 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  br label %75

31:                                               ; preds = %24
  br label %32

32:                                               ; preds = %73, %31
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = load i32, ptr %4, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds ptr, ptr %34, i64 %36
  %38 = load ptr, ptr %37, align 8, !tbaa !5
  %39 = call i32 (ptr, ptr, ...) @__isoc99_fscanf(ptr noundef %33, ptr noundef @.str.2, ptr noundef %38)
  %40 = icmp ne i32 %39, -1
  br i1 %40, label %41, label %74

41:                                               ; preds = %32
  %42 = load i32, ptr %4, align 4, !tbaa !9
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %4, align 4, !tbaa !9
  %44 = load i32, ptr %4, align 4, !tbaa !9
  %45 = load i32, ptr %5, align 4, !tbaa !9
  %46 = icmp eq i32 %44, %45
  br i1 %46, label %47, label %73

47:                                               ; preds = %41
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = load i32, ptr %5, align 4, !tbaa !9
  %50 = sext i32 %49 to i64
  %51 = mul i64 8, %50
  %52 = mul i64 %51, 2
  %53 = call ptr @realloc(ptr noundef %48, i64 noundef %52) #8
  store ptr %53, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #6
  %54 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %54, ptr %9, align 4, !tbaa !9
  br label %55

55:                                               ; preds = %67, %47
  %56 = load i32, ptr %9, align 4, !tbaa !9
  %57 = load i32, ptr %5, align 4, !tbaa !9
  %58 = mul nsw i32 %57, 2
  %59 = icmp slt i32 %56, %58
  br i1 %59, label %61, label %60

60:                                               ; preds = %55
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #6
  br label %70

61:                                               ; preds = %55
  %62 = call noalias ptr @malloc(i64 noundef 40) #7
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = load i32, ptr %9, align 4, !tbaa !9
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds ptr, ptr %63, i64 %65
  store ptr %62, ptr %66, align 8, !tbaa !5
  br label %67

67:                                               ; preds = %61
  %68 = load i32, ptr %9, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %9, align 4, !tbaa !9
  br label %55, !llvm.loop !13

70:                                               ; preds = %60
  %71 = load i32, ptr %5, align 4, !tbaa !9
  %72 = mul nsw i32 %71, 2
  store i32 %72, ptr %5, align 4, !tbaa !9
  br label %73

73:                                               ; preds = %70, %41
  br label %32, !llvm.loop !14

74:                                               ; preds = %32
  br label %75

75:                                               ; preds = %74, %29
  %76 = load i32, ptr %4, align 4, !tbaa !9
  %77 = add nsw i32 %76, 1
  %78 = load ptr, ptr %2, align 8, !tbaa !5
  store i32 %77, ptr %78, align 4, !tbaa !9
  %79 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #6
  call void @llvm.lifetime.end.p0(i64 22, ptr %3) #6
  ret ptr %79
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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

declare noalias ptr @fopen(ptr noundef, ptr noundef) #4

declare i32 @printf(ptr noundef, ...) #4

declare i32 @__isoc99_fscanf(ptr noundef, 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { "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 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 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


; 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"

@__const.inputParser.filename = external hidden unnamed_addr constant [22 x i8], align 16
@.str = external hidden unnamed_addr constant [2 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local noalias ptr @inputParser(ptr nocapture noundef writeonly %0) local_unnamed_addr #0 {
  %2 = tail call noalias dereferenceable_or_null(80) ptr @malloc(i64 noundef 80) #5
  %3 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #5
  store ptr %3, ptr %2, align 8, !tbaa !5
  %4 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #5
  %5 = getelementptr inbounds i8, ptr %2, i64 8
  store ptr %4, ptr %5, align 8, !tbaa !5
  %6 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #5
  %7 = getelementptr inbounds i8, ptr %2, i64 16
  store ptr %6, ptr %7, align 8, !tbaa !5
  %8 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #5
  %9 = getelementptr inbounds i8, ptr %2, i64 24
  store ptr %8, ptr %9, align 8, !tbaa !5
  %10 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #5
  %11 = getelementptr inbounds i8, ptr %2, i64 32
  store ptr %10, ptr %11, align 8, !tbaa !5
  %12 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #5
  %13 = getelementptr inbounds i8, ptr %2, i64 40
  store ptr %12, ptr %13, align 8, !tbaa !5
  %14 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #5
  %15 = getelementptr inbounds i8, ptr %2, i64 48
  store ptr %14, ptr %15, align 8, !tbaa !5
  %16 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #5
  %17 = getelementptr inbounds i8, ptr %2, i64 56
  store ptr %16, ptr %17, align 8, !tbaa !5
  %18 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #5
  %19 = getelementptr inbounds i8, ptr %2, i64 64
  store ptr %18, ptr %19, align 8, !tbaa !5
  %20 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #5
  %21 = getelementptr inbounds i8, ptr %2, i64 72
  store ptr %20, ptr %21, align 8, !tbaa !5
  %22 = tail call noalias ptr @fopen(ptr noundef nonnull @__const.inputParser.filename, ptr noundef nonnull @.str)
  %23 = icmp eq ptr %22, null
  br i1 %23, label %25, label %.preheader

.preheader:                                       ; preds = %1
  %24 = tail call i32 (ptr, ptr, ...) @__isoc99_fscanf(ptr noundef nonnull %22, ptr noundef nonnull @.str.2, ptr noundef %3) #6
  %.not32 = icmp eq i32 %24, -1
  br i1 %.not32, label %.loopexit29, label %.lr.ph36

25:                                               ; preds = %1
  %26 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  br label %.loopexit29

.lr.ph36:                                         ; preds = %.preheader, %.loopexit
  %indvars.iv40 = phi i64 [ %indvars.iv.next41, %.loopexit ], [ 0, %.preheader ]
  %.02434 = phi ptr [ %.125, %.loopexit ], [ %2, %.preheader ]
  %.02633 = phi i32 [ %.127, %.loopexit ], [ 10, %.preheader ]
  %indvars.iv.next41 = add nuw nsw i64 %indvars.iv40, 1
  %27 = zext i32 %.02633 to i64
  %28 = icmp eq i64 %indvars.iv.next41, %27
  br i1 %28, label %29, label %.loopexit

29:                                               ; preds = %.lr.ph36
  %30 = sext i32 %.02633 to i64
  %31 = shl nsw i64 %30, 4
  %32 = tail call ptr @realloc(ptr noundef nonnull %.02434, i64 noundef %31) #7
  %33 = shl nsw i32 %.02633, 1
  %34 = icmp slt i32 %.02633, %33
  br i1 %34, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %29
  %wide.trip.count = sext i32 %33 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %30, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %35 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #5
  %36 = getelementptr inbounds ptr, ptr %32, i64 %indvars.iv
  store ptr %35, ptr %36, align 8, !tbaa !5
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph, %29, %.lr.ph36
  %.127 = phi i32 [ %.02633, %.lr.ph36 ], [ %33, %29 ], [ %33, %.lr.ph ]
  %.125 = phi ptr [ %.02434, %.lr.ph36 ], [ %32, %29 ], [ %32, %.lr.ph ]
  %37 = getelementptr inbounds ptr, ptr %.125, i64 %indvars.iv.next41
  %38 = load ptr, ptr %37, align 8, !tbaa !5
  %39 = tail call i32 (ptr, ptr, ...) @__isoc99_fscanf(ptr noundef nonnull %22, ptr noundef nonnull @.str.2, ptr noundef %38) #6
  %.not = icmp eq i32 %39, -1
  br i1 %.not, label %.loopexit29.loopexit, label %.lr.ph36, !llvm.loop !11

.loopexit29.loopexit:                             ; preds = %.loopexit
  %40 = trunc nuw nsw i64 %indvars.iv.next41 to i32
  %41 = add nuw nsw i32 %40, 1
  br label %.loopexit29

.loopexit29:                                      ; preds = %.loopexit29.loopexit, %.preheader, %25
  %.2 = phi ptr [ %2, %25 ], [ %2, %.preheader ], [ %.125, %.loopexit29.loopexit ]
  %.1 = phi i32 [ 1, %25 ], [ 1, %.preheader ], [ %41, %.loopexit29.loopexit ]
  store i32 %.1, ptr %0, align 4, !tbaa !12
  ret ptr %.2
}

; 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 noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #2

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

declare i32 @__isoc99_fscanf(ptr noundef, ptr 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 = { "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 }
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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass