; LLVM IR from square/subzero :: core/external-crypto/modes/ltc (ltc_ctr)

; 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.ctr_ctx = type { [1 x %struct.aes_crypt_ctx], [16 x i8], [16 x i8], i32, i32, [8 x i8] }
%struct.aes_crypt_ctx = type { [60 x i32], %union.aes_inf, [12 x i8] }
%union.aes_inf = type { i32 }

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

declare i32 @aes_encrypt(ptr noundef, ptr noundef, 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 dso_local i32 @ctr_encrypt(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) #2 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %12 = load ptr, ptr %9, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.ctr_ctx, ptr %12, i32 0, i32 3
  %14 = load i32, ptr %13, align 16, !tbaa !11
  %15 = icmp slt i32 %14, 0
  br i1 %15, label %21, label %16

16:                                               ; preds = %4
  %17 = load ptr, ptr %9, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.ctr_ctx, ptr %17, i32 0, i32 3
  %19 = load i32, ptr %18, align 16, !tbaa !11
  %20 = icmp sgt i32 %19, 16
  br i1 %20, label %21, label %22

21:                                               ; preds = %16, %4
  store i32 1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %128

22:                                               ; preds = %16
  br label %23

23:                                               ; preds = %106, %22
  %24 = load i64, ptr %8, align 8, !tbaa !9
  %25 = icmp ne i64 %24, 0
  br i1 %25, label %26, label %127

26:                                               ; preds = %23
  %27 = load ptr, ptr %9, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.ctr_ctx, ptr %27, i32 0, i32 3
  %29 = load i32, ptr %28, align 16, !tbaa !11
  %30 = icmp eq i32 %29, 16
  br i1 %30, label %31, label %106

31:                                               ; preds = %26
  %32 = load ptr, ptr %9, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.ctr_ctx, ptr %32, i32 0, i32 4
  %34 = load i32, ptr %33, align 4, !tbaa !14
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %36, label %63

36:                                               ; preds = %31
  store i32 0, ptr %10, align 4, !tbaa !15
  br label %37

37:                                               ; preds = %59, %36
  %38 = load i32, ptr %10, align 4, !tbaa !15
  %39 = icmp slt i32 %38, 16
  br i1 %39, label %40, label %62

40:                                               ; preds = %37
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.ctr_ctx, ptr %41, i32 0, i32 1
  %43 = load i32, ptr %10, align 4, !tbaa !15
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [16 x i8], ptr %42, i64 0, i64 %44
  %46 = load i8, ptr %45, align 1, !tbaa !16
  %47 = zext i8 %46 to i32
  %48 = add nsw i32 %47, 1
  %49 = and i32 %48, 255
  %50 = trunc i32 %49 to i8
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.ctr_ctx, ptr %51, i32 0, i32 1
  %53 = load i32, ptr %10, align 4, !tbaa !15
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [16 x i8], ptr %52, i64 0, i64 %54
  store i8 %50, ptr %55, align 1, !tbaa !16
  %56 = icmp ne i8 %50, 0
  br i1 %56, label %57, label %58

57:                                               ; preds = %40
  br label %62

58:                                               ; preds = %40
  br label %59

59:                                               ; preds = %58
  %60 = load i32, ptr %10, align 4, !tbaa !15
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %10, align 4, !tbaa !15
  br label %37, !llvm.loop !17

62:                                               ; preds = %57, %37
  br label %90

63:                                               ; preds = %31
  store i32 15, ptr %10, align 4, !tbaa !15
  br label %64

64:                                               ; preds = %86, %63
  %65 = load i32, ptr %10, align 4, !tbaa !15
  %66 = icmp sge i32 %65, 0
  br i1 %66, label %67, label %89

67:                                               ; preds = %64
  %68 = load ptr, ptr %9, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.ctr_ctx, ptr %68, i32 0, i32 1
  %70 = load i32, ptr %10, align 4, !tbaa !15
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds [16 x i8], ptr %69, i64 0, i64 %71
  %73 = load i8, ptr %72, align 1, !tbaa !16
  %74 = zext i8 %73 to i32
  %75 = add nsw i32 %74, 1
  %76 = and i32 %75, 255
  %77 = trunc i32 %76 to i8
  %78 = load ptr, ptr %9, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.ctr_ctx, ptr %78, i32 0, i32 1
  %80 = load i32, ptr %10, align 4, !tbaa !15
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds [16 x i8], ptr %79, i64 0, i64 %81
  store i8 %77, ptr %82, align 1, !tbaa !16
  %83 = icmp ne i8 %77, 0
  br i1 %83, label %84, label %85

84:                                               ; preds = %67
  br label %89

85:                                               ; preds = %67
  br label %86

86:                                               ; preds = %85
  %87 = load i32, ptr %10, align 4, !tbaa !15
  %88 = add nsw i32 %87, -1
  store i32 %88, ptr %10, align 4, !tbaa !15
  br label %64, !llvm.loop !19

89:                                               ; preds = %84, %64
  br label %90

90:                                               ; preds = %89, %62
  %91 = load ptr, ptr %9, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.ctr_ctx, ptr %91, i32 0, i32 1
  %93 = getelementptr inbounds [16 x i8], ptr %92, i64 0, i64 0
  %94 = load ptr, ptr %9, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.ctr_ctx, ptr %94, i32 0, i32 2
  %96 = getelementptr inbounds [16 x i8], ptr %95, i64 0, i64 0
  %97 = load ptr, ptr %9, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.ctr_ctx, ptr %97, i32 0, i32 0
  %99 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %98, i64 0, i64 0
  %100 = call i32 @aes_encrypt(ptr noundef %93, ptr noundef %96, ptr noundef %99)
  %101 = icmp ne i32 %100, 0
  br i1 %101, label %102, label %103

102:                                              ; preds = %90
  store i32 1, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %128

103:                                              ; preds = %90
  %104 = load ptr, ptr %9, align 8, !tbaa !5
  %105 = getelementptr inbounds %struct.ctr_ctx, ptr %104, i32 0, i32 3
  store i32 0, ptr %105, align 16, !tbaa !11
  br label %106

106:                                              ; preds = %103, %26
  %107 = load ptr, ptr %6, align 8, !tbaa !5
  %108 = getelementptr inbounds i8, ptr %107, i32 1
  store ptr %108, ptr %6, align 8, !tbaa !5
  %109 = load i8, ptr %107, align 1, !tbaa !16
  %110 = zext i8 %109 to i32
  %111 = load ptr, ptr %9, align 8, !tbaa !5
  %112 = getelementptr inbounds %struct.ctr_ctx, ptr %111, i32 0, i32 2
  %113 = load ptr, ptr %9, align 8, !tbaa !5
  %114 = getelementptr inbounds %struct.ctr_ctx, ptr %113, i32 0, i32 3
  %115 = load i32, ptr %114, align 16, !tbaa !11
  %116 = add nsw i32 %115, 1
  store i32 %116, ptr %114, align 16, !tbaa !11
  %117 = sext i32 %115 to i64
  %118 = getelementptr inbounds [16 x i8], ptr %112, i64 0, i64 %117
  %119 = load i8, ptr %118, align 1, !tbaa !16
  %120 = zext i8 %119 to i32
  %121 = xor i32 %110, %120
  %122 = trunc i32 %121 to i8
  %123 = load ptr, ptr %7, align 8, !tbaa !5
  %124 = getelementptr inbounds i8, ptr %123, i32 1
  store ptr %124, ptr %7, align 8, !tbaa !5
  store i8 %122, ptr %123, align 1, !tbaa !16
  %125 = load i64, ptr %8, align 8, !tbaa !9
  %126 = add i64 %125, -1
  store i64 %126, ptr %8, align 8, !tbaa !9
  br label %23, !llvm.loop !20

127:                                              ; preds = %23
  store i32 0, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %128

128:                                              ; preds = %127, %102, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %129 = load i32, ptr %5, align 4
  ret i32 %129
}

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 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 288}
!12 = !{!"", !7, i64 0, !7, i64 256, !7, i64 272, !13, i64 288, !13, i64 292}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !13, i64 292}
!15 = !{!13, !13, i64 0}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}
!20 = distinct !{!20, !18}


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

declare i32 @aes_encrypt(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @ctr_encrypt(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, i64 noundef %2, ptr noundef %3) local_unnamed_addr #1 {
  %5 = getelementptr inbounds i8, ptr %3, i64 288
  %6 = load i32, ptr %5, align 16, !tbaa !5
  %or.cond = icmp ugt i32 %6, 16
  br i1 %or.cond, label %.loopexit37, label %.preheader36

.preheader36:                                     ; preds = %4
  %.not40 = icmp eq i64 %2, 0
  br i1 %.not40, label %.loopexit37, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader36
  %7 = getelementptr inbounds i8, ptr %3, i64 292
  %8 = getelementptr inbounds i8, ptr %3, i64 256
  %9 = getelementptr inbounds i8, ptr %3, i64 272
  br label %10

10:                                               ; preds = %.lr.ph, %28
  %.02743 = phi i64 [ %2, %.lr.ph ], [ %38, %28 ]
  %.02842 = phi ptr [ %1, %.lr.ph ], [ %37, %28 ]
  %.02941 = phi ptr [ %0, %.lr.ph ], [ %30, %28 ]
  %11 = load i32, ptr %5, align 16, !tbaa !5
  %12 = icmp eq i32 %11, 16
  br i1 %12, label %13, label %28

13:                                               ; preds = %10
  %14 = load i32, ptr %7, align 4, !tbaa !10
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %.preheader, label %.preheader34

.preheader:                                       ; preds = %13, %.preheader
  %indvars.iv = phi i64 [ %indvars.iv.next, %.preheader ], [ 0, %13 ]
  %16 = getelementptr inbounds [16 x i8], ptr %8, i64 0, i64 %indvars.iv
  %17 = load i8, ptr %16, align 1, !tbaa !11
  %18 = add i8 %17, 1
  store i8 %18, ptr %16, align 1, !tbaa !11
  %.not32 = icmp eq i8 %18, 0
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %19 = icmp ult i64 %indvars.iv, 15
  %or.cond46 = and i1 %.not32, %19
  br i1 %or.cond46, label %.preheader, label %.loopexit, !llvm.loop !12

.preheader34:                                     ; preds = %13, %.preheader34
  %.138 = phi i32 [ %24, %.preheader34 ], [ 15, %13 ]
  %20 = zext nneg i32 %.138 to i64
  %21 = getelementptr inbounds [16 x i8], ptr %8, i64 0, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !11
  %23 = add i8 %22, 1
  store i8 %23, ptr %21, align 1, !tbaa !11
  %.not31 = icmp eq i8 %23, 0
  %24 = add nsw i32 %.138, -1
  %25 = icmp ne i32 %.138, 0
  %or.cond47 = and i1 %.not31, %25
  br i1 %or.cond47, label %.preheader34, label %.loopexit, !llvm.loop !14

.loopexit:                                        ; preds = %.preheader34, %.preheader
  %26 = tail call i32 @aes_encrypt(ptr noundef nonnull %8, ptr noundef nonnull %9, ptr noundef nonnull %3) #2
  %.not33 = icmp eq i32 %26, 0
  br i1 %.not33, label %27, label %.loopexit37

27:                                               ; preds = %.loopexit
  store i32 0, ptr %5, align 16, !tbaa !5
  br label %28

28:                                               ; preds = %27, %10
  %29 = phi i32 [ 0, %27 ], [ %11, %10 ]
  %30 = getelementptr inbounds i8, ptr %.02941, i64 1
  %31 = load i8, ptr %.02941, align 1, !tbaa !11
  %32 = add nsw i32 %29, 1
  store i32 %32, ptr %5, align 16, !tbaa !5
  %33 = sext i32 %29 to i64
  %34 = getelementptr inbounds [16 x i8], ptr %9, i64 0, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !11
  %36 = xor i8 %35, %31
  %37 = getelementptr inbounds i8, ptr %.02842, i64 1
  store i8 %36, ptr %.02842, align 1, !tbaa !11
  %38 = add i64 %.02743, -1
  %.not = icmp eq i64 %38, 0
  br i1 %.not, label %.loopexit37, label %10, !llvm.loop !15

.loopexit37:                                      ; preds = %.loopexit, %28, %.preheader36, %4
  %.026 = phi i32 [ 1, %4 ], [ 0, %.preheader36 ], [ 1, %.loopexit ], [ 0, %28 ]
  ret i32 %.026
}

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, !9, i64 288}
!6 = !{!"", !7, i64 0, !7, i64 256, !7, i64 272, !9, i64 288, !9, i64 292}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!6, !9, i64 292}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}
!15 = distinct !{!15, !13}


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