; LLVM IR from paintdream/DontStarveLuaJIT :: src/luajit/src (lj_opt_fold)

; 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.jit_State = type { %struct.GCtrace, ptr, ptr, ptr, ptr, ptr, i32, i32, i32, i8, i8, %struct.IRType1, i8, %struct.FoldState, ptr, i32, i32, i32, i32, i32, i32, i32, %struct.MRef, [5 x %union.TValue], ptr, i32, i32, i32, i32, ptr, ptr, i32, i32, i8, ptr, i32, i32, ptr, [100 x i16], [255 x i32], [15 x i32], [16 x ptr], [64 x %struct.HotPenalty], i32, i32, [16 x %struct.BPropEntry], i32, %struct.ScEvEntry, ptr, i32, i32, ptr, i32, i32, ptr, ptr, ptr, i64, i64, %union.TValue, ptr, i32, i32 }
%struct.GCtrace = type { %struct.GCRef, i8, i8, i8, i8, i32, %struct.GCRef, ptr, i32, i16, i16, ptr, ptr, %struct.GCRef, %struct.MRef, i32, i32, ptr, i32, i16, i16, i16, i16, i16, i16, i16, i8, i8 }
%struct.GCRef = type { i32 }
%struct.IRType1 = type { i8 }
%struct.FoldState = type { %union.IRIns, %union.IRIns, %union.IRIns }
%union.IRIns = type { %struct.anon.0 }
%struct.anon.0 = type { i32, %struct.IRType1, i8, i8, i8 }
%struct.MRef = type { i32 }
%struct.HotPenalty = type { %struct.MRef, i16, i16 }
%struct.BPropEntry = type { i16, i16, i32 }
%struct.ScEvEntry = type { %struct.MRef, i16, i16, i16, i16, %struct.IRType1, i8 }
%union.TValue = type { i64 }
%struct.anon = type { i16, i16, i16, i16 }

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

declare hidden i32 @lj_ir_emit(ptr noundef) #1

; 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 hidden i32 @lj_opt_cselim(ptr noundef %0, i32 noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.jit_State, ptr %9, i32 0, i32 38
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.jit_State, ptr %11, i32 0, i32 13
  %13 = getelementptr inbounds %struct.FoldState, ptr %12, i32 0, i32 0
  %14 = getelementptr inbounds %struct.anon.0, ptr %13, i32 0, i32 2
  %15 = load i8, ptr %14, align 1, !tbaa !11
  %16 = zext i8 %15 to i64
  %17 = getelementptr inbounds [100 x i16], ptr %10, i64 0, i64 %16
  %18 = load i16, ptr %17, align 2, !tbaa !12
  %19 = zext i16 %18 to i32
  store i32 %19, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.jit_State, ptr %20, i32 0, i32 13
  %22 = getelementptr inbounds %struct.FoldState, ptr %21, i32 0, i32 0
  %23 = getelementptr inbounds %struct.anon, ptr %22, i32 0, i32 0
  %24 = load i16, ptr %23, align 8, !tbaa !11
  %25 = zext i16 %24 to i32
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.jit_State, ptr %26, i32 0, i32 13
  %28 = getelementptr inbounds %struct.FoldState, ptr %27, i32 0, i32 0
  %29 = getelementptr inbounds %struct.anon, ptr %28, i32 0, i32 1
  %30 = load i16, ptr %29, align 2, !tbaa !11
  %31 = zext i16 %30 to i32
  %32 = shl i32 %31, 16
  %33 = add i32 %25, %32
  store i32 %33, ptr %7, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %52, %2
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = icmp ugt i32 %35, %36
  br i1 %37, label %38, label %63

38:                                               ; preds = %34
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.jit_State, ptr %39, i32 0, i32 0
  %41 = getelementptr inbounds %struct.GCtrace, ptr %40, i32 0, i32 7
  %42 = load ptr, ptr %41, align 8, !tbaa !14
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = zext i32 %43 to i64
  %45 = getelementptr inbounds %union.IRIns, ptr %42, i64 %44
  %46 = getelementptr inbounds %struct.anon.0, ptr %45, i32 0, i32 0
  %47 = load i32, ptr %46, align 4, !tbaa !11
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = icmp eq i32 %47, %48
  br i1 %49, label %50, label %52

50:                                               ; preds = %38
  %51 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %51, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %66

52:                                               ; preds = %38
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.jit_State, ptr %53, i32 0, i32 0
  %55 = getelementptr inbounds %struct.GCtrace, ptr %54, i32 0, i32 7
  %56 = load ptr, ptr %55, align 8, !tbaa !14
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = zext i32 %57 to i64
  %59 = getelementptr inbounds %union.IRIns, ptr %56, i64 %58
  %60 = getelementptr inbounds %struct.anon, ptr %59, i32 0, i32 3
  %61 = load i16, ptr %60, align 2, !tbaa !11
  %62 = zext i16 %61 to i32
  store i32 %62, ptr %6, align 4, !tbaa !9
  br label %34, !llvm.loop !23

63:                                               ; preds = %34
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = call i32 @lj_ir_emit(ptr noundef %64)
  store i32 %65, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %66

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !6, i64 16}
!15 = !{!"jit_State", !16, i64 0, !6, i64 96, !6, i64 104, !6, i64 112, !6, i64 120, !6, i64 128, !10, i64 136, !10, i64 140, !10, i64 144, !7, i64 148, !7, i64 149, !19, i64 150, !7, i64 151, !20, i64 152, !6, i64 176, !10, i64 184, !10, i64 188, !10, i64 192, !10, i64 196, !10, i64 200, !10, i64 204, !10, i64 208, !18, i64 212, !7, i64 216, !6, i64 256, !10, i64 264, !10, i64 268, !10, i64 272, !10, i64 276, !6, i64 280, !6, i64 288, !10, i64 296, !10, i64 300, !7, i64 304, !6, i64 312, !10, i64 320, !10, i64 324, !6, i64 328, !7, i64 336, !7, i64 536, !7, i64 1556, !7, i64 1616, !7, i64 1744, !10, i64 2256, !10, i64 2260, !7, i64 2264, !10, i64 2392, !21, i64 2396, !6, i64 2416, !10, i64 2424, !10, i64 2428, !6, i64 2432, !10, i64 2440, !10, i64 2444, !6, i64 2448, !6, i64 2456, !6, i64 2464, !22, i64 2472, !22, i64 2480, !7, i64 2488, !6, i64 2496, !10, i64 2504, !10, i64 2508}
!16 = !{!"GCtrace", !17, i64 0, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !10, i64 8, !17, i64 12, !6, i64 16, !10, i64 24, !13, i64 28, !13, i64 30, !6, i64 32, !6, i64 40, !17, i64 48, !18, i64 52, !10, i64 56, !10, i64 60, !6, i64 64, !10, i64 72, !13, i64 76, !13, i64 78, !13, i64 80, !13, i64 82, !13, i64 84, !13, i64 86, !13, i64 88, !7, i64 90, !7, i64 91}
!17 = !{!"GCRef", !10, i64 0}
!18 = !{!"MRef", !10, i64 0}
!19 = !{!"IRType1", !7, i64 0}
!20 = !{!"FoldState", !7, i64 0, !7, i64 8, !7, i64 16}
!21 = !{!"ScEvEntry", !18, i64 0, !13, i64 4, !13, i64 6, !13, i64 8, !13, i64 10, !19, i64 12, !7, i64 13}
!22 = !{!"long", !7, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"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"

%union.IRIns = type { %struct.anon.0 }
%struct.anon.0 = type { i32, %struct.IRType1, i8, i8, i8 }
%struct.IRType1 = type { i8 }

declare hidden i32 @lj_ir_emit(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define hidden i32 @lj_opt_cselim(ptr noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = getelementptr inbounds i8, ptr %0, i64 336
  %4 = getelementptr inbounds i8, ptr %0, i64 152
  %5 = getelementptr inbounds i8, ptr %0, i64 157
  %6 = load i8, ptr %5, align 1, !tbaa !5
  %7 = zext i8 %6 to i64
  %8 = getelementptr inbounds [100 x i16], ptr %3, i64 0, i64 %7
  %9 = load i32, ptr %4, align 8
  %.013.in15 = load i16, ptr %8, align 2, !tbaa !5
  %.01316 = zext i16 %.013.in15 to i32
  %10 = icmp ugt i32 %.01316, %1
  br i1 %10, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %2
  %11 = getelementptr inbounds i8, ptr %0, i64 16
  %12 = load ptr, ptr %11, align 8, !tbaa !8
  %13 = zext i16 %.013.in15 to i64
  %14 = getelementptr inbounds %union.IRIns, ptr %12, i64 %13
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp eq i32 %15, %9
  br i1 %16, label %.loopexit, label %.lr.ph22

.lr.ph22:                                         ; preds = %.lr.ph, %20
  %17 = phi i64 [ %21, %20 ], [ %13, %.lr.ph ]
  %18 = getelementptr inbounds %union.IRIns, ptr %12, i64 %17, i32 0, i32 3
  %.013.in = load i16, ptr %18, align 2, !tbaa !5
  %.013 = zext i16 %.013.in to i32
  %19 = icmp ugt i32 %.013, %1
  br i1 %19, label %20, label %._crit_edge, !llvm.loop !20

20:                                               ; preds = %.lr.ph22
  %21 = zext i16 %.013.in to i64
  %22 = getelementptr inbounds %union.IRIns, ptr %12, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = icmp eq i32 %23, %9
  br i1 %24, label %.loopexit, label %.lr.ph22, !llvm.loop !20

._crit_edge:                                      ; preds = %.lr.ph22, %2
  %25 = tail call i32 @lj_ir_emit(ptr noundef nonnull %0) #2
  br label %.loopexit

.loopexit:                                        ; preds = %20, %.lr.ph, %._crit_edge
  %.0 = phi i32 [ %25, %._crit_edge ], [ %.01316, %.lr.ph ], [ %.013, %20 ]
  ret i32 %.0
}

attributes #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 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !13, i64 16}
!9 = !{!"jit_State", !10, i64 0, !13, i64 96, !13, i64 104, !13, i64 112, !13, i64 120, !13, i64 128, !12, i64 136, !12, i64 140, !12, i64 144, !6, i64 148, !6, i64 149, !16, i64 150, !6, i64 151, !17, i64 152, !13, i64 176, !12, i64 184, !12, i64 188, !12, i64 192, !12, i64 196, !12, i64 200, !12, i64 204, !12, i64 208, !15, i64 212, !6, i64 216, !13, i64 256, !12, i64 264, !12, i64 268, !12, i64 272, !12, i64 276, !13, i64 280, !13, i64 288, !12, i64 296, !12, i64 300, !6, i64 304, !13, i64 312, !12, i64 320, !12, i64 324, !13, i64 328, !6, i64 336, !6, i64 536, !6, i64 1556, !6, i64 1616, !6, i64 1744, !12, i64 2256, !12, i64 2260, !6, i64 2264, !12, i64 2392, !18, i64 2396, !13, i64 2416, !12, i64 2424, !12, i64 2428, !13, i64 2432, !12, i64 2440, !12, i64 2444, !13, i64 2448, !13, i64 2456, !13, i64 2464, !19, i64 2472, !19, i64 2480, !6, i64 2488, !13, i64 2496, !12, i64 2504, !12, i64 2508}
!10 = !{!"GCtrace", !11, i64 0, !6, i64 4, !6, i64 5, !6, i64 6, !6, i64 7, !12, i64 8, !11, i64 12, !13, i64 16, !12, i64 24, !14, i64 28, !14, i64 30, !13, i64 32, !13, i64 40, !11, i64 48, !15, i64 52, !12, i64 56, !12, i64 60, !13, i64 64, !12, i64 72, !14, i64 76, !14, i64 78, !14, i64 80, !14, i64 82, !14, i64 84, !14, i64 86, !14, i64 88, !6, i64 90, !6, i64 91}
!11 = !{!"GCRef", !12, i64 0}
!12 = !{!"int", !6, i64 0}
!13 = !{!"any pointer", !6, i64 0}
!14 = !{!"short", !6, i64 0}
!15 = !{!"MRef", !12, i64 0}
!16 = !{!"IRType1", !6, i64 0}
!17 = !{!"FoldState", !6, i64 0, !6, i64 8, !6, i64 16}
!18 = !{!"ScEvEntry", !15, i64 0, !14, i64 4, !14, i64 6, !14, i64 8, !14, i64 10, !16, i64 12, !6, i64 13}
!19 = !{!"long", !6, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


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