; LLVM IR from AllenDowney/ExercisesInC :: exercises/ex05 (find_track_oo)

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

@tracks = external dso_local global [5 x [80 x i8]], align 16
@.str = external hidden unnamed_addr constant [16 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @make_regex(ptr noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @regex_match(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @regex_free(ptr noundef) #0

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

7:                                                ; preds = %26, %1
  %8 = load i32, ptr %3, align 4, !tbaa !9
  %9 = icmp slt i32 %8, 5
  br i1 %9, label %10, label %29

10:                                               ; preds = %7
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = load i32, ptr %3, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [5 x [80 x i8]], ptr @tracks, i64 0, i64 %13
  %15 = getelementptr inbounds [80 x i8], ptr %14, i64 0, i64 0
  %16 = call i32 @regex_match(ptr noundef %11, ptr noundef %15)
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %25

18:                                               ; preds = %10
  %19 = load i32, ptr %3, align 4, !tbaa !9
  %20 = load i32, ptr %3, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [5 x [80 x i8]], ptr @tracks, i64 0, i64 %21
  %23 = getelementptr inbounds [80 x i8], ptr %22, i64 0, i64 0
  %24 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %19, ptr noundef %23)
  br label %25

25:                                               ; preds = %18, %10
  br label %26

26:                                               ; preds = %25
  %27 = load i32, ptr %3, align 4, !tbaa !9
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %3, align 4, !tbaa !9
  br label %7, !llvm.loop !11

29:                                               ; preds = %7
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  call void @regex_free(ptr noundef %30)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret void
}

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

declare i32 @printf(ptr noundef, ...) #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 = { "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 = { 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"}


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

@tracks = external dso_local global [5 x [80 x i8]], align 16
@.str = external hidden unnamed_addr constant [16 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @make_regex(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @regex_match(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @regex_free(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @find_track_regex(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call ptr @make_regex(ptr noundef %0, i32 noundef 9)
  %3 = tail call i32 @regex_match(ptr noundef %2, ptr noundef nonnull @tracks)
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %6, label %4

4:                                                ; preds = %1
  %5 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef 0, ptr noundef nonnull @tracks)
  br label %6

6:                                                ; preds = %1, %4
  %7 = tail call i32 @regex_match(ptr noundef %2, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 80))
  %.not.1 = icmp eq i32 %7, 0
  br i1 %.not.1, label %10, label %8

8:                                                ; preds = %6
  %9 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef 1, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 80))
  br label %10

10:                                               ; preds = %8, %6
  %11 = tail call i32 @regex_match(ptr noundef %2, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 160))
  %.not.2 = icmp eq i32 %11, 0
  br i1 %.not.2, label %14, label %12

12:                                               ; preds = %10
  %13 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef 2, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 160))
  br label %14

14:                                               ; preds = %12, %10
  %15 = tail call i32 @regex_match(ptr noundef %2, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 240))
  %.not.3 = icmp eq i32 %15, 0
  br i1 %.not.3, label %18, label %16

16:                                               ; preds = %14
  %17 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef 3, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 240))
  br label %18

18:                                               ; preds = %16, %14
  %19 = tail call i32 @regex_match(ptr noundef %2, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 320))
  %.not.4 = icmp eq i32 %19, 0
  br i1 %.not.4, label %22, label %20

20:                                               ; preds = %18
  %21 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef 4, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 320))
  br label %22

22:                                               ; preds = %20, %18
  tail call void @regex_free(ptr noundef %2)
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #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 = { 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" }

!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)"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,SROAPass,SimplifyCFGPass,TailCallElimPass