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

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

@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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_x509write_crt_set_extension(ptr noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef, i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_x509write_crt_set_basic_constraints(ptr noundef %0, i32 noundef %1, i32 noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca [9 x i8], align 1
  %10 = alloca ptr, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 9, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %13 = getelementptr inbounds [9 x i8], ptr %9, i64 0, i64 0
  %14 = getelementptr inbounds i8, ptr %13, i64 9
  store ptr %14, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  store i64 0, ptr %11, align 8, !tbaa !11
  %15 = getelementptr inbounds [9 x i8], ptr %9, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 1 %15, i8 0, i64 9, i1 false)
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %22

18:                                               ; preds = %3
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = icmp sgt i32 %19, 127
  br i1 %20, label %21, label %22

21:                                               ; preds = %18
  store i32 -10240, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %97

22:                                               ; preds = %18, %3
  %23 = load i32, ptr %6, align 4, !tbaa !9
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %59

25:                                               ; preds = %22
  %26 = load i32, ptr %7, align 4, !tbaa !9
  %27 = icmp sge i32 %26, 0
  br i1 %27, label %28, label %44

28:                                               ; preds = %25
  br label %29

29:                                               ; preds = %28
  %30 = getelementptr inbounds [9 x i8], ptr %9, i64 0, i64 0
  %31 = load i32, ptr %7, align 4, !tbaa !9
  %32 = call i32 @mbedtls_asn1_write_int(ptr noundef %10, ptr noundef %30, i32 noundef %31)
  store i32 %32, ptr %8, align 4, !tbaa !9
  %33 = icmp slt i32 %32, 0
  br i1 %33, label %34, label %36

34:                                               ; preds = %29
  %35 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %35, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %97

36:                                               ; preds = %29
  %37 = load i32, ptr %8, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = load i64, ptr %11, align 8, !tbaa !11
  %40 = add i64 %39, %38
  store i64 %40, ptr %11, align 8, !tbaa !11
  br label %41

41:                                               ; preds = %36
  br label %42

42:                                               ; preds = %41
  br label %43

43:                                               ; preds = %42
  br label %44

44:                                               ; preds = %43, %25
  br label %45

45:                                               ; preds = %44
  %46 = getelementptr inbounds [9 x i8], ptr %9, i64 0, i64 0
  %47 = call i32 @mbedtls_asn1_write_bool(ptr noundef %10, ptr noundef %46, i32 noundef 1)
  store i32 %47, ptr %8, align 4, !tbaa !9
  %48 = icmp slt i32 %47, 0
  br i1 %48, label %49, label %51

49:                                               ; preds = %45
  %50 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %50, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %97

51:                                               ; preds = %45
  %52 = load i32, ptr %8, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = load i64, ptr %11, align 8, !tbaa !11
  %55 = add i64 %54, %53
  store i64 %55, ptr %11, align 8, !tbaa !11
  br label %56

56:                                               ; preds = %51
  br label %57

57:                                               ; preds = %56
  br label %58

58:                                               ; preds = %57
  br label %59

59:                                               ; preds = %58, %22
  br label %60

60:                                               ; preds = %59
  %61 = getelementptr inbounds [9 x i8], ptr %9, i64 0, i64 0
  %62 = load i64, ptr %11, align 8, !tbaa !11
  %63 = call i32 @mbedtls_asn1_write_len(ptr noundef %10, ptr noundef %61, i64 noundef %62)
  store i32 %63, ptr %8, align 4, !tbaa !9
  %64 = icmp slt i32 %63, 0
  br i1 %64, label %65, label %67

65:                                               ; preds = %60
  %66 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %66, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %97

67:                                               ; preds = %60
  %68 = load i32, ptr %8, align 4, !tbaa !9
  %69 = sext i32 %68 to i64
  %70 = load i64, ptr %11, align 8, !tbaa !11
  %71 = add i64 %70, %69
  store i64 %71, ptr %11, align 8, !tbaa !11
  br label %72

72:                                               ; preds = %67
  br label %73

73:                                               ; preds = %72
  br label %74

74:                                               ; preds = %73
  br label %75

75:                                               ; preds = %74
  %76 = getelementptr inbounds [9 x i8], ptr %9, i64 0, i64 0
  %77 = call i32 @mbedtls_asn1_write_tag(ptr noundef %10, ptr noundef %76, i8 noundef zeroext 48)
  store i32 %77, ptr %8, align 4, !tbaa !9
  %78 = icmp slt i32 %77, 0
  br i1 %78, label %79, label %81

79:                                               ; preds = %75
  %80 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %80, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %97

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

86:                                               ; preds = %81
  br label %87

87:                                               ; preds = %86
  br label %88

88:                                               ; preds = %87
  %89 = load ptr, ptr %5, align 8, !tbaa !5
  %90 = getelementptr inbounds [9 x i8], ptr %9, i64 0, i64 0
  %91 = getelementptr inbounds i8, ptr %90, i64 9
  %92 = load i64, ptr %11, align 8, !tbaa !11
  %93 = sub i64 0, %92
  %94 = getelementptr inbounds i8, ptr %91, i64 %93
  %95 = load i64, ptr %11, align 8, !tbaa !11
  %96 = call i32 @mbedtls_x509write_crt_set_extension(ptr noundef %89, ptr noundef @.str, i64 noundef 3, i32 noundef 0, ptr noundef %94, i64 noundef %95)
  store i32 %96, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %97

97:                                               ; preds = %88, %79, %65, %49, %34, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 9, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %98 = load i32, ptr %4, align 4
  ret i32 %98
}

declare i32 @mbedtls_asn1_write_int(ptr noundef, ptr noundef, i32 noundef) #3

declare i32 @mbedtls_asn1_write_bool(ptr noundef, ptr noundef, i32 noundef) #3

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

declare i32 @mbedtls_asn1_write_tag(ptr noundef, ptr noundef, i8 noundef zeroext) #3

attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { "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 }

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

@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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_x509write_crt_set_extension(ptr noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_x509write_crt_set_basic_constraints(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #2 {
  %4 = alloca [9 x i8], align 1
  %5 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 9, ptr nonnull %4) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #4
  %6 = getelementptr inbounds i8, ptr %4, i64 9
  store ptr %6, ptr %5, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(9) %4, i8 0, i64 9, i1 false)
  %7 = icmp ne i32 %1, 0
  %8 = icmp sgt i32 %2, 127
  %or.cond = and i1 %7, %8
  br i1 %or.cond, label %37, label %9

9:                                                ; preds = %3
  br i1 %7, label %10, label %23

10:                                               ; preds = %9
  %11 = icmp sgt i32 %2, -1
  br i1 %11, label %12, label %17

12:                                               ; preds = %10
  %13 = call i32 @mbedtls_asn1_write_int(ptr noundef nonnull %5, ptr noundef nonnull %4, i32 noundef %2) #4
  %14 = icmp slt i32 %13, 0
  br i1 %14, label %37, label %15

15:                                               ; preds = %12
  %16 = zext nneg i32 %13 to i64
  br label %17

17:                                               ; preds = %10, %15
  %.0 = phi i64 [ %16, %15 ], [ 0, %10 ]
  %18 = call i32 @mbedtls_asn1_write_bool(ptr noundef nonnull %5, ptr noundef nonnull %4, i32 noundef 1) #4
  %19 = icmp slt i32 %18, 0
  br i1 %19, label %37, label %20

20:                                               ; preds = %17
  %21 = zext nneg i32 %18 to i64
  %22 = add nuw nsw i64 %.0, %21
  br label %23

23:                                               ; preds = %9, %20
  %.1 = phi i64 [ %22, %20 ], [ 0, %9 ]
  %24 = call i32 @mbedtls_asn1_write_len(ptr noundef nonnull %5, ptr noundef nonnull %4, i64 noundef %.1) #4
  %25 = icmp slt i32 %24, 0
  br i1 %25, label %37, label %26

26:                                               ; preds = %23
  %27 = call i32 @mbedtls_asn1_write_tag(ptr noundef nonnull %5, ptr noundef nonnull %4, i8 noundef zeroext 48) #4
  %28 = icmp slt i32 %27, 0
  br i1 %28, label %37, label %29

29:                                               ; preds = %26
  %30 = zext nneg i32 %24 to i64
  %31 = add nuw nsw i64 %.1, %30
  %32 = zext nneg i32 %27 to i64
  %33 = add nuw nsw i64 %31, %32
  %34 = sub nsw i64 0, %33
  %35 = getelementptr inbounds i8, ptr %6, i64 %34
  %36 = call i32 @mbedtls_x509write_crt_set_extension(ptr noundef %0, ptr noundef nonnull @.str, i64 noundef 3, i32 noundef 0, ptr noundef nonnull %35, i64 noundef %33)
  br label %37

37:                                               ; preds = %26, %23, %17, %12, %3, %29
  %.022 = phi i32 [ %36, %29 ], [ -10240, %3 ], [ %13, %12 ], [ %18, %17 ], [ %24, %23 ], [ %27, %26 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #4
  call void @llvm.lifetime.end.p0(i64 9, ptr nonnull %4) #4
  ret i32 %.022
}

declare i32 @mbedtls_asn1_write_int(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #3

declare i32 @mbedtls_asn1_write_bool(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #3

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

declare i32 @mbedtls_asn1_write_tag(ptr noundef, ptr noundef, i8 noundef zeroext) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { "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 }

!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"}


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