; LLVM IR from ARMmbed/mbed-crypto :: library (chachapoly)

; 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.mbedtls_chachapoly_context = type { %struct.mbedtls_chacha20_context, %struct.mbedtls_poly1305_context, i64, i64, i32, i32 }
%struct.mbedtls_chacha20_context = type { [16 x i32], [64 x i8], i64 }
%struct.mbedtls_poly1305_context = type { [4 x i32], [4 x i32], [5 x i32], [16 x i8], i64 }

; 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

declare i32 @mbedtls_chacha20_update(ptr noundef, i64 noundef, ptr noundef, ptr noundef) #1

declare i32 @mbedtls_poly1305_update(ptr noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_chachapoly_update(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) #2 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, 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 i64 %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 -110, ptr %10, align 4, !tbaa !11
  br label %12

12:                                               ; preds = %4
  br label %13

13:                                               ; preds = %12
  br label %14

14:                                               ; preds = %13
  br label %15

15:                                               ; preds = %14
  br label %16

16:                                               ; preds = %15
  br label %17

17:                                               ; preds = %16
  br label %18

18:                                               ; preds = %17
  br label %19

19:                                               ; preds = %18
  br label %20

20:                                               ; preds = %19
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.mbedtls_chachapoly_context, ptr %21, i32 0, i32 4
  %23 = load i32, ptr %22, align 8, !tbaa !13
  %24 = icmp ne i32 %23, 1
  br i1 %24, label %25, label %31

25:                                               ; preds = %20
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.mbedtls_chachapoly_context, ptr %26, i32 0, i32 4
  %28 = load i32, ptr %27, align 8, !tbaa !13
  %29 = icmp ne i32 %28, 2
  br i1 %29, label %30, label %31

30:                                               ; preds = %25
  store i32 -84, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %101

31:                                               ; preds = %25, %20
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.mbedtls_chachapoly_context, ptr %32, i32 0, i32 4
  %34 = load i32, ptr %33, align 8, !tbaa !13
  %35 = icmp eq i32 %34, 1
  br i1 %35, label %36, label %46

36:                                               ; preds = %31
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.mbedtls_chachapoly_context, ptr %37, i32 0, i32 4
  store i32 2, ptr %38, align 8, !tbaa !13
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = call i32 @chachapoly_pad_aad(ptr noundef %39)
  store i32 %40, ptr %10, align 4, !tbaa !11
  %41 = load i32, ptr %10, align 4, !tbaa !11
  %42 = icmp ne i32 %41, 0
  br i1 %42, label %43, label %45

43:                                               ; preds = %36
  %44 = load i32, ptr %10, align 4, !tbaa !11
  store i32 %44, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %101

45:                                               ; preds = %36
  br label %46

46:                                               ; preds = %45, %31
  %47 = load i64, ptr %7, align 8, !tbaa !9
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.mbedtls_chachapoly_context, ptr %48, i32 0, i32 3
  %50 = load i64, ptr %49, align 8, !tbaa !17
  %51 = add i64 %50, %47
  store i64 %51, ptr %49, align 8, !tbaa !17
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.mbedtls_chachapoly_context, ptr %52, i32 0, i32 5
  %54 = load i32, ptr %53, align 4, !tbaa !18
  %55 = icmp eq i32 %54, 0
  br i1 %55, label %56, label %78

56:                                               ; preds = %46
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.mbedtls_chachapoly_context, ptr %57, i32 0, i32 0
  %59 = load i64, ptr %7, align 8, !tbaa !9
  %60 = load ptr, ptr %8, align 8, !tbaa !5
  %61 = load ptr, ptr %9, align 8, !tbaa !5
  %62 = call i32 @mbedtls_chacha20_update(ptr noundef %58, i64 noundef %59, ptr noundef %60, ptr noundef %61)
  store i32 %62, ptr %10, align 4, !tbaa !11
  %63 = load i32, ptr %10, align 4, !tbaa !11
  %64 = icmp ne i32 %63, 0
  br i1 %64, label %65, label %67

65:                                               ; preds = %56
  %66 = load i32, ptr %10, align 4, !tbaa !11
  store i32 %66, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %101

67:                                               ; preds = %56
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.mbedtls_chachapoly_context, ptr %68, i32 0, i32 1
  %70 = load ptr, ptr %9, align 8, !tbaa !5
  %71 = load i64, ptr %7, align 8, !tbaa !9
  %72 = call i32 @mbedtls_poly1305_update(ptr noundef %69, ptr noundef %70, i64 noundef %71)
  store i32 %72, ptr %10, align 4, !tbaa !11
  %73 = load i32, ptr %10, align 4, !tbaa !11
  %74 = icmp ne i32 %73, 0
  br i1 %74, label %75, label %77

75:                                               ; preds = %67
  %76 = load i32, ptr %10, align 4, !tbaa !11
  store i32 %76, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %101

77:                                               ; preds = %67
  br label %100

78:                                               ; preds = %46
  %79 = load ptr, ptr %6, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.mbedtls_chachapoly_context, ptr %79, i32 0, i32 1
  %81 = load ptr, ptr %8, align 8, !tbaa !5
  %82 = load i64, ptr %7, align 8, !tbaa !9
  %83 = call i32 @mbedtls_poly1305_update(ptr noundef %80, ptr noundef %81, i64 noundef %82)
  store i32 %83, ptr %10, align 4, !tbaa !11
  %84 = load i32, ptr %10, align 4, !tbaa !11
  %85 = icmp ne i32 %84, 0
  br i1 %85, label %86, label %88

86:                                               ; preds = %78
  %87 = load i32, ptr %10, align 4, !tbaa !11
  store i32 %87, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %101

88:                                               ; preds = %78
  %89 = load ptr, ptr %6, align 8, !tbaa !5
  %90 = getelementptr inbounds %struct.mbedtls_chachapoly_context, ptr %89, i32 0, i32 0
  %91 = load i64, ptr %7, align 8, !tbaa !9
  %92 = load ptr, ptr %8, align 8, !tbaa !5
  %93 = load ptr, ptr %9, align 8, !tbaa !5
  %94 = call i32 @mbedtls_chacha20_update(ptr noundef %90, i64 noundef %91, ptr noundef %92, ptr noundef %93)
  store i32 %94, ptr %10, align 4, !tbaa !11
  %95 = load i32, ptr %10, align 4, !tbaa !11
  %96 = icmp ne i32 %95, 0
  br i1 %96, label %97, label %99

97:                                               ; preds = %88
  %98 = load i32, ptr %10, align 4, !tbaa !11
  store i32 %98, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %101

99:                                               ; preds = %88
  br label %100

100:                                              ; preds = %99, %77
  store i32 0, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %101

101:                                              ; preds = %100, %97, %86, %75, %65, %43, %30
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %102 = load i32, ptr %5, align 4
  ret i32 %102
}

; Function Attrs: nounwind uwtable
declare hidden i32 @chachapoly_pad_aad(ptr noundef) #2

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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 232}
!14 = !{!"mbedtls_chachapoly_context", !15, i64 0, !16, i64 136, !10, i64 216, !10, i64 224, !12, i64 232, !12, i64 236}
!15 = !{!"mbedtls_chacha20_context", !7, i64 0, !7, i64 64, !10, i64 128}
!16 = !{!"mbedtls_poly1305_context", !7, i64 0, !7, i64 16, !7, i64 32, !7, i64 52, !10, i64 72}
!17 = !{!14, !10, i64 224}
!18 = !{!14, !12, i64 236}


; 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 @mbedtls_chacha20_update(ptr noundef, i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

declare i32 @mbedtls_poly1305_update(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_chachapoly_update(ptr noundef %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #1 {
  %5 = getelementptr inbounds i8, ptr %0, i64 232
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %.off = add i32 %6, -1
  %switch = icmp ult i32 %.off, 2
  br i1 %switch, label %7, label %29

7:                                                ; preds = %4
  %8 = icmp eq i32 %6, 1
  br i1 %8, label %9, label %11

9:                                                ; preds = %7
  store i32 2, ptr %5, align 8, !tbaa !5
  %10 = tail call i32 @chachapoly_pad_aad(ptr noundef nonnull %0)
  %.not39 = icmp eq i32 %10, 0
  br i1 %.not39, label %11, label %29

11:                                               ; preds = %9, %7
  %12 = getelementptr inbounds i8, ptr %0, i64 224
  %13 = load i64, ptr %12, align 8, !tbaa !13
  %14 = add i64 %13, %1
  store i64 %14, ptr %12, align 8, !tbaa !13
  %15 = getelementptr inbounds i8, ptr %0, i64 236
  %16 = load i32, ptr %15, align 4, !tbaa !14
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %23

18:                                               ; preds = %11
  %19 = tail call i32 @mbedtls_chacha20_update(ptr noundef nonnull %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) #2
  %.not42 = icmp eq i32 %19, 0
  br i1 %.not42, label %20, label %29

20:                                               ; preds = %18
  %21 = getelementptr inbounds i8, ptr %0, i64 136
  %22 = tail call i32 @mbedtls_poly1305_update(ptr noundef nonnull %21, ptr noundef %3, i64 noundef %1) #2
  %.not43 = icmp eq i32 %22, 0
  br i1 %.not43, label %28, label %29

23:                                               ; preds = %11
  %24 = getelementptr inbounds i8, ptr %0, i64 136
  %25 = tail call i32 @mbedtls_poly1305_update(ptr noundef nonnull %24, ptr noundef %2, i64 noundef %1) #2
  %.not40 = icmp eq i32 %25, 0
  br i1 %.not40, label %26, label %29

26:                                               ; preds = %23
  %27 = tail call i32 @mbedtls_chacha20_update(ptr noundef nonnull %0, i64 noundef %1, ptr noundef %2, ptr noundef %3) #2
  %.not41 = icmp eq i32 %27, 0
  br i1 %.not41, label %28, label %29

28:                                               ; preds = %26, %20
  br label %29

29:                                               ; preds = %4, %26, %23, %20, %18, %9, %28
  %.0 = phi i32 [ 0, %28 ], [ -84, %4 ], [ %10, %9 ], [ %19, %18 ], [ %22, %20 ], [ %25, %23 ], [ %27, %26 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden i32 @chachapoly_pad_aad(ptr noundef) local_unnamed_addr #1

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, !12, i64 232}
!6 = !{!"mbedtls_chachapoly_context", !7, i64 0, !11, i64 136, !10, i64 216, !10, i64 224, !12, i64 232, !12, i64 236}
!7 = !{!"mbedtls_chacha20_context", !8, i64 0, !8, i64 64, !10, i64 128}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"mbedtls_poly1305_context", !8, i64 0, !8, i64 16, !8, i64 32, !8, i64 52, !10, i64 72}
!12 = !{!"int", !8, i64 0}
!13 = !{!6, !10, i64 224}
!14 = !{!6, !12, i64 236}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass