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

@.str = external hidden unnamed_addr constant [29 x i8], align 1
@test_key = external hidden constant [1 x [32 x i8]], align 16
@.str.1 = external hidden unnamed_addr constant [25 x i8], align 1
@test_input_len = external hidden constant [1 x i64], align 8
@test_nonce = external hidden constant [1 x [12 x i8]], align 1
@test_aad = external hidden constant [1 x [12 x i8]], align 1
@test_aad_len = external hidden constant [1 x i64], align 8
@test_input = external hidden constant [1 x [114 x i8]], align 16
@.str.2 = external hidden unnamed_addr constant [32 x i8], align 1
@test_output = external hidden constant [1 x [114 x i8]], align 16
@.str.3 = external hidden unnamed_addr constant [24 x i8], align 1
@test_mac = external hidden constant [1 x [16 x i8]], align 16
@.str.4 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_chachapoly_init(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_chachapoly_free(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_chachapoly_setkey(ptr noundef, ptr noundef) #0

; 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

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_chachapoly_encrypt_and_tag(ptr noundef, i64 noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_chachapoly_self_test(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca %struct.mbedtls_chachapoly_context, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca [200 x i8], align 16
  %8 = alloca [16 x i8], align 16
  %9 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 240, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 -110, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 200, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #4
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %117, %1
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = icmp ult i32 %11, 1
  br i1 %12, label %13, label %120

13:                                               ; preds = %10
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %19

16:                                               ; preds = %13
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %17)
  br label %19

19:                                               ; preds = %16, %13
  call void @mbedtls_chachapoly_init(ptr noundef %4)
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = zext i32 %20 to i64
  %22 = getelementptr inbounds [1 x [32 x i8]], ptr @test_key, i64 0, i64 %21
  %23 = getelementptr inbounds [32 x i8], ptr %22, i64 0, i64 0
  %24 = call i32 @mbedtls_chachapoly_setkey(ptr noundef %4, ptr noundef %23)
  store i32 %24, ptr %6, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %19
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = icmp eq i32 0, %26
  br i1 %27, label %35, label %28

28:                                               ; preds = %25
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %34

31:                                               ; preds = %28
  %32 = load i32, ptr %6, align 4, !tbaa !5
  %33 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %32)
  br label %34

34:                                               ; preds = %31, %28
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %9, align 4
  br label %126

35:                                               ; preds = %25
  br label %36

36:                                               ; preds = %35
  br label %37

37:                                               ; preds = %36
  %38 = load i32, ptr %5, align 4, !tbaa !5
  %39 = zext i32 %38 to i64
  %40 = getelementptr inbounds [1 x i64], ptr @test_input_len, i64 0, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !9
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = zext i32 %42 to i64
  %44 = getelementptr inbounds [1 x [12 x i8]], ptr @test_nonce, i64 0, i64 %43
  %45 = getelementptr inbounds [12 x i8], ptr %44, i64 0, i64 0
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = zext i32 %46 to i64
  %48 = getelementptr inbounds [1 x [12 x i8]], ptr @test_aad, i64 0, i64 %47
  %49 = getelementptr inbounds [12 x i8], ptr %48, i64 0, i64 0
  %50 = load i32, ptr %5, align 4, !tbaa !5
  %51 = zext i32 %50 to i64
  %52 = getelementptr inbounds [1 x i64], ptr @test_aad_len, i64 0, i64 %51
  %53 = load i64, ptr %52, align 8, !tbaa !9
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = zext i32 %54 to i64
  %56 = getelementptr inbounds [1 x [114 x i8]], ptr @test_input, i64 0, i64 %55
  %57 = getelementptr inbounds [114 x i8], ptr %56, i64 0, i64 0
  %58 = getelementptr inbounds [200 x i8], ptr %7, i64 0, i64 0
  %59 = getelementptr inbounds [16 x i8], ptr %8, i64 0, i64 0
  %60 = call i32 @mbedtls_chachapoly_encrypt_and_tag(ptr noundef %4, i64 noundef %41, ptr noundef %45, ptr noundef %49, i64 noundef %53, ptr noundef %57, ptr noundef %58, ptr noundef %59)
  store i32 %60, ptr %6, align 4, !tbaa !5
  br label %61

61:                                               ; preds = %37
  %62 = load i32, ptr %6, align 4, !tbaa !5
  %63 = icmp eq i32 0, %62
  br i1 %63, label %71, label %64

64:                                               ; preds = %61
  %65 = load i32, ptr %3, align 4, !tbaa !5
  %66 = icmp ne i32 %65, 0
  br i1 %66, label %67, label %70

67:                                               ; preds = %64
  %68 = load i32, ptr %6, align 4, !tbaa !5
  %69 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i32 noundef %68)
  br label %70

70:                                               ; preds = %67, %64
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %9, align 4
  br label %126

71:                                               ; preds = %61
  br label %72

72:                                               ; preds = %71
  br label %73

73:                                               ; preds = %72
  br label %74

74:                                               ; preds = %73
  %75 = getelementptr inbounds [200 x i8], ptr %7, i64 0, i64 0
  %76 = load i32, ptr %5, align 4, !tbaa !5
  %77 = zext i32 %76 to i64
  %78 = getelementptr inbounds [1 x [114 x i8]], ptr @test_output, i64 0, i64 %77
  %79 = getelementptr inbounds [114 x i8], ptr %78, i64 0, i64 0
  %80 = load i32, ptr %5, align 4, !tbaa !5
  %81 = zext i32 %80 to i64
  %82 = getelementptr inbounds [1 x i64], ptr @test_input_len, i64 0, i64 %81
  %83 = load i64, ptr %82, align 8, !tbaa !9
  %84 = call i32 @memcmp(ptr noundef %75, ptr noundef %79, i64 noundef %83) #5
  %85 = icmp eq i32 0, %84
  br i1 %85, label %92, label %86

86:                                               ; preds = %74
  %87 = load i32, ptr %3, align 4, !tbaa !5
  %88 = icmp ne i32 %87, 0
  br i1 %88, label %89, label %91

89:                                               ; preds = %86
  %90 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  br label %91

91:                                               ; preds = %89, %86
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %9, align 4
  br label %126

92:                                               ; preds = %74
  br label %93

93:                                               ; preds = %92
  br label %94

94:                                               ; preds = %93
  br label %95

95:                                               ; preds = %94
  %96 = getelementptr inbounds [16 x i8], ptr %8, i64 0, i64 0
  %97 = load i32, ptr %5, align 4, !tbaa !5
  %98 = zext i32 %97 to i64
  %99 = getelementptr inbounds [1 x [16 x i8]], ptr @test_mac, i64 0, i64 %98
  %100 = getelementptr inbounds [16 x i8], ptr %99, i64 0, i64 0
  %101 = call i32 @memcmp(ptr noundef %96, ptr noundef %100, i64 noundef 16) #5
  %102 = icmp eq i32 0, %101
  br i1 %102, label %109, label %103

103:                                              ; preds = %95
  %104 = load i32, ptr %3, align 4, !tbaa !5
  %105 = icmp ne i32 %104, 0
  br i1 %105, label %106, label %108

106:                                              ; preds = %103
  %107 = call i32 (ptr, ...) @printf(ptr noundef @.str.4)
  br label %108

108:                                              ; preds = %106, %103
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %9, align 4
  br label %126

109:                                              ; preds = %95
  br label %110

110:                                              ; preds = %109
  br label %111

111:                                              ; preds = %110
  call void @mbedtls_chachapoly_free(ptr noundef %4)
  %112 = load i32, ptr %3, align 4, !tbaa !5
  %113 = icmp ne i32 %112, 0
  br i1 %113, label %114, label %116

114:                                              ; preds = %111
  %115 = call i32 (ptr, ...) @printf(ptr noundef @.str.5)
  br label %116

116:                                              ; preds = %114, %111
  br label %117

117:                                              ; preds = %116
  %118 = load i32, ptr %5, align 4, !tbaa !5
  %119 = add i32 %118, 1
  store i32 %119, ptr %5, align 4, !tbaa !5
  br label %10, !llvm.loop !11

120:                                              ; preds = %10
  %121 = load i32, ptr %3, align 4, !tbaa !5
  %122 = icmp ne i32 %121, 0
  br i1 %122, label %123, label %125

123:                                              ; preds = %120
  %124 = call i32 (ptr, ...) @printf(ptr noundef @.str.6)
  br label %125

125:                                              ; preds = %123, %120
  store i32 0, ptr %2, align 4
  store i32 1, ptr %9, align 4
  br label %126

126:                                              ; preds = %125, %108, %91, %70, %34
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 200, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 240, ptr %4) #4
  %127 = load i32, ptr %2, align 4
  ret i32 %127
}

declare i32 @printf(ptr noundef, ...) #2

; Function Attrs: nounwind willreturn memory(read)
declare i32 @memcmp(ptr noundef, ptr noundef, i64 noundef) #3

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 = { "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 willreturn memory(read) "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 }
attributes #5 = { nounwind willreturn memory(read) }

!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"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

%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 }

@.str = external hidden unnamed_addr constant [29 x i8], align 1
@test_key = external hidden constant [1 x [32 x i8]], align 16
@.str.1 = external hidden unnamed_addr constant [25 x i8], align 1
@test_input_len = external hidden local_unnamed_addr constant [1 x i64], align 8
@test_nonce = external hidden constant [1 x [12 x i8]], align 1
@test_aad = external hidden constant [1 x [12 x i8]], align 1
@test_aad_len = external hidden local_unnamed_addr constant [1 x i64], align 8
@test_input = external hidden constant [1 x [114 x i8]], align 16
@.str.2 = external hidden unnamed_addr constant [32 x i8], align 1
@test_output = external hidden constant [1 x [114 x i8]], align 16
@.str.3 = external hidden unnamed_addr constant [24 x i8], align 1
@test_mac = external hidden constant [1 x [16 x i8]], align 16
@.str.4 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_chachapoly_init(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @mbedtls_chachapoly_free(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_chachapoly_setkey(ptr noundef, ptr noundef) local_unnamed_addr #0

; 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 nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @mbedtls_chachapoly_encrypt_and_tag(ptr noundef, i64 noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @mbedtls_chachapoly_self_test(i32 noundef %0) local_unnamed_addr #0 {
.critedge:
  %1 = alloca %struct.mbedtls_chachapoly_context, align 8
  %2 = alloca [200 x i8], align 16
  %3 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 240, ptr nonnull %1) #4
  call void @llvm.lifetime.start.p0(i64 200, ptr nonnull %2) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #4
  %.not27 = icmp eq i32 %0, 0
  br i1 %.not27, label %6, label %4

4:                                                ; preds = %.critedge
  %5 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef 0)
  br label %6

6:                                                ; preds = %4, %.critedge
  call void @mbedtls_chachapoly_init(ptr noundef nonnull %1)
  %7 = call i32 @mbedtls_chachapoly_setkey(ptr noundef nonnull %1, ptr noundef nonnull @test_key)
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %12, label %9

9:                                                ; preds = %6
  br i1 %.not27, label %.thread, label %10

10:                                               ; preds = %9
  %11 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %7)
  br label %.thread

12:                                               ; preds = %6
  %13 = load i64, ptr @test_input_len, align 8, !tbaa !5
  %14 = load i64, ptr @test_aad_len, align 8, !tbaa !5
  %15 = call i32 @mbedtls_chachapoly_encrypt_and_tag(ptr noundef nonnull %1, i64 noundef %13, ptr noundef nonnull @test_nonce, ptr noundef nonnull @test_aad, i64 noundef %14, ptr noundef nonnull @test_input, ptr noundef nonnull %2, ptr noundef nonnull %3)
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %20, label %17

17:                                               ; preds = %12
  br i1 %.not27, label %.thread, label %18

18:                                               ; preds = %17
  %19 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %15)
  br label %.thread

20:                                               ; preds = %12
  %bcmp = call i32 @bcmp(ptr nonnull %2, ptr nonnull @test_output, i64 %13)
  %21 = icmp eq i32 %bcmp, 0
  br i1 %21, label %25, label %22

22:                                               ; preds = %20
  br i1 %.not27, label %.thread, label %23

23:                                               ; preds = %22
  %24 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  br label %.thread

25:                                               ; preds = %20
  %bcmp28 = call i32 @bcmp(ptr noundef nonnull dereferenceable(16) %3, ptr noundef nonnull dereferenceable(16) @test_mac, i64 16)
  %26 = icmp eq i32 %bcmp28, 0
  br i1 %26, label %30, label %27

27:                                               ; preds = %25
  br i1 %.not27, label %.thread, label %28

28:                                               ; preds = %27
  %29 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4)
  br label %.thread

30:                                               ; preds = %25
  call void @mbedtls_chachapoly_free(ptr noundef nonnull %1)
  br i1 %.not27, label %.thread, label %31

31:                                               ; preds = %30
  %32 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5)
  %33 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6)
  br label %.thread

.thread:                                          ; preds = %30, %31, %27, %28, %22, %23, %17, %18, %9, %10
  %.024 = phi i32 [ -1, %10 ], [ -1, %9 ], [ -1, %18 ], [ -1, %17 ], [ -1, %23 ], [ -1, %22 ], [ -1, %28 ], [ -1, %27 ], [ 0, %31 ], [ 0, %30 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #4
  call void @llvm.lifetime.end.p0(i64 200, ptr nonnull %2) #4
  call void @llvm.lifetime.end.p0(i64 240, ptr nonnull %1) #4
  ret i32 %.024
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nofree nounwind willreturn memory(argmem: read)
declare i32 @bcmp(ptr nocapture, ptr nocapture, i64) local_unnamed_addr #3

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 = { nofree nounwind "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 = { nofree nounwind willreturn memory(argmem: read) }
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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


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