; LLVM IR from MidnightBlueLabs/TETRA_crypto :: tea2 (tea2)

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

@g_abTea2LutA = external dso_local constant [8 x i16], align 16
@g_abTea2LutB = external dso_local constant [8 x i16], align 16
@g_abTea2Sbox = external dso_local constant [256 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local void @tea2(i32 noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca [10 x i8], align 1
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i8, align 1
  %16 = alloca i8, align 1
  %17 = alloca i8, align 1
  %18 = alloca i8, align 1
  %19 = alloca i8, align 1
  %20 = alloca i8, align 1
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 10, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 51, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = call i64 @tea2_expand_iv(i32 noundef %21)
  store i64 %22, ptr %11, align 8, !tbaa !11
  %23 = getelementptr inbounds [10 x i8], ptr %9, i64 0, i64 0
  %24 = load ptr, ptr %6, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %23, ptr align 1 %24, i64 10, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  store i32 0, ptr %12, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %104, %4
  %26 = load i32, ptr %12, align 4, !tbaa !5
  %27 = load i32, ptr %7, align 4, !tbaa !5
  %28 = icmp ult i32 %26, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %25
  store i32 2, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %107

30:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  store i32 0, ptr %14, align 4, !tbaa !5
  br label %31

31:                                               ; preds = %93, %30
  %32 = load i32, ptr %14, align 4, !tbaa !5
  %33 = load i32, ptr %10, align 4, !tbaa !5
  %34 = icmp ult i32 %32, %33
  br i1 %34, label %36, label %35

35:                                               ; preds = %31
  store i32 5, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  br label %96

36:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 1, ptr %15) #3
  %37 = getelementptr inbounds [10 x i8], ptr %9, i64 0, i64 0
  %38 = load i8, ptr %37, align 1, !tbaa !13
  %39 = zext i8 %38 to i32
  %40 = getelementptr inbounds [10 x i8], ptr %9, i64 0, i64 7
  %41 = load i8, ptr %40, align 1, !tbaa !13
  %42 = zext i8 %41 to i32
  %43 = xor i32 %39, %42
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [256 x i8], ptr @g_abTea2Sbox, i64 0, i64 %44
  %46 = load i8, ptr %45, align 1, !tbaa !13
  store i8 %46, ptr %15, align 1, !tbaa !13
  %47 = getelementptr inbounds [10 x i8], ptr %9, i64 0, i64 0
  %48 = getelementptr inbounds [10 x i8], ptr %9, i64 0, i64 0
  %49 = getelementptr inbounds i8, ptr %48, i64 1
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %47, ptr align 1 %49, i64 9, i1 false)
  %50 = load i8, ptr %15, align 1, !tbaa !13
  %51 = getelementptr inbounds [10 x i8], ptr %9, i64 0, i64 9
  store i8 %50, ptr %51, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %16) #3
  %52 = load i64, ptr %11, align 8, !tbaa !11
  %53 = lshr i64 %52, 0
  %54 = and i64 %53, 65535
  %55 = trunc i64 %54 to i16
  %56 = call zeroext i8 @tea2_state_word_to_newbyte(i16 noundef zeroext %55, ptr noundef @g_abTea2LutA)
  store i8 %56, ptr %16, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %17) #3
  %57 = load i64, ptr %11, align 8, !tbaa !11
  %58 = lshr i64 %57, 24
  %59 = and i64 %58, 65535
  %60 = trunc i64 %59 to i16
  %61 = call zeroext i8 @tea2_state_word_to_newbyte(i16 noundef zeroext %60, ptr noundef @g_abTea2LutB)
  store i8 %61, ptr %17, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %18) #3
  %62 = load i64, ptr %11, align 8, !tbaa !11
  %63 = lshr i64 %62, 40
  %64 = and i64 %63, 255
  %65 = trunc i64 %64 to i8
  %66 = call zeroext i8 @tea2_reorder_state_byte(i8 noundef zeroext %65)
  store i8 %66, ptr %18, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %19) #3
  %67 = load i64, ptr %11, align 8, !tbaa !11
  %68 = lshr i64 %67, 56
  %69 = load i64, ptr %11, align 8, !tbaa !11
  %70 = lshr i64 %69, 16
  %71 = xor i64 %68, %70
  %72 = load i8, ptr %18, align 1, !tbaa !13
  %73 = zext i8 %72 to i64
  %74 = xor i64 %71, %73
  %75 = load i8, ptr %16, align 1, !tbaa !13
  %76 = zext i8 %75 to i64
  %77 = xor i64 %74, %76
  %78 = load i8, ptr %15, align 1, !tbaa !13
  %79 = zext i8 %78 to i64
  %80 = xor i64 %77, %79
  %81 = and i64 %80, 255
  %82 = trunc i64 %81 to i8
  store i8 %82, ptr %19, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %20) #3
  %83 = load i8, ptr %17, align 1, !tbaa !13
  store i8 %83, ptr %20, align 1, !tbaa !13
  %84 = load i64, ptr %11, align 8, !tbaa !11
  %85 = shl i64 %84, 8
  %86 = load i8, ptr %20, align 1, !tbaa !13
  %87 = zext i8 %86 to i64
  %88 = shl i64 %87, 24
  %89 = xor i64 %85, %88
  %90 = load i8, ptr %19, align 1, !tbaa !13
  %91 = zext i8 %90 to i64
  %92 = or i64 %89, %91
  store i64 %92, ptr %11, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 1, ptr %20) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %19) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %18) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %17) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %16) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %15) #3
  br label %93

93:                                               ; preds = %36
  %94 = load i32, ptr %14, align 4, !tbaa !5
  %95 = add nsw i32 %94, 1
  store i32 %95, ptr %14, align 4, !tbaa !5
  br label %31, !llvm.loop !14

96:                                               ; preds = %35
  %97 = load i64, ptr %11, align 8, !tbaa !11
  %98 = lshr i64 %97, 56
  %99 = trunc i64 %98 to i8
  %100 = load ptr, ptr %8, align 8, !tbaa !9
  %101 = load i32, ptr %12, align 4, !tbaa !5
  %102 = sext i32 %101 to i64
  %103 = getelementptr inbounds i8, ptr %100, i64 %102
  store i8 %99, ptr %103, align 1, !tbaa !13
  store i32 19, ptr %10, align 4, !tbaa !5
  br label %104

104:                                              ; preds = %96
  %105 = load i32, ptr %12, align 4, !tbaa !5
  %106 = add nsw i32 %105, 1
  store i32 %106, ptr %12, align 4, !tbaa !5
  br label %25, !llvm.loop !16

107:                                              ; preds = %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 10, ptr %9) #3
  ret void
}

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

; Function Attrs: nounwind uwtable
declare hidden i64 @tea2_expand_iv(i32 noundef) #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @tea2_state_word_to_newbyte(i16 noundef zeroext, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @tea2_reorder_state_byte(i8 noundef zeroext) #0

; 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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

@g_abTea2LutA = external dso_local constant [8 x i16], align 16
@g_abTea2LutB = external dso_local constant [8 x i16], align 16
@g_abTea2Sbox = external dso_local local_unnamed_addr constant [256 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local void @tea2(i32 noundef %0, ptr nocapture noundef readonly %1, i32 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = alloca [10 x i8], align 1
  call void @llvm.lifetime.start.p0(i64 10, ptr nonnull %5)
  %6 = tail call i64 @tea2_expand_iv(i32 noundef %0)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(10) %5, ptr noundef nonnull align 1 dereferenceable(10) %1, i64 10, i1 false)
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %._crit_edge, label %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %4
  %wide.trip.count = zext i32 %2 to i64
  %.7.sroa_idx42 = getelementptr inbounds i8, ptr %5, i64 7
  %.1.sroa_idx41 = getelementptr inbounds i8, ptr %5, i64 1
  %.9.sroa_idx43 = getelementptr inbounds i8, ptr %5, i64 9
  br label %.preheader

.preheader:                                       ; preds = %.preheader.lr.ph, %7
  %indvars.iv = phi i64 [ 0, %.preheader.lr.ph ], [ %indvars.iv.next, %7 ]
  %.034 = phi i32 [ 51, %.preheader.lr.ph ], [ 19, %7 ]
  %.02433 = phi i64 [ %6, %.preheader.lr.ph ], [ %35, %7 ]
  br label %11

._crit_edge:                                      ; preds = %7, %4
  call void @llvm.lifetime.end.p0(i64 10, ptr nonnull %5)
  ret void

7:                                                ; preds = %11
  %8 = lshr i64 %30, 56
  %9 = trunc nuw i64 %8 to i8
  %10 = getelementptr inbounds i8, ptr %3, i64 %indvars.iv
  store i8 %9, ptr %10, align 1, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond37.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond37.not, label %._crit_edge, label %.preheader, !llvm.loop !8

11:                                               ; preds = %.preheader, %11
  %.131 = phi i64 [ %.02433, %.preheader ], [ %35, %11 ]
  %.02630 = phi i32 [ 0, %.preheader ], [ %36, %11 ]
  %.0..0..0..0. = load i8, ptr %5, align 1, !tbaa !5
  %.7..7..7..7. = load i8, ptr %.7.sroa_idx42, align 1, !tbaa !5
  %12 = xor i8 %.7..7..7..7., %.0..0..0..0.
  %13 = zext i8 %12 to i64
  %14 = getelementptr inbounds [256 x i8], ptr @g_abTea2Sbox, i64 0, i64 %13
  %15 = load i8, ptr %14, align 1, !tbaa !5
  call void @llvm.memmove.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(9) %5, ptr noundef nonnull align 1 dereferenceable(9) %.1.sroa_idx41, i64 9, i1 false)
  store i8 %15, ptr %.9.sroa_idx43, align 1, !tbaa !5
  %16 = trunc i64 %.131 to i16
  %17 = tail call zeroext i8 @tea2_state_word_to_newbyte(i16 noundef zeroext %16, ptr noundef nonnull @g_abTea2LutA)
  %18 = lshr i64 %.131, 24
  %19 = trunc i64 %18 to i16
  %20 = tail call zeroext i8 @tea2_state_word_to_newbyte(i16 noundef zeroext %19, ptr noundef nonnull @g_abTea2LutB)
  %21 = lshr i64 %.131, 40
  %22 = trunc i64 %21 to i8
  %23 = tail call zeroext i8 @tea2_reorder_state_byte(i8 noundef zeroext %22)
  %24 = lshr i64 %.131, 56
  %25 = lshr i64 %.131, 16
  %26 = xor i64 %24, %25
  %27 = trunc i64 %26 to i8
  %28 = xor i8 %17, %27
  %29 = xor i8 %28, %23
  %.narrow = xor i8 %29, %15
  %30 = shl i64 %.131, 8
  %31 = zext i8 %20 to i64
  %32 = shl nuw nsw i64 %31, 24
  %33 = xor i64 %32, %30
  %34 = zext i8 %.narrow to i64
  %35 = or disjoint i64 %33, %34
  %36 = add nuw nsw i32 %.02630, 1
  %exitcond.not = icmp eq i32 %36, %.034
  br i1 %exitcond.not, label %7, label %11, !llvm.loop !10
}

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

; Function Attrs: nounwind uwtable
declare hidden i64 @tea2_expand_iv(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @tea2_state_word_to_newbyte(i16 noundef zeroext, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @tea2_reorder_state_byte(i8 noundef zeroext) local_unnamed_addr #0

; 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }

!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 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}


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