; LLVM IR from trustwallet/wallet-core :: trezor-crypto/crypto/chacha20poly1305 (chacha20poly1305)

; 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.ECRYPT_ctx = type { [16 x i32] }
%struct.chacha20poly1305_ctx = type { %struct.ECRYPT_ctx, %struct.poly1305_context }
%struct.poly1305_context = type { i64, [136 x i8] }

; Function Attrs: nounwind uwtable
define dso_local void @xchacha20poly1305_init(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca [32 x i8], align 16
  %8 = alloca [64 x i8], align 16
  %9 = alloca %struct.ECRYPT_ctx, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 32, ptr %7) #4
  call void @llvm.memset.p0.i64(ptr align 16 %7, i8 0, i64 32, i1 false)
  call void @llvm.lifetime.start.p0(i64 64, ptr %8) #4
  call void @llvm.memset.p0.i64(ptr align 16 %8, i8 0, i64 64, i1 false)
  call void @llvm.lifetime.start.p0(i64 64, ptr %9) #4
  call void @llvm.memset.p0.i64(ptr align 4 %9, i8 0, i64 64, i1 false)
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  call void @ECRYPT_keysetup(ptr noundef %9, ptr noundef %10, i32 noundef 256, i32 noundef 16)
  %11 = load ptr, ptr %6, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 0
  %13 = getelementptr inbounds i32, ptr %12, i64 0
  %14 = load i32, ptr %13, align 4, !tbaa !9
  %15 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %9, i32 0, i32 0
  %16 = getelementptr inbounds [16 x i32], ptr %15, i64 0, i64 12
  store i32 %14, ptr %16, align 4, !tbaa !9
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i64 4
  %19 = getelementptr inbounds i32, ptr %18, i64 0
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %9, i32 0, i32 0
  %22 = getelementptr inbounds [16 x i32], ptr %21, i64 0, i64 13
  store i32 %20, ptr %22, align 4, !tbaa !9
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %23, i64 8
  %25 = getelementptr inbounds i32, ptr %24, i64 0
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %9, i32 0, i32 0
  %28 = getelementptr inbounds [16 x i32], ptr %27, i64 0, i64 14
  store i32 %26, ptr %28, align 4, !tbaa !9
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %29, i64 12
  %31 = getelementptr inbounds i32, ptr %30, i64 0
  %32 = load i32, ptr %31, align 4, !tbaa !9
  %33 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %9, i32 0, i32 0
  %34 = getelementptr inbounds [16 x i32], ptr %33, i64 0, i64 15
  store i32 %32, ptr %34, align 4, !tbaa !9
  %35 = getelementptr inbounds [32 x i8], ptr %7, i64 0, i64 0
  call void @hchacha20(ptr noundef %9, ptr noundef %35)
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.chacha20poly1305_ctx, ptr %36, i32 0, i32 0
  %38 = getelementptr inbounds [32 x i8], ptr %7, i64 0, i64 0
  call void @ECRYPT_keysetup(ptr noundef %37, ptr noundef %38, i32 noundef 256, i32 noundef 16)
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.chacha20poly1305_ctx, ptr %39, i32 0, i32 0
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds i8, ptr %41, i64 16
  call void @ECRYPT_ivsetup(ptr noundef %40, ptr noundef %42)
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.chacha20poly1305_ctx, ptr %43, i32 0, i32 0
  %45 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  %46 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  call void @ECRYPT_encrypt_bytes(ptr noundef %44, ptr noundef %45, ptr noundef %46, i32 noundef 64)
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.chacha20poly1305_ctx, ptr %47, i32 0, i32 1
  %49 = getelementptr inbounds [64 x i8], ptr %8, i64 0, i64 0
  call void @poly1305_init(ptr noundef %48, ptr noundef %49)
  call void @llvm.lifetime.end.p0(i64 64, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr %7) #4
  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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

declare void @ECRYPT_keysetup(ptr noundef, ptr noundef, i32 noundef, i32 noundef) #3

declare void @hchacha20(ptr noundef, ptr noundef) #3

declare void @ECRYPT_ivsetup(ptr noundef, ptr noundef) #3

declare void @ECRYPT_encrypt_bytes(ptr noundef, ptr noundef, ptr noundef, i32 noundef) #3

declare void @poly1305_init(ptr noundef, ptr noundef) #3

; 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: write) }
attributes #3 = { "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 #4 = { 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}


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

%struct.ECRYPT_ctx = type { [16 x i32] }

; Function Attrs: nounwind uwtable
define dso_local void @xchacha20poly1305_init(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = alloca [32 x i8], align 16
  %5 = alloca [64 x i8], align 16
  %6 = alloca %struct.ECRYPT_ctx, align 4
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %4) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(32) %4, i8 0, i64 32, i1 false)
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %5) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(64) %5, i8 0, i64 64, i1 false)
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %6) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(64) %6, i8 0, i64 64, i1 false)
  call void @ECRYPT_keysetup(ptr noundef nonnull %6, ptr noundef %1, i32 noundef 256, i32 noundef 16) #4
  %7 = getelementptr inbounds i8, ptr %6, i64 48
  %8 = load <4 x i32>, ptr %2, align 4, !tbaa !5
  store <4 x i32> %8, ptr %7, align 4, !tbaa !5
  call void @hchacha20(ptr noundef nonnull %6, ptr noundef nonnull %4) #4
  call void @ECRYPT_keysetup(ptr noundef %0, ptr noundef nonnull %4, i32 noundef 256, i32 noundef 16) #4
  %9 = getelementptr inbounds i8, ptr %2, i64 16
  call void @ECRYPT_ivsetup(ptr noundef %0, ptr noundef nonnull %9) #4
  call void @ECRYPT_encrypt_bytes(ptr noundef %0, ptr noundef nonnull %5, ptr noundef nonnull %5, i32 noundef 64) #4
  %10 = getelementptr inbounds i8, ptr %0, i64 64
  call void @poly1305_init(ptr noundef nonnull %10, ptr noundef nonnull %5) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %6) #4
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %5) #4
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %4) #4
  ret void
}

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

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

declare void @ECRYPT_keysetup(ptr noundef, ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #3

declare void @hchacha20(ptr noundef, ptr noundef) local_unnamed_addr #3

declare void @ECRYPT_ivsetup(ptr noundef, ptr noundef) local_unnamed_addr #3

declare void @ECRYPT_encrypt_bytes(ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #3

declare void @poly1305_init(ptr noundef, ptr noundef) local_unnamed_addr #3

; 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: write) }
attributes #3 = { "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 #4 = { 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"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SLPVectorizerPass,SROAPass