; LLVM IR from JessonYue/LeetCodeLearning :: Array/caiweixiong/c (homework_031)

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @firstUniqChar_(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca [26 x i32], align 16
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %12, label %11

11:                                               ; preds = %1
  store i32 -1, ptr %2, align 4
  br label %70

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = call i64 @strlen(ptr noundef %13) #5
  store i64 %14, ptr %4, align 8, !tbaa !9
  %15 = load i64, ptr %4, align 8, !tbaa !9
  %16 = icmp eq i64 %15, 0
  br i1 %16, label %17, label %18

17:                                               ; preds = %12
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %69

18:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 104, ptr %6) #4
  call void @llvm.memset.p0.i64(ptr align 16 %6, i8 0, i64 104, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 0, ptr %7, align 4, !tbaa !11
  br label %19

19:                                               ; preds = %37, %18
  %20 = load i32, ptr %7, align 4, !tbaa !11
  %21 = sext i32 %20 to i64
  %22 = load i64, ptr %4, align 8, !tbaa !9
  %23 = icmp ult i64 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %19
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %40

25:                                               ; preds = %19
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = load i32, ptr %7, align 4, !tbaa !11
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i8, ptr %26, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !13
  %31 = sext i8 %30 to i32
  %32 = sub nsw i32 %31, 97
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [26 x i32], ptr %6, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !11
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %34, align 4, !tbaa !11
  br label %37

37:                                               ; preds = %25
  %38 = load i32, ptr %7, align 4, !tbaa !11
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %7, align 4, !tbaa !11
  br label %19, !llvm.loop !14

40:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !11
  br label %41

41:                                               ; preds = %62, %40
  %42 = load i32, ptr %8, align 4, !tbaa !11
  %43 = sext i32 %42 to i64
  %44 = load i64, ptr %4, align 8, !tbaa !9
  %45 = icmp ult i64 %43, %44
  br i1 %45, label %47, label %46

46:                                               ; preds = %41
  store i32 5, ptr %5, align 4
  br label %65

47:                                               ; preds = %41
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = load i32, ptr %8, align 4, !tbaa !11
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds i8, ptr %48, i64 %50
  %52 = load i8, ptr %51, align 1, !tbaa !13
  %53 = sext i8 %52 to i32
  %54 = sub nsw i32 %53, 97
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [26 x i32], ptr %6, i64 0, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !11
  %58 = icmp eq i32 %57, 1
  br i1 %58, label %59, label %61

59:                                               ; preds = %47
  %60 = load i32, ptr %8, align 4, !tbaa !11
  store i32 %60, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %65

61:                                               ; preds = %47
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %8, align 4, !tbaa !11
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %8, align 4, !tbaa !11
  br label %41, !llvm.loop !16

65:                                               ; preds = %59, %46
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %66 = load i32, ptr %5, align 4
  switch i32 %66, label %68 [
    i32 5, label %67
  ]

67:                                               ; preds = %65
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %5, align 4
  br label %68

68:                                               ; preds = %67, %65
  call void @llvm.lifetime.end.p0(i64 104, ptr %6) #4
  br label %69

69:                                               ; preds = %68, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %70

70:                                               ; preds = %69, %11
  %71 = load i32, ptr %2, align 4
  ret i32 %71
}

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

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 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { nounwind }
attributes #5 = { nounwind willreturn memory(read) }

!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 = !{!"long long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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

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

; Function Attrs: nofree nounwind memory(argmem: read) uwtable
define dso_local i32 @firstUniqChar_(ptr noundef readonly %0) local_unnamed_addr #1 {
  %2 = alloca [26 x i32], align 16
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %39, label %3

3:                                                ; preds = %1
  %4 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #4
  %5 = icmp eq i64 %4, 0
  br i1 %5, label %39, label %6

6:                                                ; preds = %3
  call void @llvm.lifetime.start.p0(i64 104, ptr nonnull %2) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(104) %2, i8 0, i64 104, i1 false)
  %xtraiter = and i64 %4, 1
  %7 = icmp eq i64 %4, 1
  br i1 %7, label %.preheader.preheader.unr-lcssa, label %.new

.new:                                             ; preds = %6
  %unroll_iter = and i64 %4, -2
  br label %8

8:                                                ; preds = %8, %.new
  %indvars.iv = phi i64 [ 0, %.new ], [ %indvars.iv.next.1, %8 ]
  %niter = phi i64 [ 0, %.new ], [ %niter.next.1, %8 ]
  %9 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  %10 = load i8, ptr %9, align 1, !tbaa !5
  %11 = sext i8 %10 to i64
  %12 = add nsw i64 %11, -97
  %13 = getelementptr inbounds [26 x i32], ptr %2, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !8
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %13, align 4, !tbaa !8
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %16 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next
  %17 = load i8, ptr %16, align 1, !tbaa !5
  %18 = sext i8 %17 to i64
  %19 = add nsw i64 %18, -97
  %20 = getelementptr inbounds [26 x i32], ptr %2, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !8
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %20, align 4, !tbaa !8
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.preheader.preheader.unr-lcssa, label %8, !llvm.loop !10

.preheader.preheader.unr-lcssa:                   ; preds = %8, %6
  %indvars.iv.unr = phi i64 [ 0, %6 ], [ %indvars.iv.next.1, %8 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader.preheader, label %.preheader.preheader.epilog-lcssa

.preheader.preheader.epilog-lcssa:                ; preds = %.preheader.preheader.unr-lcssa
  %23 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.unr
  %24 = load i8, ptr %23, align 1, !tbaa !5
  %25 = sext i8 %24 to i64
  %26 = add nsw i64 %25, -97
  %27 = getelementptr inbounds [26 x i32], ptr %2, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !8
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %27, align 4, !tbaa !8
  br label %.preheader.preheader

.preheader.preheader:                             ; preds = %.preheader.preheader.unr-lcssa, %.preheader.preheader.epilog-lcssa
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %37
  %indvars.iv26 = phi i64 [ %indvars.iv.next27, %37 ], [ 0, %.preheader.preheader ]
  %30 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv26
  %31 = load i8, ptr %30, align 1, !tbaa !5
  %32 = sext i8 %31 to i64
  %33 = add nsw i64 %32, -97
  %34 = getelementptr inbounds [26 x i32], ptr %2, i64 0, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !8
  %36 = icmp eq i32 %35, 1
  br i1 %36, label %.split.loop.exit, label %37

37:                                               ; preds = %.preheader
  %indvars.iv.next27 = add nuw i64 %indvars.iv26, 1
  %exitcond29.not = icmp eq i64 %indvars.iv.next27, %4
  br i1 %exitcond29.not, label %.split.loop.exit30, label %.preheader, !llvm.loop !12

.split.loop.exit:                                 ; preds = %.preheader
  %38 = trunc nuw nsw i64 %indvars.iv26 to i32
  br label %.split.loop.exit30

.split.loop.exit30:                               ; preds = %37, %.split.loop.exit
  %spec.select = phi i32 [ %38, %.split.loop.exit ], [ -1, %37 ]
  call void @llvm.lifetime.end.p0(i64 104, ptr nonnull %2) #5
  br label %39

39:                                               ; preds = %.split.loop.exit30, %3, %1
  %.3 = phi i32 [ -1, %1 ], [ %spec.select, %.split.loop.exit30 ], [ -1, %3 ]
  ret i32 %.3
}

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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nofree nounwind memory(argmem: read) 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 = { 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { nounwind willreturn memory(read) }
attributes #5 = { nounwind }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


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