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

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

9:                                                ; preds = %19, %3
  %10 = load i32, ptr %8, align 4, !tbaa !9
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = srem i32 %11, 8
  %13 = icmp slt i32 %10, %12
  br i1 %13, label %14, label %22

14:                                               ; preds = %9
  %15 = load i8, ptr %7, align 1, !tbaa !11
  %16 = zext i8 %15 to i32
  %17 = ashr i32 %16, 1
  %18 = trunc i32 %17 to i8
  store i8 %18, ptr %7, align 1, !tbaa !11
  br label %19

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

22:                                               ; preds = %9
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %42

25:                                               ; preds = %22
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = sdiv i32 %27, 8
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i8, ptr %26, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !11
  %32 = zext i8 %31 to i32
  %33 = load i8, ptr %7, align 1, !tbaa !11
  %34 = zext i8 %33 to i32
  %35 = or i32 %32, %34
  %36 = trunc i32 %35 to i8
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = load i32, ptr %5, align 4, !tbaa !9
  %39 = sdiv i32 %38, 8
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %37, i64 %40
  store i8 %36, ptr %41, align 1, !tbaa !11
  br label %60

42:                                               ; preds = %22
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = load i32, ptr %5, align 4, !tbaa !9
  %45 = sdiv i32 %44, 8
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i8, ptr %43, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !11
  %49 = zext i8 %48 to i32
  %50 = load i8, ptr %7, align 1, !tbaa !11
  %51 = zext i8 %50 to i32
  %52 = xor i32 %51, -1
  %53 = and i32 %49, %52
  %54 = trunc i32 %53 to i8
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = load i32, ptr %5, align 4, !tbaa !9
  %57 = sdiv i32 %56, 8
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds i8, ptr %55, i64 %58
  store i8 %54, ptr %59, align 1, !tbaa !11
  br label %60

60:                                               ; preds = %42, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #2
  ret void
}

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 = { 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 = distinct !{!12, !13}
!13 = !{!"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"

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @bit_set(ptr nocapture noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = srem i32 %1, 8
  %5 = sdiv i32 %1, 8
  %6 = icmp sgt i32 %4, 0
  br i1 %6, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3
  switch i32 %4, label %.lr.ph.6 [
    i32 1, label %._crit_edge
    i32 2, label %._crit_edge.loopexit.fold.split
    i32 3, label %._crit_edge.loopexit.fold.split17
    i32 4, label %._crit_edge.loopexit.fold.split18
    i32 5, label %._crit_edge.loopexit.fold.split19
    i32 6, label %._crit_edge.loopexit.fold.split20
  ]

.lr.ph.6:                                         ; preds = %.lr.ph
  br label %._crit_edge

._crit_edge.loopexit.fold.split:                  ; preds = %.lr.ph
  br label %._crit_edge

._crit_edge.loopexit.fold.split17:                ; preds = %.lr.ph
  br label %._crit_edge

._crit_edge.loopexit.fold.split18:                ; preds = %.lr.ph
  br label %._crit_edge

._crit_edge.loopexit.fold.split19:                ; preds = %.lr.ph
  br label %._crit_edge

._crit_edge.loopexit.fold.split20:                ; preds = %.lr.ph
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.6, %._crit_edge.loopexit.fold.split, %._crit_edge.loopexit.fold.split17, %._crit_edge.loopexit.fold.split18, %._crit_edge.loopexit.fold.split19, %._crit_edge.loopexit.fold.split20, %.lr.ph, %3
  %.014.lcssa = phi i8 [ -128, %3 ], [ 64, %.lr.ph ], [ 1, %.lr.ph.6 ], [ 32, %._crit_edge.loopexit.fold.split ], [ 16, %._crit_edge.loopexit.fold.split17 ], [ 8, %._crit_edge.loopexit.fold.split18 ], [ 4, %._crit_edge.loopexit.fold.split19 ], [ 2, %._crit_edge.loopexit.fold.split20 ]
  %.not = icmp eq i32 %2, 0
  %7 = sext i32 %5 to i64
  %8 = getelementptr inbounds i8, ptr %0, i64 %7
  %9 = load i8, ptr %8, align 1, !tbaa !5
  %10 = xor i8 %.014.lcssa, -1
  %11 = and i8 %9, %10
  %12 = or i8 %9, %.014.lcssa
  %.sink = select i1 %.not, i8 %11, i8 %12
  store i8 %.sink, ptr %8, align 1, !tbaa !5
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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" }

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


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