; LLVM IR from yourtion/LearningMasteringAlgorithms-C :: source (chtbl)

; 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.CHTbl_ = type { i32, ptr, ptr, ptr, i32, ptr }
%struct.List_ = type { i32, ptr, ptr, ptr, ptr }
%struct.ListElmt_ = type { ptr, ptr }

; 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 i32 @chtbl_remove(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.CHTbl_, ptr %10, i32 0, i32 1
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = load ptr, ptr %13, align 8, !tbaa !5
  %15 = call i32 %12(ptr noundef %14)
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.CHTbl_, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 8, !tbaa !12
  %19 = srem i32 %15, %18
  store i32 %19, ptr %8, align 4, !tbaa !13
  store ptr null, ptr %7, align 8, !tbaa !5
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.CHTbl_, ptr %20, i32 0, i32 5
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  %23 = load i32, ptr %8, align 4, !tbaa !13
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds %struct.List_, ptr %22, i64 %24
  %26 = getelementptr inbounds %struct.List_, ptr %25, i32 0, i32 3
  %27 = load ptr, ptr %26, align 8, !tbaa !15
  store ptr %27, ptr %6, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %61, %2
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = icmp ne ptr %29, null
  br i1 %30, label %31, label %65

31:                                               ; preds = %28
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.CHTbl_, ptr %32, i32 0, i32 2
  %34 = load ptr, ptr %33, align 8, !tbaa !17
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = load ptr, ptr %35, align 8, !tbaa !5
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.ListElmt_, ptr %37, i32 0, i32 0
  %39 = load ptr, ptr %38, align 8, !tbaa !18
  %40 = call i32 %34(ptr noundef %36, ptr noundef %39)
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %42, label %59

42:                                               ; preds = %31
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.CHTbl_, ptr %43, i32 0, i32 5
  %45 = load ptr, ptr %44, align 8, !tbaa !14
  %46 = load i32, ptr %8, align 4, !tbaa !13
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds %struct.List_, ptr %45, i64 %47
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = call i32 @list_rem_next(ptr noundef %48, ptr noundef %49, ptr noundef %50)
  %52 = icmp ne i32 %51, 0
  br i1 %52, label %53, label %54

53:                                               ; preds = %42
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %66

54:                                               ; preds = %42
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.CHTbl_, ptr %55, i32 0, i32 4
  %57 = load i32, ptr %56, align 8, !tbaa !20
  %58 = add nsw i32 %57, -1
  store i32 %58, ptr %56, align 8, !tbaa !20
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %66

59:                                               ; preds = %31
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %60, ptr %7, align 8, !tbaa !5
  br label %61

61:                                               ; preds = %59
  %62 = load ptr, ptr %6, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.ListElmt_, ptr %62, i32 0, i32 1
  %64 = load ptr, ptr %63, align 8, !tbaa !21
  store ptr %64, ptr %6, align 8, !tbaa !5
  br label %28, !llvm.loop !22

65:                                               ; preds = %28
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %66

66:                                               ; preds = %65, %54, %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %67 = load i32, ptr %3, align 4
  ret i32 %67
}

declare i32 @list_rem_next(ptr noundef, ptr noundef, ptr noundef) #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 = { "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, !6, i64 8}
!10 = !{!"CHTbl_", !11, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !6, i64 40}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !6, i64 40}
!15 = !{!16, !6, i64 24}
!16 = !{!"List_", !11, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!17 = !{!10, !6, i64 16}
!18 = !{!19, !6, i64 0}
!19 = !{!"ListElmt_", !6, i64 0, !6, i64 8}
!20 = !{!10, !11, i64 32}
!21 = !{!19, !6, i64 8}
!22 = distinct !{!22, !23}
!23 = !{!"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.List_ = type { i32, ptr, ptr, ptr, ptr }

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @chtbl_remove(ptr nocapture noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = load ptr, ptr %1, align 8, !tbaa !11
  %6 = tail call i32 %4(ptr noundef %5) #2
  %7 = load i32, ptr %0, align 8, !tbaa !12
  %8 = srem i32 %6, %7
  %9 = getelementptr inbounds i8, ptr %0, i64 40
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  %11 = sext i32 %8 to i64
  %12 = getelementptr inbounds %struct.List_, ptr %10, i64 %11, i32 3
  %.01721 = load ptr, ptr %12, align 8, !tbaa !11
  %.not22 = icmp eq ptr %.01721, null
  br i1 %.not22, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %13 = getelementptr inbounds i8, ptr %0, i64 16
  %14 = load ptr, ptr %13, align 8, !tbaa !14
  %15 = load ptr, ptr %1, align 8, !tbaa !11
  %16 = load ptr, ptr %.01721, align 8, !tbaa !15
  %17 = tail call i32 %14(ptr noundef %15, ptr noundef %16) #2
  %.not1828 = icmp eq i32 %17, 0
  br i1 %.not1828, label %.lr.ph30, label %._crit_edge

.lr.ph30:                                         ; preds = %.lr.ph, %19
  %.0172429 = phi ptr [ %.017, %19 ], [ %.01721, %.lr.ph ]
  %18 = getelementptr inbounds i8, ptr %.0172429, i64 8
  %.017 = load ptr, ptr %18, align 8, !tbaa !11
  %.not = icmp eq ptr %.017, null
  br i1 %.not, label %.loopexit, label %19, !llvm.loop !17

19:                                               ; preds = %.lr.ph30
  %20 = load ptr, ptr %13, align 8, !tbaa !14
  %21 = load ptr, ptr %1, align 8, !tbaa !11
  %22 = load ptr, ptr %.017, align 8, !tbaa !15
  %23 = tail call i32 %20(ptr noundef %21, ptr noundef %22) #2
  %.not18 = icmp eq i32 %23, 0
  br i1 %.not18, label %.lr.ph30, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %19, %.lr.ph
  %.01623.lcssa = phi ptr [ null, %.lr.ph ], [ %.0172429, %19 ]
  %24 = load ptr, ptr %9, align 8, !tbaa !13
  %25 = getelementptr inbounds %struct.List_, ptr %24, i64 %11
  %26 = tail call i32 @list_rem_next(ptr noundef %25, ptr noundef %.01623.lcssa, ptr noundef nonnull %1) #2
  %.not19 = icmp eq i32 %26, 0
  br i1 %.not19, label %27, label %.loopexit

27:                                               ; preds = %._crit_edge
  %28 = getelementptr inbounds i8, ptr %0, i64 32
  %29 = load i32, ptr %28, align 8, !tbaa !19
  %30 = add nsw i32 %29, -1
  store i32 %30, ptr %28, align 8, !tbaa !19
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph30, %2, %._crit_edge, %27
  %.0 = phi i32 [ 0, %27 ], [ -1, %._crit_edge ], [ -1, %2 ], [ -1, %.lr.ph30 ]
  ret i32 %.0
}

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

!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, !10, i64 8}
!6 = !{!"CHTbl_", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !7, i64 32, !10, i64 40}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!10, !10, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !10, i64 40}
!14 = !{!6, !10, i64 16}
!15 = !{!16, !10, i64 0}
!16 = !{!"ListElmt_", !10, i64 0, !10, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!6, !7, i64 32}


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