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

; 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_awTea3LutA = external dso_local constant [8 x i16], align 16
@g_awTea3LutB = external dso_local constant [8 x i16], align 16
@g_abTea3Sbox = external dso_local constant [256 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local void @tea3(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 @tea3_compute_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 = %107, %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 %110

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 = %96, %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 %99

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 7
  %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 2
  %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_abTea3Sbox, i64 0, i64 %44
  %46 = load i8, ptr %45, align 1, !tbaa !13
  %47 = zext i8 %46 to i32
  %48 = getelementptr inbounds [10 x i8], ptr %9, i64 0, i64 0
  %49 = load i8, ptr %48, align 1, !tbaa !13
  %50 = zext i8 %49 to i32
  %51 = xor i32 %47, %50
  %52 = trunc i32 %51 to i8
  store i8 %52, ptr %15, align 1, !tbaa !13
  %53 = getelementptr inbounds [10 x i8], ptr %9, i64 0, i64 0
  %54 = getelementptr inbounds [10 x i8], ptr %9, i64 0, i64 0
  %55 = getelementptr inbounds i8, ptr %54, i64 1
  call void @llvm.memmove.p0.p0.i64(ptr align 1 %53, ptr align 1 %55, i64 9, i1 false)
  %56 = load i8, ptr %15, align 1, !tbaa !13
  %57 = getelementptr inbounds [10 x i8], ptr %9, i64 0, i64 9
  store i8 %56, ptr %57, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %16) #3
  %58 = load i64, ptr %11, align 8, !tbaa !11
  %59 = lshr i64 %58, 8
  %60 = and i64 %59, 65535
  %61 = trunc i64 %60 to i16
  %62 = call zeroext i8 @tea3_state_word_to_newbyte(i16 noundef zeroext %61, ptr noundef @g_awTea3LutA)
  store i8 %62, ptr %16, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %17) #3
  %63 = load i64, ptr %11, align 8, !tbaa !11
  %64 = lshr i64 %63, 40
  %65 = and i64 %64, 65535
  %66 = trunc i64 %65 to i16
  %67 = call zeroext i8 @tea3_state_word_to_newbyte(i16 noundef zeroext %66, ptr noundef @g_awTea3LutB)
  store i8 %67, ptr %17, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %18) #3
  %68 = load i64, ptr %11, align 8, !tbaa !11
  %69 = lshr i64 %68, 32
  %70 = and i64 %69, 255
  %71 = trunc i64 %70 to i8
  %72 = call zeroext i8 @tea3_reorder_state_byte(i8 noundef zeroext %71)
  store i8 %72, ptr %18, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %19) #3
  %73 = load i64, ptr %11, align 8, !tbaa !11
  %74 = lshr i64 %73, 56
  %75 = load i8, ptr %18, align 1, !tbaa !13
  %76 = zext i8 %75 to i64
  %77 = xor i64 %74, %76
  %78 = load i8, ptr %16, align 1, !tbaa !13
  %79 = zext i8 %78 to i64
  %80 = xor i64 %77, %79
  %81 = load i8, ptr %15, align 1, !tbaa !13
  %82 = zext i8 %81 to i64
  %83 = xor i64 %80, %82
  %84 = and i64 %83, 255
  %85 = trunc i64 %84 to i8
  store i8 %85, ptr %19, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %20) #3
  %86 = load i8, ptr %17, align 1, !tbaa !13
  store i8 %86, ptr %20, align 1, !tbaa !13
  %87 = load i64, ptr %11, align 8, !tbaa !11
  %88 = shl i64 %87, 8
  %89 = load i8, ptr %20, align 1, !tbaa !13
  %90 = zext i8 %89 to i64
  %91 = shl i64 %90, 40
  %92 = xor i64 %88, %91
  %93 = load i8, ptr %19, align 1, !tbaa !13
  %94 = zext i8 %93 to i64
  %95 = or i64 %92, %94
  store i64 %95, 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 %96

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

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

107:                                              ; preds = %99
  %108 = load i32, ptr %12, align 4, !tbaa !5
  %109 = add nsw i32 %108, 1
  store i32 %109, ptr %12, align 4, !tbaa !5
  br label %25, !llvm.loop !16

110:                                              ; 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 @tea3_compute_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 @tea3_state_word_to_newbyte(i16 noundef zeroext, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @tea3_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_awTea3LutA = external dso_local constant [8 x i16], align 16
@g_awTea3LutB = external dso_local constant [8 x i16], align 16
@g_abTea3Sbox = external dso_local local_unnamed_addr constant [256 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local void @tea3(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 @tea3_compute_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_idx43 = getelementptr inbounds i8, ptr %5, i64 7
  %.2.sroa_idx42 = getelementptr inbounds i8, ptr %5, i64 2
  %.1.sroa_idx41 = getelementptr inbounds i8, ptr %5, i64 1
  %.9.sroa_idx44 = 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 ]
  %.02333 = phi i64 [ %6, %.preheader.lr.ph ], [ %36, %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 %31, 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 [ %.02333, %.preheader ], [ %36, %11 ]
  %.02530 = phi i32 [ 0, %.preheader ], [ %37, %11 ]
  %.7..7..7..7. = load i8, ptr %.7.sroa_idx43, align 1, !tbaa !5
  %.2..2..2..2. = load i8, ptr %.2.sroa_idx42, align 1, !tbaa !5
  %12 = xor i8 %.2..2..2..2., %.7..7..7..7.
  %13 = zext i8 %12 to i64
  %14 = getelementptr inbounds [256 x i8], ptr @g_abTea3Sbox, i64 0, i64 %13
  %15 = load i8, ptr %14, align 1, !tbaa !5
  %.0..0..0..0. = load i8, ptr %5, align 1, !tbaa !5
  %16 = xor i8 %.0..0..0..0., %15
  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 %16, ptr %.9.sroa_idx44, align 1, !tbaa !5
  %17 = lshr i64 %.131, 8
  %18 = trunc i64 %17 to i16
  %19 = tail call zeroext i8 @tea3_state_word_to_newbyte(i16 noundef zeroext %18, ptr noundef nonnull @g_awTea3LutA)
  %20 = lshr i64 %.131, 40
  %21 = trunc i64 %20 to i16
  %22 = tail call zeroext i8 @tea3_state_word_to_newbyte(i16 noundef zeroext %21, ptr noundef nonnull @g_awTea3LutB)
  %23 = lshr i64 %.131, 32
  %24 = trunc i64 %23 to i8
  %25 = tail call zeroext i8 @tea3_reorder_state_byte(i8 noundef zeroext %24)
  %26 = lshr i64 %.131, 56
  %27 = trunc nuw i64 %26 to i8
  %28 = xor i8 %19, %27
  %29 = xor i8 %28, %25
  %30 = xor i8 %29, %16
  %31 = shl i64 %.131, 8
  %32 = zext i8 %22 to i64
  %33 = shl nuw nsw i64 %32, 40
  %34 = xor i64 %33, %31
  %35 = zext i8 %30 to i64
  %36 = or disjoint i64 %34, %35
  %37 = add nuw nsw i32 %.02530, 1
  %exitcond.not = icmp eq i32 %37, %.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 @tea3_compute_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 @tea3_state_word_to_newbyte(i16 noundef zeroext, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @tea3_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