; LLVM IR from nonstriater/Learn-Algorithms :: 9 Algorithms Job Interview/codes/1 string (string)

; 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: 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 signext i8 @first_appear_only_once(ptr noundef %0) #1 {
  %2 = alloca i8, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 256, ptr %4, align 4, !tbaa !9
  %9 = call ptr @llvm.stacksave.p0()
  store ptr %9, ptr %5, align 8
  %10 = alloca i32, i64 256, align 16
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %11

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

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

15:                                               ; preds = %11
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds i32, ptr %10, i64 %17
  store i32 0, ptr %18, align 4, !tbaa !9
  br label %19

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

22:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %23, ptr %7, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %28, %22
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = load i8, ptr %25, align 1, !tbaa !13
  %27 = icmp ne i8 %26, 0
  br i1 %27, label %28, label %36

28:                                               ; preds = %24
  %29 = load ptr, ptr %7, align 8, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %29, i32 1
  store ptr %30, ptr %7, align 8, !tbaa !5
  %31 = load i8, ptr %29, align 1, !tbaa !13
  %32 = sext i8 %31 to i64
  %33 = getelementptr inbounds i32, ptr %10, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %33, align 4, !tbaa !9
  br label %24, !llvm.loop !14

36:                                               ; preds = %24
  br label %37

37:                                               ; preds = %51, %36
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = load i8, ptr %38, align 1, !tbaa !13
  %40 = icmp ne i8 %39, 0
  br i1 %40, label %41, label %54

41:                                               ; preds = %37
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = load i8, ptr %42, align 1, !tbaa !13
  %44 = sext i8 %43 to i64
  %45 = getelementptr inbounds i32, ptr %10, i64 %44
  %46 = load i32, ptr %45, align 4, !tbaa !9
  %47 = icmp eq i32 %46, 1
  br i1 %47, label %48, label %51

48:                                               ; preds = %41
  %49 = load ptr, ptr %3, align 8, !tbaa !5
  %50 = load i8, ptr %49, align 1, !tbaa !13
  store i8 %50, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %60

51:                                               ; preds = %41
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds i8, ptr %52, i32 1
  store ptr %53, ptr %3, align 8, !tbaa !5
  br label %37, !llvm.loop !15

54:                                               ; preds = %37
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = load i8, ptr %55, align 1, !tbaa !13
  %57 = icmp ne i8 %56, 0
  br i1 %57, label %59, label %58

58:                                               ; preds = %54
  store i8 48, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %60

59:                                               ; preds = %54
  store i8 48, ptr %2, align 1
  store i32 1, ptr %8, align 4
  br label %60

60:                                               ; preds = %59, %58, %48
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  %61 = load ptr, ptr %5, align 8
  call void @llvm.stackrestore.p0(ptr %61)
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %62 = load i8, ptr %2, align 1
  ret i8 %62
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

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

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local signext range(i8 1, 0) i8 @first_appear_only_once(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
.preheader18:
  %1 = alloca [256 x i32], align 16
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(1024) %1, i8 0, i64 1024, i1 false), !tbaa !5
  %2 = load i8, ptr %0, align 1, !tbaa !9
  %.not20 = icmp eq i8 %2, 0
  br i1 %.not20, label %._crit_edge, label %.lr.ph

.preheader:                                       ; preds = %.lr.ph
  br i1 %.not20, label %._crit_edge, label %.lr.ph24

.lr.ph:                                           ; preds = %.preheader18, %.lr.ph
  %3 = phi i8 [ %9, %.lr.ph ], [ %2, %.preheader18 ]
  %.021 = phi ptr [ %4, %.lr.ph ], [ %0, %.preheader18 ]
  %4 = getelementptr inbounds i8, ptr %.021, i64 1
  %5 = sext i8 %3 to i64
  %6 = getelementptr inbounds i32, ptr %1, i64 %5
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = add nsw i32 %7, 1
  store i32 %8, ptr %6, align 4, !tbaa !5
  %9 = load i8, ptr %4, align 1, !tbaa !9
  %.not = icmp eq i8 %9, 0
  br i1 %.not, label %.preheader, label %.lr.ph, !llvm.loop !10

10:                                               ; preds = %.lr.ph24
  %11 = getelementptr inbounds i8, ptr %.01423, i64 1
  %12 = load i8, ptr %11, align 1, !tbaa !9
  %.not17 = icmp eq i8 %12, 0
  br i1 %.not17, label %._crit_edge, label %.lr.ph24, !llvm.loop !12

.lr.ph24:                                         ; preds = %.preheader, %10
  %13 = phi i8 [ %12, %10 ], [ %2, %.preheader ]
  %.01423 = phi ptr [ %11, %10 ], [ %0, %.preheader ]
  %14 = sext i8 %13 to i64
  %15 = getelementptr inbounds i32, ptr %1, i64 %14
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = icmp eq i32 %16, 1
  br i1 %17, label %._crit_edge, label %10

._crit_edge:                                      ; preds = %.lr.ph24, %10, %.preheader18, %.preheader
  %.013 = phi i8 [ 48, %.preheader ], [ 48, %.preheader18 ], [ 48, %10 ], [ %13, %.lr.ph24 ]
  ret i8 %.013
}

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

attributes #0 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) 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 nounwind willreturn memory(argmem: write) }

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


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