; LLVM IR from tleonhardt/practical_cryptography_engineering :: mbedtls (ecp)

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

; 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

; Function Attrs: nounwind uwtable
declare dso_local ptr @mbedtls_ecp_curve_info_from_tls_id(i16 noundef zeroext) #1

declare i32 @mbedtls_ecp_group_load(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ecp_tls_read_group(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i16, align 2
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %11 = load i64, ptr %7, align 8, !tbaa !9
  %12 = icmp ult i64 %11, 3
  br i1 %12, label %13, label %14

13:                                               ; preds = %3
  store i32 -20352, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %51

14:                                               ; preds = %3
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = load ptr, ptr %15, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %16, i32 1
  store ptr %17, ptr %15, align 8, !tbaa !5
  %18 = load i8, ptr %16, align 1, !tbaa !11
  %19 = zext i8 %18 to i32
  %20 = icmp ne i32 %19, 3
  br i1 %20, label %21, label %22

21:                                               ; preds = %14
  store i32 -20352, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %51

22:                                               ; preds = %14
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = load ptr, ptr %23, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %24, i32 1
  store ptr %25, ptr %23, align 8, !tbaa !5
  %26 = load i8, ptr %24, align 1, !tbaa !11
  %27 = zext i8 %26 to i16
  store i16 %27, ptr %8, align 2, !tbaa !12
  %28 = load i16, ptr %8, align 2, !tbaa !12
  %29 = zext i16 %28 to i32
  %30 = shl i32 %29, 8
  %31 = trunc i32 %30 to i16
  store i16 %31, ptr %8, align 2, !tbaa !12
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = load ptr, ptr %32, align 8, !tbaa !5
  %34 = getelementptr inbounds i8, ptr %33, i32 1
  store ptr %34, ptr %32, align 8, !tbaa !5
  %35 = load i8, ptr %33, align 1, !tbaa !11
  %36 = zext i8 %35 to i32
  %37 = load i16, ptr %8, align 2, !tbaa !12
  %38 = zext i16 %37 to i32
  %39 = or i32 %38, %36
  %40 = trunc i32 %39 to i16
  store i16 %40, ptr %8, align 2, !tbaa !12
  %41 = load i16, ptr %8, align 2, !tbaa !12
  %42 = call ptr @mbedtls_ecp_curve_info_from_tls_id(i16 noundef zeroext %41)
  store ptr %42, ptr %9, align 8, !tbaa !5
  %43 = icmp eq ptr %42, null
  br i1 %43, label %44, label %45

44:                                               ; preds = %22
  store i32 -20096, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %51

45:                                               ; preds = %22
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = load ptr, ptr %9, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.mbedtls_ecp_curve_info, ptr %47, i32 0, i32 0
  %49 = load i32, ptr %48, align 8, !tbaa !14
  %50 = call i32 @mbedtls_ecp_group_load(ptr noundef %46, i32 noundef %49)
  store i32 %50, ptr %4, align 4
  store i32 1, ptr %10, align 4
  br label %51

51:                                               ; preds = %45, %44, %21, %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #3
  %52 = load i32, ptr %4, align 4
  ret i32 %52
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!15, !16, i64 0}
!15 = !{!"", !16, i64 0, !13, i64 4, !13, i64 6, !6, i64 8}
!16 = !{!"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"

; Function Attrs: nounwind uwtable
declare dso_local ptr @mbedtls_ecp_curve_info_from_tls_id(i16 noundef zeroext) local_unnamed_addr #0

declare i32 @mbedtls_ecp_group_load(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ecp_tls_read_group(ptr noundef %0, ptr nocapture noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = icmp ult i64 %2, 3
  br i1 %4, label %23, label %5

5:                                                ; preds = %3
  %6 = load ptr, ptr %1, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %6, i64 1
  store ptr %7, ptr %1, align 8, !tbaa !5
  %8 = load i8, ptr %6, align 1, !tbaa !9
  %.not = icmp eq i8 %8, 3
  br i1 %.not, label %9, label %23

9:                                                ; preds = %5
  %10 = getelementptr inbounds i8, ptr %6, i64 2
  store ptr %10, ptr %1, align 8, !tbaa !5
  %11 = load i8, ptr %7, align 1, !tbaa !9
  %12 = zext i8 %11 to i16
  %13 = shl nuw i16 %12, 8
  %14 = getelementptr inbounds i8, ptr %6, i64 3
  store ptr %14, ptr %1, align 8, !tbaa !5
  %15 = load i8, ptr %10, align 1, !tbaa !9
  %16 = zext i8 %15 to i16
  %17 = or disjoint i16 %13, %16
  %18 = tail call ptr @mbedtls_ecp_curve_info_from_tls_id(i16 noundef zeroext %17)
  %19 = icmp eq ptr %18, null
  br i1 %19, label %23, label %20

20:                                               ; preds = %9
  %21 = load i32, ptr %18, align 8, !tbaa !10
  %22 = tail call i32 @mbedtls_ecp_group_load(ptr noundef %0, i32 noundef %21) #2
  br label %23

23:                                               ; preds = %9, %5, %3, %20
  %.0 = phi i32 [ %22, %20 ], [ -20352, %3 ], [ -20352, %5 ], [ -20096, %9 ]
  ret i32 %.0
}

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 = { "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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !12, i64 0}
!11 = !{!"", !12, i64 0, !13, i64 4, !13, i64 6, !6, i64 8}
!12 = !{!"int", !7, i64 0}
!13 = !{!"short", !7, i64 0}


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