; LLVM IR from MidnightBlueLabs/TETRA_crypto :: tea1 (tea1_inner)

; 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_awTea1LutA = external dso_local constant [8 x i16], align 16
@g_awTea1LutB = external dso_local constant [8 x i16], align 16
@g_abTea1Sbox = external dso_local constant [256 x i8], 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: nounwind uwtable
declare dso_local zeroext i8 @tea1_state_word_to_newbyte(i16 noundef zeroext, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @tea1_reorder_state_byte(i8 noundef zeroext) #1

; Function Attrs: nounwind uwtable
define dso_local void @tea1_inner(i64 noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef %3) #1 {
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i8, align 1
  %14 = alloca i8, align 1
  %15 = alloca i8, align 1
  %16 = alloca i8, align 1
  %17 = alloca i8, align 1
  %18 = alloca i8, align 1
  store i64 %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 54, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !9
  br label %19

19:                                               ; preds = %93, %4
  %20 = load i32, ptr %10, align 4, !tbaa !9
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = icmp ult i32 %20, %21
  br i1 %22, label %24, label %23

23:                                               ; preds = %19
  store i32 2, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %96

24:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %25

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

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

30:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 1, ptr %13) #2
  %31 = load i32, ptr %6, align 4, !tbaa !9
  %32 = lshr i32 %31, 24
  %33 = load i32, ptr %6, align 4, !tbaa !9
  %34 = xor i32 %32, %33
  %35 = and i32 %34, 255
  %36 = zext i32 %35 to i64
  %37 = getelementptr inbounds [256 x i8], ptr @g_abTea1Sbox, i64 0, i64 %36
  %38 = load i8, ptr %37, align 1, !tbaa !13
  store i8 %38, ptr %13, align 1, !tbaa !13
  %39 = load i32, ptr %6, align 4, !tbaa !9
  %40 = shl i32 %39, 8
  %41 = load i8, ptr %13, align 1, !tbaa !13
  %42 = zext i8 %41 to i32
  %43 = or i32 %40, %42
  store i32 %43, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %14) #2
  %44 = load i64, ptr %5, align 8, !tbaa !5
  %45 = lshr i64 %44, 8
  %46 = and i64 %45, 65535
  %47 = trunc i64 %46 to i16
  %48 = call zeroext i8 @tea1_state_word_to_newbyte(i16 noundef zeroext %47, ptr noundef @g_awTea1LutA)
  store i8 %48, ptr %14, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %15) #2
  %49 = load i64, ptr %5, align 8, !tbaa !5
  %50 = lshr i64 %49, 40
  %51 = and i64 %50, 65535
  %52 = trunc i64 %51 to i16
  %53 = call zeroext i8 @tea1_state_word_to_newbyte(i16 noundef zeroext %52, ptr noundef @g_awTea1LutB)
  store i8 %53, ptr %15, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %16) #2
  %54 = load i64, ptr %5, align 8, !tbaa !5
  %55 = lshr i64 %54, 32
  %56 = and i64 %55, 255
  %57 = trunc i64 %56 to i8
  %58 = call zeroext i8 @tea1_reorder_state_byte(i8 noundef zeroext %57)
  store i8 %58, ptr %16, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %17) #2
  %59 = load i8, ptr %15, align 1, !tbaa !13
  %60 = zext i8 %59 to i64
  %61 = load i64, ptr %5, align 8, !tbaa !5
  %62 = lshr i64 %61, 56
  %63 = xor i64 %60, %62
  %64 = load i8, ptr %16, align 1, !tbaa !13
  %65 = zext i8 %64 to i64
  %66 = xor i64 %63, %65
  %67 = load i8, ptr %13, align 1, !tbaa !13
  %68 = zext i8 %67 to i64
  %69 = xor i64 %66, %68
  %70 = and i64 %69, 255
  %71 = trunc i64 %70 to i8
  store i8 %71, ptr %17, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 1, ptr %18) #2
  %72 = load i8, ptr %14, align 1, !tbaa !13
  store i8 %72, ptr %18, align 1, !tbaa !13
  %73 = load i64, ptr %5, align 8, !tbaa !5
  %74 = shl i64 %73, 8
  %75 = load i8, ptr %18, align 1, !tbaa !13
  %76 = zext i8 %75 to i64
  %77 = shl i64 %76, 32
  %78 = xor i64 %74, %77
  %79 = load i8, ptr %17, align 1, !tbaa !13
  %80 = zext i8 %79 to i64
  %81 = or i64 %78, %80
  store i64 %81, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr %18) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %17) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %16) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %13) #2
  br label %82

82:                                               ; preds = %30
  %83 = load i32, ptr %12, align 4, !tbaa !9
  %84 = add nsw i32 %83, 1
  store i32 %84, ptr %12, align 4, !tbaa !9
  br label %25, !llvm.loop !14

85:                                               ; preds = %29
  %86 = load i64, ptr %5, align 8, !tbaa !5
  %87 = lshr i64 %86, 56
  %88 = trunc i64 %87 to i8
  %89 = load ptr, ptr %8, align 8, !tbaa !11
  %90 = load i32, ptr %10, align 4, !tbaa !9
  %91 = sext i32 %90 to i64
  %92 = getelementptr inbounds i8, ptr %89, i64 %91
  store i8 %88, ptr %92, align 1, !tbaa !13
  store i32 19, ptr %9, align 4, !tbaa !9
  br label %93

93:                                               ; preds = %85
  %94 = load i32, ptr %10, align 4, !tbaa !9
  %95 = add nsw i32 %94, 1
  store i32 %95, ptr %10, align 4, !tbaa !9
  br label %19, !llvm.loop !16

96:                                               ; preds = %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #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 = !{!"long", !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 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !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_awTea1LutA = external dso_local constant [8 x i16], align 16
@g_awTea1LutB = external dso_local constant [8 x i16], align 16
@g_abTea1Sbox = external dso_local local_unnamed_addr constant [256 x i8], align 16

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

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

; Function Attrs: nounwind uwtable
define dso_local void @tea1_inner(i64 noundef %0, i32 noundef %1, i32 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %._crit_edge, label %.preheader.preheader

.preheader.preheader:                             ; preds = %4
  %wide.trip.count = zext i32 %2 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %5
  %indvars.iv = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next, %5 ]
  %.034 = phi i64 [ %0, %.preheader.preheader ], [ %37, %5 ]
  %.02333 = phi i32 [ %1, %.preheader.preheader ], [ %17, %5 ]
  %.02532 = phi i32 [ 54, %.preheader.preheader ], [ 19, %5 ]
  br label %9

._crit_edge:                                      ; preds = %5, %4
  ret void

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

9:                                                ; preds = %.preheader, %9
  %.130 = phi i64 [ %.034, %.preheader ], [ %37, %9 ]
  %.12429 = phi i32 [ %.02333, %.preheader ], [ %17, %9 ]
  %.02728 = phi i32 [ 0, %.preheader ], [ %38, %9 ]
  %10 = lshr i32 %.12429, 24
  %.124.masked = and i32 %.12429, 255
  %11 = xor i32 %10, %.124.masked
  %12 = zext nneg i32 %11 to i64
  %13 = getelementptr inbounds [256 x i8], ptr @g_abTea1Sbox, i64 0, i64 %12
  %14 = load i8, ptr %13, align 1, !tbaa !5
  %15 = shl i32 %.12429, 8
  %16 = zext i8 %14 to i32
  %17 = or disjoint i32 %15, %16
  %18 = lshr i64 %.130, 8
  %19 = trunc i64 %18 to i16
  %20 = tail call zeroext i8 @tea1_state_word_to_newbyte(i16 noundef zeroext %19, ptr noundef nonnull @g_awTea1LutA)
  %21 = lshr i64 %.130, 40
  %22 = trunc i64 %21 to i16
  %23 = tail call zeroext i8 @tea1_state_word_to_newbyte(i16 noundef zeroext %22, ptr noundef nonnull @g_awTea1LutB)
  %24 = lshr i64 %.130, 32
  %25 = trunc i64 %24 to i8
  %26 = tail call zeroext i8 @tea1_reorder_state_byte(i8 noundef zeroext %25)
  %27 = lshr i64 %.130, 56
  %28 = trunc nuw i64 %27 to i8
  %29 = xor i8 %23, %28
  %30 = xor i8 %29, %26
  %31 = xor i8 %30, %14
  %32 = shl i64 %.130, 8
  %33 = zext i8 %20 to i64
  %34 = shl nuw nsw i64 %33, 32
  %35 = xor i64 %34, %32
  %36 = zext i8 %31 to i64
  %37 = or disjoint i64 %35, %36
  %38 = add nuw nsw i32 %.02728, 1
  %exitcond.not = icmp eq i32 %38, %.02532
  br i1 %exitcond.not, label %5, label %9, !llvm.loop !10
}

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

!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: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass