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

; 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.re_pattern_buffer = type { ptr, i64, i64, i64, ptr, ptr, i64, i8 }

@tracks = external dso_local global [5 x [80 x i8]], align 16
@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [25 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [24 x i8], align 1

; 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 %struct.re_pattern_buffer, align 8
  %5 = alloca i32, align 4
  %6 = alloca [100 x i8], align 16
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  call void @llvm.lifetime.start.p0(i64 64, ptr %4) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.start.p0(i64 100, ptr %6) #5
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = call i32 @regcomp(ptr noundef %4, ptr noundef %7, i32 noundef 9)
  store i32 %8, ptr %5, align 4, !tbaa !9
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %11, label %14

11:                                               ; preds = %1
  %12 = load ptr, ptr @stderr, align 8, !tbaa !5
  %13 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %12, ptr noundef @.str) #5
  call void @exit(i32 noundef 1) #6
  unreachable

14:                                               ; preds = %1
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %45, %14
  %16 = load i32, ptr %3, align 4, !tbaa !9
  %17 = icmp slt i32 %16, 5
  br i1 %17, label %18, label %48

18:                                               ; preds = %15
  %19 = load i32, ptr %3, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [5 x [80 x i8]], ptr @tracks, i64 0, i64 %20
  %22 = getelementptr inbounds [80 x i8], ptr %21, i64 0, i64 0
  %23 = call i32 @regexec(ptr noundef %4, ptr noundef %22, i64 noundef 0, ptr noundef null, i32 noundef 0)
  store i32 %23, ptr %5, align 4, !tbaa !9
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %33, label %26

26:                                               ; preds = %18
  %27 = load i32, ptr %3, align 4, !tbaa !9
  %28 = load i32, ptr %3, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [5 x [80 x i8]], ptr @tracks, i64 0, i64 %29
  %31 = getelementptr inbounds [80 x i8], ptr %30, i64 0, i64 0
  %32 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %27, ptr noundef %31)
  br label %44

33:                                               ; preds = %18
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = icmp eq i32 %34, 1
  br i1 %35, label %36, label %37

36:                                               ; preds = %33
  br label %45

37:                                               ; preds = %33
  %38 = load i32, ptr %5, align 4, !tbaa !9
  %39 = getelementptr inbounds [100 x i8], ptr %6, i64 0, i64 0
  %40 = call i64 @regerror(i32 noundef %38, ptr noundef %4, ptr noundef %39, i64 noundef 100)
  %41 = load ptr, ptr @stderr, align 8, !tbaa !5
  %42 = getelementptr inbounds [100 x i8], ptr %6, i64 0, i64 0
  %43 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %41, ptr noundef @.str.2, ptr noundef %42) #5
  call void @exit(i32 noundef 1) #6
  unreachable

44:                                               ; preds = %26
  br label %45

45:                                               ; preds = %44, %36
  %46 = load i32, ptr %3, align 4, !tbaa !9
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %3, align 4, !tbaa !9
  br label %15, !llvm.loop !11

48:                                               ; preds = %15
  call void @regfree(ptr noundef %4)
  call void @llvm.lifetime.end.p0(i64 100, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  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 @regcomp(ptr noundef, ptr noundef, i32 noundef) #2

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #3

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #4

declare i32 @regexec(ptr noundef, ptr noundef, i64 noundef, ptr noundef, i32 noundef) #2

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

declare i64 @regerror(i32 noundef, ptr noundef, ptr noundef, i64 noundef) #2

declare void @regfree(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 "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 = { noreturn 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 #5 = { nounwind }
attributes #6 = { noreturn 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"

%struct.re_pattern_buffer = type { ptr, i64, i64, i64, ptr, ptr, i64, i8 }

@tracks = external dso_local global [5 x [80 x i8]], align 16
@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [25 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [16 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [24 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @find_track_regex(ptr noundef %0) local_unnamed_addr #0 {
  %2 = alloca %struct.re_pattern_buffer, align 8
  %3 = alloca [100 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %2) #5
  call void @llvm.lifetime.start.p0(i64 100, ptr nonnull %3) #5
  %4 = call i32 @regcomp(ptr noundef nonnull %2, ptr noundef %0, i32 noundef 9) #5
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %.preheader.preheader, label %6

.preheader.preheader:                             ; preds = %1
  %5 = call i32 @regexec(ptr noundef nonnull %2, ptr noundef nonnull @tracks, i64 noundef 0, ptr noundef null, i32 noundef 0) #5
  switch i32 %5, label %11 [
    i32 0, label %9
    i32 1, label %.preheader.1
  ]

6:                                                ; preds = %1
  %7 = load ptr, ptr @stderr, align 8, !tbaa !5
  %8 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %7, ptr noundef nonnull @.str) #6
  call void @exit(i32 noundef 1) #7
  unreachable

9:                                                ; preds = %.preheader.preheader
  %10 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef 0, ptr noundef nonnull @tracks)
  br label %.preheader.1

11:                                               ; preds = %.preheader.4, %.preheader.3, %.preheader.2, %.preheader.1, %.preheader.preheader
  %.lcssa = phi i32 [ %5, %.preheader.preheader ], [ %15, %.preheader.1 ], [ %18, %.preheader.2 ], [ %21, %.preheader.3 ], [ %24, %.preheader.4 ]
  %12 = call i64 @regerror(i32 noundef %.lcssa, ptr noundef nonnull %2, ptr noundef nonnull %3, i64 noundef 100) #5
  %13 = load ptr, ptr @stderr, align 8, !tbaa !5
  %14 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %13, ptr noundef nonnull @.str.2, ptr noundef nonnull %3) #6
  call void @exit(i32 noundef 1) #7
  unreachable

.preheader.1:                                     ; preds = %.preheader.preheader, %9
  %15 = call i32 @regexec(ptr noundef nonnull %2, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 80), i64 noundef 0, ptr noundef null, i32 noundef 0) #5
  switch i32 %15, label %11 [
    i32 0, label %16
    i32 1, label %.preheader.2
  ]

16:                                               ; preds = %.preheader.1
  %17 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef 1, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 80))
  br label %.preheader.2

.preheader.2:                                     ; preds = %16, %.preheader.1
  %18 = call i32 @regexec(ptr noundef nonnull %2, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 160), i64 noundef 0, ptr noundef null, i32 noundef 0) #5
  switch i32 %18, label %11 [
    i32 0, label %19
    i32 1, label %.preheader.3
  ]

19:                                               ; preds = %.preheader.2
  %20 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef 2, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 160))
  br label %.preheader.3

.preheader.3:                                     ; preds = %19, %.preheader.2
  %21 = call i32 @regexec(ptr noundef nonnull %2, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 240), i64 noundef 0, ptr noundef null, i32 noundef 0) #5
  switch i32 %21, label %11 [
    i32 0, label %22
    i32 1, label %.preheader.4
  ]

22:                                               ; preds = %.preheader.3
  %23 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef 3, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 240))
  br label %.preheader.4

.preheader.4:                                     ; preds = %22, %.preheader.3
  %24 = call i32 @regexec(ptr noundef nonnull %2, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 320), i64 noundef 0, ptr noundef null, i32 noundef 0) #5
  switch i32 %24, label %11 [
    i32 0, label %25
    i32 1, label %27
  ]

25:                                               ; preds = %.preheader.4
  %26 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef 4, ptr noundef nonnull getelementptr inbounds (i8, ptr @tracks, i64 320))
  br label %27

27:                                               ; preds = %25, %.preheader.4
  call void @regfree(ptr noundef nonnull %2) #5
  call void @llvm.lifetime.end.p0(i64 100, ptr nonnull %3) #5
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %2) #5
  ret void
}

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

declare i32 @regcomp(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #4

declare i32 @regexec(ptr noundef, ptr noundef, i64 noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

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

declare i64 @regerror(i32 noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

declare void @regfree(ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress 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 = { mustprogress 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 = { 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 #4 = { noreturn 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 #5 = { nounwind }
attributes #6 = { cold nounwind }
attributes #7 = { noreturn 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"}


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