; LLVM IR from erwincoumans/tiny-differentiable-simulator :: third_party/stb_image/stb_image_write.ll/Z11stbi (crc32Phi)

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

@_ZZ11stbi__crc32PhiE9crc_table = external hidden global [256 x i32], align 16

; 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: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z11stbi__crc32Phi(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 -1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %8 = load i32, ptr getelementptr inbounds ([256 x i32], ptr @_ZZ11stbi__crc32PhiE9crc_table, i64 0, i64 1), align 4, !tbaa !9
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %10, label %48

10:                                               ; preds = %2
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %11

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

14:                                               ; preds = %11
  %15 = load i32, ptr %6, align 4, !tbaa !9
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [256 x i32], ptr @_ZZ11stbi__crc32PhiE9crc_table, i64 0, i64 %17
  store i32 %15, ptr %18, align 4, !tbaa !9
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %40, %14
  %20 = load i32, ptr %7, align 4, !tbaa !9
  %21 = icmp slt i32 %20, 8
  br i1 %21, label %22, label %43

22:                                               ; preds = %19
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [256 x i32], ptr @_ZZ11stbi__crc32PhiE9crc_table, i64 0, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = lshr i32 %26, 1
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [256 x i32], ptr @_ZZ11stbi__crc32PhiE9crc_table, i64 0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = and i32 %31, 1
  %33 = icmp ne i32 %32, 0
  %34 = zext i1 %33 to i64
  %35 = select i1 %33, i32 -306674912, i32 0
  %36 = xor i32 %27, %35
  %37 = load i32, ptr %6, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [256 x i32], ptr @_ZZ11stbi__crc32PhiE9crc_table, i64 0, i64 %38
  store i32 %36, ptr %39, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %22
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %7, align 4, !tbaa !9
  br label %19, !llvm.loop !11

43:                                               ; preds = %19
  br label %44

44:                                               ; preds = %43
  %45 = load i32, ptr %6, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %6, align 4, !tbaa !9
  br label %11, !llvm.loop !13

47:                                               ; preds = %11
  br label %48

48:                                               ; preds = %47, %2
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %49

49:                                               ; preds = %69, %48
  %50 = load i32, ptr %6, align 4, !tbaa !9
  %51 = load i32, ptr %4, align 4, !tbaa !9
  %52 = icmp slt i32 %50, %51
  br i1 %52, label %53, label %72

53:                                               ; preds = %49
  %54 = load i32, ptr %5, align 4, !tbaa !9
  %55 = lshr i32 %54, 8
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds i8, ptr %56, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !14
  %61 = zext i8 %60 to i32
  %62 = load i32, ptr %5, align 4, !tbaa !9
  %63 = and i32 %62, 255
  %64 = xor i32 %61, %63
  %65 = zext i32 %64 to i64
  %66 = getelementptr inbounds [256 x i32], ptr @_ZZ11stbi__crc32PhiE9crc_table, i64 0, i64 %65
  %67 = load i32, ptr %66, align 4, !tbaa !9
  %68 = xor i32 %55, %67
  store i32 %68, ptr %5, align 4, !tbaa !9
  br label %69

69:                                               ; preds = %53
  %70 = load i32, ptr %6, align 4, !tbaa !9
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %6, align 4, !tbaa !9
  br label %49, !llvm.loop !15

72:                                               ; preds = %49
  %73 = load i32, ptr %5, align 4, !tbaa !9
  %74 = xor i32 %73, -1
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %74
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = !{!7, !7, i64 0}
!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"

@_ZZ11stbi__crc32PhiE9crc_table = external hidden local_unnamed_addr global [256 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z11stbi__crc32Phi(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = load i32, ptr getelementptr inbounds (i8, ptr @_ZZ11stbi__crc32PhiE9crc_table, i64 4), align 4, !tbaa !5
  %4 = icmp eq i32 %3, 0
  br i1 %4, label %vector.body, label %.loopexit

vector.body:                                      ; preds = %2, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %2 ]
  %vec.ind = phi <4 x i32> [ %vec.ind.next, %vector.body ], [ <i32 0, i32 1, i32 2, i32 3>, %2 ]
  %5 = getelementptr inbounds [256 x i32], ptr @_ZZ11stbi__crc32PhiE9crc_table, i64 0, i64 %index
  %6 = lshr <4 x i32> %vec.ind, <i32 1, i32 1, i32 1, i32 1>
  %7 = and <4 x i32> %vec.ind, <i32 1, i32 1, i32 1, i32 1>
  %8 = icmp eq <4 x i32> %7, zeroinitializer
  %9 = select <4 x i1> %8, <4 x i32> zeroinitializer, <4 x i32> <i32 -306674912, i32 -306674912, i32 -306674912, i32 -306674912>
  %10 = xor <4 x i32> %9, %6
  %11 = lshr <4 x i32> %10, <i32 1, i32 1, i32 1, i32 1>
  %12 = and <4 x i32> %vec.ind, <i32 2, i32 2, i32 2, i32 2>
  %13 = icmp eq <4 x i32> %12, zeroinitializer
  %14 = select <4 x i1> %13, <4 x i32> zeroinitializer, <4 x i32> <i32 -306674912, i32 -306674912, i32 -306674912, i32 -306674912>
  %15 = xor <4 x i32> %14, %11
  %16 = lshr <4 x i32> %15, <i32 6, i32 6, i32 6, i32 6>
  %17 = and <4 x i32> %vec.ind, <i32 4, i32 4, i32 4, i32 4>
  %18 = icmp eq <4 x i32> %17, zeroinitializer
  %19 = select <4 x i1> %18, <4 x i32> zeroinitializer, <4 x i32> <i32 124634137, i32 124634137, i32 124634137, i32 124634137>
  %20 = xor <4 x i32> %19, %16
  %21 = and <4 x i32> %vec.ind, <i32 8, i32 8, i32 8, i32 8>
  %22 = icmp eq <4 x i32> %21, zeroinitializer
  %23 = select <4 x i1> %22, <4 x i32> zeroinitializer, <4 x i32> <i32 249268274, i32 249268274, i32 249268274, i32 249268274>
  %24 = xor <4 x i32> %23, %20
  %25 = and <4 x i32> %vec.ind, <i32 16, i32 16, i32 16, i32 16>
  %26 = icmp eq <4 x i32> %25, zeroinitializer
  %27 = select <4 x i1> %26, <4 x i32> zeroinitializer, <4 x i32> <i32 498536548, i32 498536548, i32 498536548, i32 498536548>
  %28 = xor <4 x i32> %27, %24
  %29 = and <4 x i32> %vec.ind, <i32 32, i32 32, i32 32, i32 32>
  %30 = icmp eq <4 x i32> %29, zeroinitializer
  %31 = select <4 x i1> %30, <4 x i32> zeroinitializer, <4 x i32> <i32 997073096, i32 997073096, i32 997073096, i32 997073096>
  %32 = xor <4 x i32> %31, %28
  %33 = and <4 x i32> %10, <i32 32, i32 32, i32 32, i32 32>
  %34 = icmp eq <4 x i32> %33, zeroinitializer
  %35 = select <4 x i1> %34, <4 x i32> zeroinitializer, <4 x i32> <i32 1994146192, i32 1994146192, i32 1994146192, i32 1994146192>
  %36 = xor <4 x i32> %35, %32
  %37 = and <4 x i32> %15, <i32 32, i32 32, i32 32, i32 32>
  %38 = icmp eq <4 x i32> %37, zeroinitializer
  %39 = select <4 x i1> %38, <4 x i32> zeroinitializer, <4 x i32> <i32 -306674912, i32 -306674912, i32 -306674912, i32 -306674912>
  %40 = xor <4 x i32> %39, %36
  store <4 x i32> %40, ptr %5, align 16, !tbaa !5
  %index.next = add nuw i64 %index, 4
  %vec.ind.next = add <4 x i32> %vec.ind, <i32 4, i32 4, i32 4, i32 4>
  %41 = icmp eq i64 %index.next, 256
  br i1 %41, label %.loopexit, label %vector.body, !llvm.loop !9

.loopexit:                                        ; preds = %vector.body, %2
  %42 = icmp sgt i32 %1, 0
  br i1 %42, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.loopexit
  %wide.trip.count = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %43 = icmp eq i32 %1, 1
  br i1 %43, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv24 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next25.1, %.lr.ph ]
  %.01720 = phi i32 [ -1, %.lr.ph.preheader.new ], [ %63, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %44 = lshr i32 %.01720, 8
  %45 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv24
  %46 = load i8, ptr %45, align 1, !tbaa !13
  %47 = zext i8 %46 to i32
  %48 = and i32 %.01720, 255
  %49 = xor i32 %48, %47
  %50 = zext nneg i32 %49 to i64
  %51 = getelementptr inbounds [256 x i32], ptr @_ZZ11stbi__crc32PhiE9crc_table, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !5
  %53 = xor i32 %52, %44
  %indvars.iv.next25 = or disjoint i64 %indvars.iv24, 1
  %54 = lshr i32 %53, 8
  %55 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next25
  %56 = load i8, ptr %55, align 1, !tbaa !13
  %57 = zext i8 %56 to i32
  %58 = and i32 %53, 255
  %59 = xor i32 %58, %57
  %60 = zext nneg i32 %59 to i64
  %61 = getelementptr inbounds [256 x i32], ptr @_ZZ11stbi__crc32PhiE9crc_table, i64 0, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !5
  %63 = xor i32 %62, %54
  %indvars.iv.next25.1 = add nuw nsw i64 %indvars.iv24, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !14

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %63, %.lr.ph ]
  %indvars.iv24.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next25.1, %.lr.ph ]
  %.01720.unr = phi i32 [ -1, %.lr.ph.preheader ], [ %63, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %64 = lshr i32 %.01720.unr, 8
  %65 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv24.unr
  %66 = load i8, ptr %65, align 1, !tbaa !13
  %67 = zext i8 %66 to i32
  %68 = and i32 %.01720.unr, 255
  %69 = xor i32 %68, %67
  %70 = zext nneg i32 %69 to i64
  %71 = getelementptr inbounds [256 x i32], ptr @_ZZ11stbi__crc32PhiE9crc_table, i64 0, i64 %70
  %72 = load i32, ptr %71, align 4, !tbaa !5
  %73 = xor i32 %72, %64
  br label %._crit_edge.loopexit

._crit_edge.loopexit:                             ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %.lcssa = phi i32 [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %73, %.lr.ph.epil ]
  %74 = xor i32 %.lcssa, -1
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.loopexit
  %.017.lcssa = phi i32 [ 0, %.loopexit ], [ %74, %._crit_edge.loopexit ]
  ret i32 %.017.lcssa
}

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

!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++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !10}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass