; 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: nounwind uwtable
define dso_local i32 @ctr_start(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef %5) #0 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !5
  store i32 %2, ptr %10, align 4, !tbaa !9
  store i32 %3, ptr %11, align 4, !tbaa !9
  store i32 %4, ptr %12, align 4, !tbaa !9
  store ptr %5, ptr %13, align 8, !tbaa !5
  %15 = load ptr, ptr %9, align 8, !tbaa !5
  %16 = load i32, ptr %10, align 4, !tbaa !9
  %17 = load ptr, ptr %13, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.ctr_ctx, ptr %17, i32 0, i32 0
  %19 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %18, i64 0, i64 0
  %20 = call i32 @aes_encrypt_key(ptr noundef %15, i32 noundef %16, ptr noundef %19)
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %22, label %56

22:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %23 = load ptr, ptr %13, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.ctr_ctx, ptr %23, i32 0, i32 3
  store i32 0, ptr %24, align 16, !tbaa !11
  %25 = load i32, ptr %12, align 4, !tbaa !9
  %26 = load ptr, ptr %13, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.ctr_ctx, ptr %26, i32 0, i32 4
  store i32 %25, ptr %27, align 4, !tbaa !13
  store i32 0, ptr %14, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %42, %22
  %29 = load i32, ptr %14, align 4, !tbaa !9
  %30 = icmp slt i32 %29, 16
  br i1 %30, label %31, label %45

31:                                               ; preds = %28
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = load i32, ptr %14, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i8, ptr %32, i64 %34
  %36 = load i8, ptr %35, align 1, !tbaa !14
  %37 = load ptr, ptr %13, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.ctr_ctx, ptr %37, i32 0, i32 1
  %39 = load i32, ptr %14, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [16 x i8], ptr %38, i64 0, i64 %40
  store i8 %36, ptr %41, align 1, !tbaa !14
  br label %42

42:                                               ; preds = %31
  %43 = load i32, ptr %14, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %14, align 4, !tbaa !9
  br label %28, !llvm.loop !15

45:                                               ; preds = %28
  %46 = load ptr, ptr %13, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.ctr_ctx, ptr %46, i32 0, i32 1
  %48 = getelementptr inbounds [16 x i8], ptr %47, i64 0, i64 0
  %49 = load ptr, ptr %13, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.ctr_ctx, ptr %49, i32 0, i32 2
  %51 = getelementptr inbounds [16 x i8], ptr %50, i64 0, i64 0
  %52 = load ptr, ptr %13, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.ctr_ctx, ptr %52, i32 0, i32 0
  %54 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %53, i64 0, i64 0
  %55 = call i32 @aes_encrypt(ptr noundef %48, ptr noundef %51, ptr noundef %54)
  store i32 %55, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  br label %57

56:                                               ; preds = %6
  store i32 1, ptr %7, align 4
  br label %57

57:                                               ; preds = %56, %45
  %58 = load i32, ptr %7, align 4
  ret i32 %58
}

declare i32 @aes_encrypt_key(ptr noundef, i32 noundef, ptr noundef) #1

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

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) #2

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 = { "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 = { nocallback nofree nosync 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 = !{!"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 = !{!12, !10, i64 288}
!12 = !{!"", !7, i64 0, !7, i64 256, !7, i64 272, !10, i64 288, !10, i64 292}
!13 = !{!12, !10, i64 292}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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"

; Function Attrs: nounwind uwtable
define dso_local i32 @ctr_start(ptr nocapture noundef readonly %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef %5) local_unnamed_addr #0 {
  %7 = tail call i32 @aes_encrypt_key(ptr noundef %1, i32 noundef %2, ptr noundef %5) #2
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %9, label %61

9:                                                ; preds = %6
  %10 = getelementptr inbounds i8, ptr %5, i64 288
  store i32 0, ptr %10, align 16, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %5, i64 292
  store i32 %4, ptr %11, align 4, !tbaa !10
  %12 = getelementptr inbounds i8, ptr %5, i64 256
  %13 = load i8, ptr %0, align 1, !tbaa !11
  store i8 %13, ptr %12, align 1, !tbaa !11
  %14 = getelementptr inbounds i8, ptr %0, i64 1
  %15 = load i8, ptr %14, align 1, !tbaa !11
  %16 = getelementptr inbounds i8, ptr %5, i64 257
  store i8 %15, ptr %16, align 1, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %0, i64 2
  %18 = load i8, ptr %17, align 1, !tbaa !11
  %19 = getelementptr inbounds i8, ptr %5, i64 258
  store i8 %18, ptr %19, align 1, !tbaa !11
  %20 = getelementptr inbounds i8, ptr %0, i64 3
  %21 = load i8, ptr %20, align 1, !tbaa !11
  %22 = getelementptr inbounds i8, ptr %5, i64 259
  store i8 %21, ptr %22, align 1, !tbaa !11
  %23 = getelementptr inbounds i8, ptr %0, i64 4
  %24 = load i8, ptr %23, align 1, !tbaa !11
  %25 = getelementptr inbounds i8, ptr %5, i64 260
  store i8 %24, ptr %25, align 1, !tbaa !11
  %26 = getelementptr inbounds i8, ptr %0, i64 5
  %27 = load i8, ptr %26, align 1, !tbaa !11
  %28 = getelementptr inbounds i8, ptr %5, i64 261
  store i8 %27, ptr %28, align 1, !tbaa !11
  %29 = getelementptr inbounds i8, ptr %0, i64 6
  %30 = load i8, ptr %29, align 1, !tbaa !11
  %31 = getelementptr inbounds i8, ptr %5, i64 262
  store i8 %30, ptr %31, align 1, !tbaa !11
  %32 = getelementptr inbounds i8, ptr %0, i64 7
  %33 = load i8, ptr %32, align 1, !tbaa !11
  %34 = getelementptr inbounds i8, ptr %5, i64 263
  store i8 %33, ptr %34, align 1, !tbaa !11
  %35 = getelementptr inbounds i8, ptr %0, i64 8
  %36 = load i8, ptr %35, align 1, !tbaa !11
  %37 = getelementptr inbounds i8, ptr %5, i64 264
  store i8 %36, ptr %37, align 1, !tbaa !11
  %38 = getelementptr inbounds i8, ptr %0, i64 9
  %39 = load i8, ptr %38, align 1, !tbaa !11
  %40 = getelementptr inbounds i8, ptr %5, i64 265
  store i8 %39, ptr %40, align 1, !tbaa !11
  %41 = getelementptr inbounds i8, ptr %0, i64 10
  %42 = load i8, ptr %41, align 1, !tbaa !11
  %43 = getelementptr inbounds i8, ptr %5, i64 266
  store i8 %42, ptr %43, align 1, !tbaa !11
  %44 = getelementptr inbounds i8, ptr %0, i64 11
  %45 = load i8, ptr %44, align 1, !tbaa !11
  %46 = getelementptr inbounds i8, ptr %5, i64 267
  store i8 %45, ptr %46, align 1, !tbaa !11
  %47 = getelementptr inbounds i8, ptr %0, i64 12
  %48 = load i8, ptr %47, align 1, !tbaa !11
  %49 = getelementptr inbounds i8, ptr %5, i64 268
  store i8 %48, ptr %49, align 1, !tbaa !11
  %50 = getelementptr inbounds i8, ptr %0, i64 13
  %51 = load i8, ptr %50, align 1, !tbaa !11
  %52 = getelementptr inbounds i8, ptr %5, i64 269
  store i8 %51, ptr %52, align 1, !tbaa !11
  %53 = getelementptr inbounds i8, ptr %0, i64 14
  %54 = load i8, ptr %53, align 1, !tbaa !11
  %55 = getelementptr inbounds i8, ptr %5, i64 270
  store i8 %54, ptr %55, align 1, !tbaa !11
  %56 = getelementptr inbounds i8, ptr %0, i64 15
  %57 = load i8, ptr %56, align 1, !tbaa !11
  %58 = getelementptr inbounds i8, ptr %5, i64 271
  store i8 %57, ptr %58, align 1, !tbaa !11
  %59 = getelementptr inbounds i8, ptr %5, i64 272
  %60 = tail call i32 @aes_encrypt(ptr noundef nonnull %12, ptr noundef nonnull %59, ptr noundef nonnull %5) #2
  br label %61

61:                                               ; preds = %6, %9
  %.015 = phi i32 [ %60, %9 ], [ 1, %6 ]
  ret i32 %.015
}

declare i32 @aes_encrypt_key(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare i32 @aes_encrypt(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #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 = { "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}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass