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

; 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_entropy_context = type { i32, %struct.mbedtls_sha512_context, i32, [20 x %struct.mbedtls_entropy_source_state] }
%struct.mbedtls_sha512_context = type { [2 x i64], [8 x i64], [128 x i8], i32 }
%struct.mbedtls_entropy_source_state = type { ptr, ptr, i64, i64, i32 }

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_entropy_add_source(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, i32 noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !9
  store i32 %4, ptr %10, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !11
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.mbedtls_entropy_context, ptr %13, i32 0, i32 2
  %15 = load i32, ptr %14, align 8, !tbaa !13
  store i32 %15, ptr %11, align 4, !tbaa !11
  %16 = load i32, ptr %11, align 4, !tbaa !11
  %17 = icmp sge i32 %16, 20
  br i1 %17, label %18, label %19

18:                                               ; preds = %5
  store i32 -62, ptr %12, align 4, !tbaa !11
  br label %52

19:                                               ; preds = %5
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.mbedtls_entropy_context, ptr %21, i32 0, i32 3
  %23 = load i32, ptr %11, align 4, !tbaa !11
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [20 x %struct.mbedtls_entropy_source_state], ptr %22, i64 0, i64 %24
  %26 = getelementptr inbounds %struct.mbedtls_entropy_source_state, ptr %25, i32 0, i32 0
  store ptr %20, ptr %26, align 8, !tbaa !16
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.mbedtls_entropy_context, ptr %28, i32 0, i32 3
  %30 = load i32, ptr %11, align 4, !tbaa !11
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [20 x %struct.mbedtls_entropy_source_state], ptr %29, i64 0, i64 %31
  %33 = getelementptr inbounds %struct.mbedtls_entropy_source_state, ptr %32, i32 0, i32 1
  store ptr %27, ptr %33, align 8, !tbaa !18
  %34 = load i64, ptr %9, align 8, !tbaa !9
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.mbedtls_entropy_context, ptr %35, i32 0, i32 3
  %37 = load i32, ptr %11, align 4, !tbaa !11
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [20 x %struct.mbedtls_entropy_source_state], ptr %36, i64 0, i64 %38
  %40 = getelementptr inbounds %struct.mbedtls_entropy_source_state, ptr %39, i32 0, i32 3
  store i64 %34, ptr %40, align 8, !tbaa !19
  %41 = load i32, ptr %10, align 4, !tbaa !11
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.mbedtls_entropy_context, ptr %42, i32 0, i32 3
  %44 = load i32, ptr %11, align 4, !tbaa !11
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [20 x %struct.mbedtls_entropy_source_state], ptr %43, i64 0, i64 %45
  %47 = getelementptr inbounds %struct.mbedtls_entropy_source_state, ptr %46, i32 0, i32 4
  store i32 %41, ptr %47, align 8, !tbaa !20
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.mbedtls_entropy_context, ptr %48, i32 0, i32 2
  %50 = load i32, ptr %49, align 8, !tbaa !13
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %49, align 8, !tbaa !13
  br label %52

52:                                               ; preds = %19, %18
  %53 = load i32, ptr %12, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  ret i32 %53
}

; 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 224}
!14 = !{!"mbedtls_entropy_context", !12, i64 0, !15, i64 8, !12, i64 224, !7, i64 232}
!15 = !{!"mbedtls_sha512_context", !7, i64 0, !7, i64 16, !7, i64 80, !12, i64 208}
!16 = !{!17, !6, i64 0}
!17 = !{!"mbedtls_entropy_source_state", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 24, !12, i64 32}
!18 = !{!17, !6, i64 8}
!19 = !{!17, !10, i64 24}
!20 = !{!17, !12, i64 32}


; 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_entropy_source_state = type { ptr, ptr, i64, i64, i32 }

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite) uwtable
define dso_local range(i32 -62, 1) i32 @mbedtls_entropy_add_source(ptr nocapture noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = getelementptr inbounds i8, ptr %0, i64 224
  %7 = load i32, ptr %6, align 8, !tbaa !5
  %8 = icmp sgt i32 %7, 19
  br i1 %8, label %17, label %9

9:                                                ; preds = %5
  %10 = getelementptr inbounds i8, ptr %0, i64 232
  %11 = sext i32 %7 to i64
  %12 = getelementptr inbounds [20 x %struct.mbedtls_entropy_source_state], ptr %10, i64 0, i64 %11
  store ptr %1, ptr %12, align 8, !tbaa !11
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  store ptr %2, ptr %13, align 8, !tbaa !15
  %14 = getelementptr inbounds i8, ptr %12, i64 24
  store i64 %3, ptr %14, align 8, !tbaa !16
  %15 = getelementptr inbounds i8, ptr %12, i64 32
  store i32 %4, ptr %15, align 8, !tbaa !17
  %16 = add nsw i32 %7, 1
  store i32 %16, ptr %6, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %5, %9
  %.0 = phi i32 [ 0, %9 ], [ -62, %5 ]
  ret i32 %.0
}

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, !7, i64 224}
!6 = !{!"mbedtls_entropy_context", !7, i64 0, !10, i64 8, !7, i64 224, !8, i64 232}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"mbedtls_sha512_context", !8, i64 0, !8, i64 16, !8, i64 80, !7, i64 208}
!11 = !{!12, !13, i64 0}
!12 = !{!"mbedtls_entropy_source_state", !13, i64 0, !13, i64 8, !14, i64 16, !14, i64 24, !7, i64 32}
!13 = !{!"any pointer", !8, i64 0}
!14 = !{!"long", !8, i64 0}
!15 = !{!12, !13, i64 8}
!16 = !{!12, !14, i64 24}
!17 = !{!12, !7, i64 32}


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