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

; 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_asn1_named_data = type { %struct.mbedtls_asn1_buf, %struct.mbedtls_asn1_buf, ptr, i8 }
%struct.mbedtls_asn1_buf = type { i32, i64, 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
define dso_local i32 @mbedtls_x509_write_names(ptr noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store i64 0, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %12 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %12, ptr %10, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %47, %3
  %14 = load ptr, ptr %10, align 8, !tbaa !5
  %15 = icmp ne ptr %14, null
  br i1 %15, label %16, label %51

16:                                               ; preds = %13
  br label %17

17:                                               ; preds = %16
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = load ptr, ptr %10, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %20, i32 0, i32 0
  %22 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %21, i32 0, i32 2
  %23 = load ptr, ptr %22, align 8, !tbaa !11
  %24 = load ptr, ptr %10, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %24, i32 0, i32 0
  %26 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %25, i32 0, i32 1
  %27 = load i64, ptr %26, align 8, !tbaa !15
  %28 = load ptr, ptr %10, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %28, i32 0, i32 1
  %30 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %29, i32 0, i32 2
  %31 = load ptr, ptr %30, align 8, !tbaa !16
  %32 = load ptr, ptr %10, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %32, i32 0, i32 1
  %34 = getelementptr inbounds %struct.mbedtls_asn1_buf, ptr %33, i32 0, i32 1
  %35 = load i64, ptr %34, align 8, !tbaa !17
  %36 = call i32 @x509_write_name(ptr noundef %18, ptr noundef %19, ptr noundef %23, i64 noundef %27, ptr noundef %31, i64 noundef %35)
  store i32 %36, ptr %8, align 4, !tbaa !18
  %37 = icmp slt i32 %36, 0
  br i1 %37, label %38, label %40

38:                                               ; preds = %17
  %39 = load i32, ptr %8, align 4, !tbaa !18
  store i32 %39, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %85

40:                                               ; preds = %17
  %41 = load i32, ptr %8, align 4, !tbaa !18
  %42 = sext i32 %41 to i64
  %43 = load i64, ptr %9, align 8, !tbaa !9
  %44 = add i64 %43, %42
  store i64 %44, ptr %9, align 8, !tbaa !9
  br label %45

45:                                               ; preds = %40
  br label %46

46:                                               ; preds = %45
  br label %47

47:                                               ; preds = %46
  %48 = load ptr, ptr %10, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.mbedtls_asn1_named_data, ptr %48, i32 0, i32 2
  %50 = load ptr, ptr %49, align 8, !tbaa !19
  store ptr %50, ptr %10, align 8, !tbaa !5
  br label %13, !llvm.loop !20

51:                                               ; preds = %13
  br label %52

52:                                               ; preds = %51
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = load i64, ptr %9, align 8, !tbaa !9
  %56 = call i32 @mbedtls_asn1_write_len(ptr noundef %53, ptr noundef %54, i64 noundef %55)
  store i32 %56, ptr %8, align 4, !tbaa !18
  %57 = icmp slt i32 %56, 0
  br i1 %57, label %58, label %60

58:                                               ; preds = %52
  %59 = load i32, ptr %8, align 4, !tbaa !18
  store i32 %59, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %85

60:                                               ; preds = %52
  %61 = load i32, ptr %8, align 4, !tbaa !18
  %62 = sext i32 %61 to i64
  %63 = load i64, ptr %9, align 8, !tbaa !9
  %64 = add i64 %63, %62
  store i64 %64, ptr %9, align 8, !tbaa !9
  br label %65

65:                                               ; preds = %60
  br label %66

66:                                               ; preds = %65
  br label %67

67:                                               ; preds = %66
  br label %68

68:                                               ; preds = %67
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = call i32 @mbedtls_asn1_write_tag(ptr noundef %69, ptr noundef %70, i8 noundef zeroext 48)
  store i32 %71, ptr %8, align 4, !tbaa !18
  %72 = icmp slt i32 %71, 0
  br i1 %72, label %73, label %75

73:                                               ; preds = %68
  %74 = load i32, ptr %8, align 4, !tbaa !18
  store i32 %74, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %85

75:                                               ; preds = %68
  %76 = load i32, ptr %8, align 4, !tbaa !18
  %77 = sext i32 %76 to i64
  %78 = load i64, ptr %9, align 8, !tbaa !9
  %79 = add i64 %78, %77
  store i64 %79, ptr %9, align 8, !tbaa !9
  br label %80

80:                                               ; preds = %75
  br label %81

81:                                               ; preds = %80
  br label %82

82:                                               ; preds = %81
  %83 = load i64, ptr %9, align 8, !tbaa !9
  %84 = trunc i64 %83 to i32
  store i32 %84, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %85

85:                                               ; preds = %82, %73, %58, %38
  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
  %86 = load i32, ptr %4, align 4
  ret i32 %86
}

; Function Attrs: nounwind uwtable
declare hidden i32 @x509_write_name(ptr noundef, ptr noundef, ptr noundef, i64 noundef, ptr noundef, i64 noundef) #1

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

declare i32 @mbedtls_asn1_write_tag(ptr noundef, ptr noundef, i8 noundef zeroext) #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 = !{!12, !6, i64 16}
!12 = !{!"mbedtls_asn1_named_data", !13, i64 0, !13, i64 24, !6, i64 48, !7, i64 56}
!13 = !{!"mbedtls_asn1_buf", !14, i64 0, !10, i64 8, !6, i64 16}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !10, i64 8}
!16 = !{!12, !6, i64 40}
!17 = !{!12, !10, i64 32}
!18 = !{!14, !14, i64 0}
!19 = !{!12, !6, i64 48}
!20 = distinct !{!20, !21}
!21 = !{!"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"

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_x509_write_names(ptr noundef %0, ptr noundef %1, ptr noundef readonly %2) local_unnamed_addr #0 {
  %.not32 = icmp eq ptr %2, null
  br i1 %.not32, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %14
  %.034 = phi ptr [ %18, %14 ], [ %2, %3 ]
  %.02433 = phi i64 [ %16, %14 ], [ 0, %3 ]
  %4 = getelementptr inbounds i8, ptr %.034, i64 16
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %.034, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !13
  %8 = getelementptr inbounds i8, ptr %.034, i64 40
  %9 = load ptr, ptr %8, align 8, !tbaa !14
  %10 = getelementptr inbounds i8, ptr %.034, i64 32
  %11 = load i64, ptr %10, align 8, !tbaa !15
  %12 = tail call i32 @x509_write_name(ptr noundef %0, ptr noundef %1, ptr noundef %5, i64 noundef %7, ptr noundef %9, i64 noundef %11)
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %.loopexit, label %14

14:                                               ; preds = %.lr.ph
  %15 = zext nneg i32 %12 to i64
  %16 = add i64 %.02433, %15
  %17 = getelementptr inbounds i8, ptr %.034, i64 48
  %18 = load ptr, ptr %17, align 8, !tbaa !16
  %.not = icmp eq ptr %18, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !17

._crit_edge:                                      ; preds = %14, %3
  %.024.lcssa = phi i64 [ 0, %3 ], [ %16, %14 ]
  %19 = tail call i32 @mbedtls_asn1_write_len(ptr noundef %0, ptr noundef %1, i64 noundef %.024.lcssa) #2
  %20 = icmp slt i32 %19, 0
  br i1 %20, label %.loopexit, label %21

21:                                               ; preds = %._crit_edge
  %22 = tail call i32 @mbedtls_asn1_write_tag(ptr noundef %0, ptr noundef %1, i8 noundef zeroext 48) #2
  %23 = icmp slt i32 %22, 0
  br i1 %23, label %.loopexit, label %24

24:                                               ; preds = %21
  %25 = trunc i64 %.024.lcssa to i32
  %26 = add i32 %19, %25
  %27 = add i32 %26, %22
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph, %21, %._crit_edge, %24
  %.025 = phi i32 [ %27, %24 ], [ %19, %._crit_edge ], [ %22, %21 ], [ %12, %.lr.ph ]
  ret i32 %.025
}

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

declare i32 @mbedtls_asn1_write_len(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

declare i32 @mbedtls_asn1_write_tag(ptr noundef, ptr noundef, i8 noundef zeroext) local_unnamed_addr #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 = { "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 16}
!6 = !{!"mbedtls_asn1_named_data", !7, i64 0, !7, i64 24, !12, i64 48, !9, i64 56}
!7 = !{!"mbedtls_asn1_buf", !8, i64 0, !11, i64 8, !12, i64 16}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!"any pointer", !9, i64 0}
!13 = !{!6, !11, i64 8}
!14 = !{!6, !12, i64 40}
!15 = !{!6, !11, i64 32}
!16 = !{!6, !12, i64 48}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass