; LLVM IR from HarshCasper/NeoAlgo :: C/other (leftmost_nonrepeating_char)

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

%struct.pair = type { i32, i32 }

; Function Attrs: nounwind uwtable
define dso_local i32 @find_char_index(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca [256 x %struct.pair], align 16
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2048, ptr %3) #3
  call void @llvm.memset.p0.i64(ptr align 16 %3, i8 0, i64 2048, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %7

7:                                                ; preds = %35, %1
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = load i32, ptr %4, align 4, !tbaa !9
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds i8, ptr %8, i64 %10
  %12 = load i8, ptr %11, align 1, !tbaa !11
  %13 = icmp ne i8 %12, 0
  br i1 %13, label %15, label %14

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

15:                                               ; preds = %7
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = load i32, ptr %4, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i8, ptr %16, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !11
  %21 = sext i8 %20 to i64
  %22 = getelementptr inbounds [256 x %struct.pair], ptr %3, i64 0, i64 %21
  %23 = getelementptr inbounds %struct.pair, ptr %22, i32 0, i32 0
  %24 = load i32, ptr %23, align 8, !tbaa !12
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %23, align 8, !tbaa !12
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = load i32, ptr %4, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i8, ptr %27, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !11
  %32 = sext i8 %31 to i64
  %33 = getelementptr inbounds [256 x %struct.pair], ptr %3, i64 0, i64 %32
  %34 = getelementptr inbounds %struct.pair, ptr %33, i32 0, i32 1
  store i32 %26, ptr %34, align 4, !tbaa !14
  br label %35

35:                                               ; preds = %15
  %36 = load i32, ptr %4, align 4, !tbaa !9
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %4, align 4, !tbaa !9
  br label %7, !llvm.loop !15

38:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 10001, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %69, %38
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = icmp slt i32 %40, 256
  br i1 %41, label %43, label %42

42:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %72

43:                                               ; preds = %39
  %44 = load i32, ptr %6, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [256 x %struct.pair], ptr %3, i64 0, i64 %45
  %47 = getelementptr inbounds %struct.pair, ptr %46, i32 0, i32 0
  %48 = load i32, ptr %47, align 8, !tbaa !12
  %49 = icmp eq i32 %48, 1
  br i1 %49, label %50, label %68

50:                                               ; preds = %43
  %51 = load i32, ptr %5, align 4, !tbaa !9
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [256 x %struct.pair], ptr %3, i64 0, i64 %53
  %55 = getelementptr inbounds %struct.pair, ptr %54, i32 0, i32 1
  %56 = load i32, ptr %55, align 4, !tbaa !14
  %57 = icmp sgt i32 %51, %56
  br i1 %57, label %58, label %64

58:                                               ; preds = %50
  %59 = load i32, ptr %6, align 4, !tbaa !9
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [256 x %struct.pair], ptr %3, i64 0, i64 %60
  %62 = getelementptr inbounds %struct.pair, ptr %61, i32 0, i32 1
  %63 = load i32, ptr %62, align 4, !tbaa !14
  br label %66

64:                                               ; preds = %50
  %65 = load i32, ptr %5, align 4, !tbaa !9
  br label %66

66:                                               ; preds = %64, %58
  %67 = phi i32 [ %63, %58 ], [ %65, %64 ]
  store i32 %67, ptr %5, align 4, !tbaa !9
  br label %68

68:                                               ; preds = %66, %43
  br label %69

69:                                               ; preds = %68
  %70 = load i32, ptr %6, align 4, !tbaa !9
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %6, align 4, !tbaa !9
  br label %39, !llvm.loop !17

72:                                               ; preds = %42
  %73 = load i32, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 2048, ptr %3) #3
  ret i32 %73
}

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

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

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 = !{!"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 = !{!7, !7, i64 0}
!12 = !{!13, !10, i64 0}
!13 = !{!"pair", !10, i64 0, !10, i64 4}
!14 = !{!13, !10, i64 4}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


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

%struct.pair = type { i32, i32 }

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local range(i32 -2147483648, 10002) i32 @find_char_index(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = alloca [256 x %struct.pair], align 16
  call void @llvm.lifetime.start.p0(i64 2048, ptr nonnull %2) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(2048) %2, i8 0, i64 2048, i1 false)
  %3 = load i8, ptr %0, align 1, !tbaa !5
  %.not19 = icmp eq i8 %3, 0
  br i1 %.not19, label %.preheader.preheader, label %.lr.ph

.preheader.preheader:                             ; preds = %.lr.ph, %1
  br label %.preheader

.lr.ph:                                           ; preds = %1, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %1 ]
  %4 = phi i8 [ %12, %.lr.ph ], [ %3, %1 ]
  %5 = sext i8 %4 to i64
  %6 = getelementptr inbounds [256 x %struct.pair], ptr %2, i64 0, i64 %5
  %7 = load i32, ptr %6, align 8, !tbaa !8
  %8 = add nsw i32 %7, 1
  store i32 %8, ptr %6, align 8, !tbaa !8
  %9 = getelementptr inbounds [256 x %struct.pair], ptr %2, i64 0, i64 %5, i32 1
  %10 = trunc nuw nsw i64 %indvars.iv to i32
  store i32 %10, ptr %9, align 4, !tbaa !11
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %11 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next
  %12 = load i8, ptr %11, align 1, !tbaa !5
  %.not = icmp eq i8 %12, 0
  br i1 %.not, label %.preheader.preheader, label %.lr.ph, !llvm.loop !12

13:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 2048, ptr nonnull %2) #4
  ret i32 %.1.1

.preheader:                                       ; preds = %26, %.preheader.preheader
  %indvars.iv23 = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next24.1, %26 ]
  %.01521 = phi i32 [ 10001, %.preheader.preheader ], [ %.1.1, %26 ]
  %14 = getelementptr inbounds [256 x %struct.pair], ptr %2, i64 0, i64 %indvars.iv23
  %15 = load i32, ptr %14, align 16, !tbaa !8
  %16 = icmp eq i32 %15, 1
  br i1 %16, label %17, label %.preheader.1

17:                                               ; preds = %.preheader
  %18 = getelementptr inbounds i8, ptr %14, i64 4
  %19 = load i32, ptr %18, align 4, !tbaa !11
  %..015 = tail call i32 @llvm.smin.i32(i32 %.01521, i32 %19)
  br label %.preheader.1

.preheader.1:                                     ; preds = %.preheader, %17
  %.1 = phi i32 [ %..015, %17 ], [ %.01521, %.preheader ]
  %indvars.iv.next24 = or disjoint i64 %indvars.iv23, 1
  %20 = getelementptr inbounds [256 x %struct.pair], ptr %2, i64 0, i64 %indvars.iv.next24
  %21 = load i32, ptr %20, align 8, !tbaa !8
  %22 = icmp eq i32 %21, 1
  br i1 %22, label %23, label %26

23:                                               ; preds = %.preheader.1
  %24 = getelementptr inbounds i8, ptr %20, i64 4
  %25 = load i32, ptr %24, align 4, !tbaa !11
  %..015.1 = tail call i32 @llvm.smin.i32(i32 %.1, i32 %25)
  br label %26

26:                                               ; preds = %23, %.preheader.1
  %.1.1 = phi i32 [ %..015.1, %23 ], [ %.1, %.preheader.1 ]
  %indvars.iv.next24.1 = add nuw nsw i64 %indvars.iv23, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next24.1, 256
  br i1 %exitcond.not.1, label %13, label %.preheader, !llvm.loop !14
}

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

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

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #3

attributes #0 = { nofree norecurse nosync 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 #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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, !10, i64 0}
!9 = !{!"pair", !10, i64 0, !10, i64 4}
!10 = !{!"int", !6, i64 0}
!11 = !{!9, !10, i64 4}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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