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

; 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] }

@chacha_sigma = external dso_local constant [16 x i8], align 16
@tau = external dso_local constant [16 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local void @ECRYPT_keysetup(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !9
  %10 = load i32, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  store ptr null, ptr %9, align 8, !tbaa !5
  %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 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %15, i32 0, i32 0
  %17 = getelementptr inbounds [16 x i32], ptr %16, i64 0, i64 4
  store i32 %14, ptr %17, align 4, !tbaa !9
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %18, i64 4
  %20 = getelementptr inbounds i32, ptr %19, i64 0
  %21 = load i32, ptr %20, align 4, !tbaa !9
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %22, i32 0, i32 0
  %24 = getelementptr inbounds [16 x i32], ptr %23, i64 0, i64 5
  store i32 %21, ptr %24, align 4, !tbaa !9
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %25, i64 8
  %27 = getelementptr inbounds i32, ptr %26, i64 0
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %29, i32 0, i32 0
  %31 = getelementptr inbounds [16 x i32], ptr %30, i64 0, i64 6
  store i32 %28, ptr %31, align 4, !tbaa !9
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %32, i64 12
  %34 = getelementptr inbounds i32, ptr %33, i64 0
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %36, i32 0, i32 0
  %38 = getelementptr inbounds [16 x i32], ptr %37, i64 0, i64 7
  store i32 %35, ptr %38, align 4, !tbaa !9
  %39 = load i32, ptr %7, align 4, !tbaa !9
  %40 = icmp eq i32 %39, 256
  br i1 %40, label %41, label %44

41:                                               ; preds = %4
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %42, i64 16
  store ptr %43, ptr %6, align 8, !tbaa !5
  store ptr @chacha_sigma, ptr %9, align 8, !tbaa !5
  br label %45

44:                                               ; preds = %4
  store ptr @tau, ptr %9, align 8, !tbaa !5
  br label %45

45:                                               ; preds = %44, %41
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = getelementptr inbounds i8, ptr %46, i64 0
  %48 = getelementptr inbounds i32, ptr %47, i64 0
  %49 = load i32, ptr %48, align 4, !tbaa !9
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %50, i32 0, i32 0
  %52 = getelementptr inbounds [16 x i32], ptr %51, i64 0, i64 8
  store i32 %49, ptr %52, align 4, !tbaa !9
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = getelementptr inbounds i8, ptr %53, i64 4
  %55 = getelementptr inbounds i32, ptr %54, i64 0
  %56 = load i32, ptr %55, align 4, !tbaa !9
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %57, i32 0, i32 0
  %59 = getelementptr inbounds [16 x i32], ptr %58, i64 0, i64 9
  store i32 %56, ptr %59, align 4, !tbaa !9
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = getelementptr inbounds i8, ptr %60, i64 8
  %62 = getelementptr inbounds i32, ptr %61, i64 0
  %63 = load i32, ptr %62, align 4, !tbaa !9
  %64 = load ptr, ptr %5, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %64, i32 0, i32 0
  %66 = getelementptr inbounds [16 x i32], ptr %65, i64 0, i64 10
  store i32 %63, ptr %66, align 4, !tbaa !9
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = getelementptr inbounds i8, ptr %67, i64 12
  %69 = getelementptr inbounds i32, ptr %68, i64 0
  %70 = load i32, ptr %69, align 4, !tbaa !9
  %71 = load ptr, ptr %5, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %71, i32 0, i32 0
  %73 = getelementptr inbounds [16 x i32], ptr %72, i64 0, i64 11
  store i32 %70, ptr %73, align 4, !tbaa !9
  %74 = load ptr, ptr %9, align 8, !tbaa !5
  %75 = getelementptr inbounds i8, ptr %74, i64 0
  %76 = getelementptr inbounds i32, ptr %75, i64 0
  %77 = load i32, ptr %76, align 4, !tbaa !9
  %78 = load ptr, ptr %5, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %78, i32 0, i32 0
  %80 = getelementptr inbounds [16 x i32], ptr %79, i64 0, i64 0
  store i32 %77, ptr %80, align 4, !tbaa !9
  %81 = load ptr, ptr %9, align 8, !tbaa !5
  %82 = getelementptr inbounds i8, ptr %81, i64 4
  %83 = getelementptr inbounds i32, ptr %82, i64 0
  %84 = load i32, ptr %83, align 4, !tbaa !9
  %85 = load ptr, ptr %5, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %85, i32 0, i32 0
  %87 = getelementptr inbounds [16 x i32], ptr %86, i64 0, i64 1
  store i32 %84, ptr %87, align 4, !tbaa !9
  %88 = load ptr, ptr %9, align 8, !tbaa !5
  %89 = getelementptr inbounds i8, ptr %88, i64 8
  %90 = getelementptr inbounds i32, ptr %89, i64 0
  %91 = load i32, ptr %90, align 4, !tbaa !9
  %92 = load ptr, ptr %5, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %92, i32 0, i32 0
  %94 = getelementptr inbounds [16 x i32], ptr %93, i64 0, i64 2
  store i32 %91, ptr %94, align 4, !tbaa !9
  %95 = load ptr, ptr %9, align 8, !tbaa !5
  %96 = getelementptr inbounds i8, ptr %95, i64 12
  %97 = getelementptr inbounds i32, ptr %96, i64 0
  %98 = load i32, ptr %97, align 4, !tbaa !9
  %99 = load ptr, ptr %5, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.ECRYPT_ctx, ptr %99, i32 0, i32 0
  %101 = getelementptr inbounds [16 x i32], ptr %100, i64 0, i64 3
  store i32 %98, ptr %101, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  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 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 = { 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"

@chacha_sigma = external dso_local local_unnamed_addr constant [16 x i8], align 16
@tau = external dso_local local_unnamed_addr constant [16 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) uwtable
define dso_local void @ECRYPT_keysetup(ptr nocapture noundef writeonly %0, ptr nocapture noundef readonly %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  store i32 %5, ptr %6, align 4, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %1, i64 4
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %0, i64 20
  store i32 %8, ptr %9, align 4, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %1, i64 8
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %0, i64 24
  store i32 %11, ptr %12, align 4, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %1, i64 12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %0, i64 28
  store i32 %14, ptr %15, align 4, !tbaa !5
  %16 = icmp eq i32 %2, 256
  %.026.idx = select i1 %16, i64 16, i64 0
  %.026 = getelementptr inbounds i8, ptr %1, i64 %.026.idx
  %17 = load i32, ptr %.026, align 4, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %0, i64 32
  store i32 %17, ptr %18, align 4, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %.026, i64 4
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %0, i64 36
  store i32 %20, ptr %21, align 4, !tbaa !5
  %22 = getelementptr inbounds i8, ptr %.026, i64 8
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %0, i64 40
  store i32 %23, ptr %24, align 4, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %.026, i64 12
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %0, i64 44
  store i32 %26, ptr %27, align 4, !tbaa !5
  %chacha_sigma.val = load i32, ptr @chacha_sigma, align 16
  %tau.val = load i32, ptr @tau, align 16
  %28 = select i1 %16, i32 %chacha_sigma.val, i32 %tau.val
  store i32 %28, ptr %0, align 4, !tbaa !5
  %.val = load i32, ptr getelementptr inbounds (i8, ptr @chacha_sigma, i64 4), align 4
  %.val27 = load i32, ptr getelementptr inbounds (i8, ptr @tau, i64 4), align 4
  %29 = select i1 %16, i32 %.val, i32 %.val27
  %30 = getelementptr inbounds i8, ptr %0, i64 4
  store i32 %29, ptr %30, align 4, !tbaa !5
  %.val28 = load i32, ptr getelementptr inbounds (i8, ptr @chacha_sigma, i64 8), align 8
  %.val29 = load i32, ptr getelementptr inbounds (i8, ptr @tau, i64 8), align 8
  %31 = select i1 %16, i32 %.val28, i32 %.val29
  %32 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %31, ptr %32, align 4, !tbaa !5
  %.val30 = load i32, ptr getelementptr inbounds (i8, ptr @chacha_sigma, i64 12), align 4
  %.val31 = load i32, ptr getelementptr inbounds (i8, ptr @tau, i64 12), align 4
  %33 = select i1 %16, i32 %.val30, i32 %.val31
  %34 = getelementptr inbounds i8, ptr %0, i64 12
  store i32 %33, ptr %34, align 4, !tbaa !5
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass