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

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

@.str.10 = external hidden unnamed_addr constant [27 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [25 x i8], align 1

; 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
define dso_local i32 @mbedtls_pk_parse_public_key(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 i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca %struct.mbedtls_pem_context, align 8
  %12 = 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 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 24, ptr %11) #3
  call void @mbedtls_pem_init(ptr noundef %11)
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = icmp eq i64 %13, 0
  br i1 %14, label %23, label %15

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

23:                                               ; preds = %15, %3
  store i32 -4224, ptr %8, align 4, !tbaa !12
  br label %27

24:                                               ; preds = %15
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = call i32 @mbedtls_pem_read_buffer(ptr noundef %11, ptr noundef @.str.10, ptr noundef @.str.11, ptr noundef %25, ptr noundef null, i64 noundef 0, ptr noundef %10)
  store i32 %26, ptr %8, align 4, !tbaa !12
  br label %27

27:                                               ; preds = %24, %23
  %28 = load i32, ptr %8, align 4, !tbaa !12
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %30, label %35

30:                                               ; preds = %27
  %31 = getelementptr inbounds %struct.mbedtls_pem_context, ptr %11, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !14
  store ptr %32, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.mbedtls_pem_context, ptr %11, i32 0, i32 1
  %34 = load i64, ptr %33, align 8, !tbaa !16
  store i64 %34, ptr %7, align 8, !tbaa !9
  br label %41

35:                                               ; preds = %27
  %36 = load i32, ptr %8, align 4, !tbaa !12
  %37 = icmp ne i32 %36, -4224
  br i1 %37, label %38, label %40

38:                                               ; preds = %35
  call void @mbedtls_pem_free(ptr noundef %11)
  %39 = load i32, ptr %8, align 4, !tbaa !12
  store i32 %39, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %49

40:                                               ; preds = %35
  br label %41

41:                                               ; preds = %40, %30
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %42, ptr %9, align 8, !tbaa !5
  %43 = load ptr, ptr %9, align 8, !tbaa !5
  %44 = load i64, ptr %7, align 8, !tbaa !9
  %45 = getelementptr inbounds i8, ptr %43, i64 %44
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = call i32 @mbedtls_pk_parse_subpubkey(ptr noundef %9, ptr noundef %45, ptr noundef %46)
  store i32 %47, ptr %8, align 4, !tbaa !12
  call void @mbedtls_pem_free(ptr noundef %11)
  %48 = load i32, ptr %8, align 4, !tbaa !12
  store i32 %48, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %49

49:                                               ; preds = %41, %38
  call void @llvm.lifetime.end.p0(i64 24, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %50 = load i32, ptr %4, align 4
  ret i32 %50
}

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

declare void @mbedtls_pem_init(ptr noundef) #2

declare i32 @mbedtls_pem_read_buffer(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef) #2

declare void @mbedtls_pem_free(ptr noundef) #2

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 = !{!"int", !7, i64 0}
!14 = !{!15, !6, i64 0}
!15 = !{!"", !6, i64 0, !10, i64 8, !6, i64 16}
!16 = !{!15, !10, i64 8}


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

@.str.10 = external hidden unnamed_addr constant [27 x i8], align 1
@.str.11 = external hidden unnamed_addr constant [25 x i8], align 1

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_pk_parse_public_key(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca %struct.mbedtls_pem_context, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #3
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %6) #3
  call void @mbedtls_pem_init(ptr noundef nonnull %6) #3
  %7 = icmp eq i64 %2, 0
  br i1 %7, label %.thread, label %8

8:                                                ; preds = %3
  %9 = getelementptr i8, ptr %1, i64 %2
  %10 = getelementptr i8, ptr %9, i64 -1
  %11 = load i8, ptr %10, align 1, !tbaa !5
  %.not = icmp eq i8 %11, 0
  br i1 %.not, label %12, label %.thread

12:                                               ; preds = %8
  %13 = call i32 @mbedtls_pem_read_buffer(ptr noundef nonnull %6, ptr noundef nonnull @.str.10, ptr noundef nonnull @.str.11, ptr noundef nonnull %1, ptr noundef null, i64 noundef 0, ptr noundef nonnull %5) #3
  switch i32 %13, label %20 [
    i32 0, label %14
    i32 -4224, label %.thread
  ]

14:                                               ; preds = %12
  %15 = load ptr, ptr %6, align 8, !tbaa !8
  %16 = getelementptr inbounds i8, ptr %6, i64 8
  %17 = load i64, ptr %16, align 8, !tbaa !12
  br label %.thread

.thread:                                          ; preds = %3, %8, %12, %14
  %.012 = phi ptr [ %15, %14 ], [ %1, %12 ], [ %1, %8 ], [ %1, %3 ]
  %.011 = phi i64 [ %17, %14 ], [ %2, %12 ], [ %2, %8 ], [ 0, %3 ]
  store ptr %.012, ptr %4, align 8, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %.012, i64 %.011
  %19 = call i32 @mbedtls_pk_parse_subpubkey(ptr noundef nonnull %4, ptr noundef %18, ptr noundef %0)
  br label %20

20:                                               ; preds = %12, %.thread
  %.013 = phi i32 [ %19, %.thread ], [ %13, %12 ]
  call void @mbedtls_pem_free(ptr noundef nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  ret i32 %.013
}

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

declare void @mbedtls_pem_init(ptr noundef) local_unnamed_addr #2

declare i32 @mbedtls_pem_read_buffer(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #2

declare void @mbedtls_pem_free(ptr noundef) local_unnamed_addr #2

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


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