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

%struct.mbedtls_x509write_cert = type { i32, %struct.mbedtls_mpi, ptr, ptr, ptr, ptr, i32, [16 x i8], [16 x i8], ptr }
%struct.mbedtls_mpi = type { i32, i64, ptr }

@.str.2 = 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

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

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

declare void @mbedtls_sha1(ptr noundef, i64 noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_x509write_crt_set_authority_key_identifier(ptr noundef %0) #2 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca [2068 x i8], align 16
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 2068, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %9 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %10 = getelementptr inbounds i8, ptr %9, i64 2068
  store ptr %10, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store i64 0, ptr %7, align 8, !tbaa !9
  %11 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  call void @llvm.memset.p0.i64(ptr align 16 %11, i8 0, i64 2068, i1 false)
  br label %12

12:                                               ; preds = %1
  %13 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.mbedtls_x509write_cert, ptr %14, i32 0, i32 3
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = call i32 @mbedtls_pk_write_pubkey(ptr noundef %6, ptr noundef %13, ptr noundef %16)
  store i32 %17, ptr %4, align 4, !tbaa !15
  %18 = icmp slt i32 %17, 0
  br i1 %18, label %19, label %21

19:                                               ; preds = %12
  %20 = load i32, ptr %4, align 4, !tbaa !15
  store i32 %20, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %107

21:                                               ; preds = %12
  %22 = load i32, ptr %4, align 4, !tbaa !15
  %23 = sext i32 %22 to i64
  %24 = load i64, ptr %7, align 8, !tbaa !9
  %25 = add i64 %24, %23
  store i64 %25, ptr %7, align 8, !tbaa !9
  br label %26

26:                                               ; preds = %21
  br label %27

27:                                               ; preds = %26
  br label %28

28:                                               ; preds = %27
  %29 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %30 = getelementptr inbounds i8, ptr %29, i64 2068
  %31 = load i64, ptr %7, align 8, !tbaa !9
  %32 = sub i64 0, %31
  %33 = getelementptr inbounds i8, ptr %30, i64 %32
  %34 = load i64, ptr %7, align 8, !tbaa !9
  %35 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %36 = getelementptr inbounds i8, ptr %35, i64 2068
  %37 = getelementptr inbounds i8, ptr %36, i64 -20
  call void @mbedtls_sha1(ptr noundef %33, i64 noundef %34, ptr noundef %37)
  %38 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %39 = getelementptr inbounds i8, ptr %38, i64 2068
  %40 = getelementptr inbounds i8, ptr %39, i64 -20
  store ptr %40, ptr %6, align 8, !tbaa !5
  store i64 20, ptr %7, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %28
  %42 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = call i32 @mbedtls_asn1_write_len(ptr noundef %6, ptr noundef %42, i64 noundef %43)
  store i32 %44, ptr %4, align 4, !tbaa !15
  %45 = icmp slt i32 %44, 0
  br i1 %45, label %46, label %48

46:                                               ; preds = %41
  %47 = load i32, ptr %4, align 4, !tbaa !15
  store i32 %47, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %107

48:                                               ; preds = %41
  %49 = load i32, ptr %4, align 4, !tbaa !15
  %50 = sext i32 %49 to i64
  %51 = load i64, ptr %7, align 8, !tbaa !9
  %52 = add i64 %51, %50
  store i64 %52, ptr %7, align 8, !tbaa !9
  br label %53

53:                                               ; preds = %48
  br label %54

54:                                               ; preds = %53
  br label %55

55:                                               ; preds = %54
  br label %56

56:                                               ; preds = %55
  %57 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %58 = call i32 @mbedtls_asn1_write_tag(ptr noundef %6, ptr noundef %57, i8 noundef zeroext -128)
  store i32 %58, ptr %4, align 4, !tbaa !15
  %59 = icmp slt i32 %58, 0
  br i1 %59, label %60, label %62

60:                                               ; preds = %56
  %61 = load i32, ptr %4, align 4, !tbaa !15
  store i32 %61, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %107

62:                                               ; preds = %56
  %63 = load i32, ptr %4, align 4, !tbaa !15
  %64 = sext i32 %63 to i64
  %65 = load i64, ptr %7, align 8, !tbaa !9
  %66 = add i64 %65, %64
  store i64 %66, ptr %7, align 8, !tbaa !9
  br label %67

67:                                               ; preds = %62
  br label %68

68:                                               ; preds = %67
  br label %69

69:                                               ; preds = %68
  br label %70

70:                                               ; preds = %69
  %71 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %72 = load i64, ptr %7, align 8, !tbaa !9
  %73 = call i32 @mbedtls_asn1_write_len(ptr noundef %6, ptr noundef %71, i64 noundef %72)
  store i32 %73, ptr %4, align 4, !tbaa !15
  %74 = icmp slt i32 %73, 0
  br i1 %74, label %75, label %77

75:                                               ; preds = %70
  %76 = load i32, ptr %4, align 4, !tbaa !15
  store i32 %76, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %107

77:                                               ; preds = %70
  %78 = load i32, ptr %4, align 4, !tbaa !15
  %79 = sext i32 %78 to i64
  %80 = load i64, ptr %7, align 8, !tbaa !9
  %81 = add i64 %80, %79
  store i64 %81, ptr %7, align 8, !tbaa !9
  br label %82

82:                                               ; preds = %77
  br label %83

83:                                               ; preds = %82
  br label %84

84:                                               ; preds = %83
  br label %85

85:                                               ; preds = %84
  %86 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %87 = call i32 @mbedtls_asn1_write_tag(ptr noundef %6, ptr noundef %86, i8 noundef zeroext 48)
  store i32 %87, ptr %4, align 4, !tbaa !15
  %88 = icmp slt i32 %87, 0
  br i1 %88, label %89, label %91

89:                                               ; preds = %85
  %90 = load i32, ptr %4, align 4, !tbaa !15
  store i32 %90, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %107

91:                                               ; preds = %85
  %92 = load i32, ptr %4, align 4, !tbaa !15
  %93 = sext i32 %92 to i64
  %94 = load i64, ptr %7, align 8, !tbaa !9
  %95 = add i64 %94, %93
  store i64 %95, ptr %7, align 8, !tbaa !9
  br label %96

96:                                               ; preds = %91
  br label %97

97:                                               ; preds = %96
  br label %98

98:                                               ; preds = %97
  %99 = load ptr, ptr %3, align 8, !tbaa !5
  %100 = getelementptr inbounds [2068 x i8], ptr %5, i64 0, i64 0
  %101 = getelementptr inbounds i8, ptr %100, i64 2068
  %102 = load i64, ptr %7, align 8, !tbaa !9
  %103 = sub i64 0, %102
  %104 = getelementptr inbounds i8, ptr %101, i64 %103
  %105 = load i64, ptr %7, align 8, !tbaa !9
  %106 = call i32 @mbedtls_x509write_crt_set_extension(ptr noundef %99, ptr noundef @.str.2, i64 noundef 3, i32 noundef 0, ptr noundef %104, i64 noundef %105)
  store i32 %106, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %107

107:                                              ; preds = %98, %89, %75, %60, %46, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 2068, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  %108 = load i32, ptr %2, align 4
  ret i32 %108
}

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 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 40}
!12 = !{!"mbedtls_x509write_cert", !13, i64 0, !14, i64 8, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !13, i64 64, !7, i64 68, !7, i64 84, !6, i64 104}
!13 = !{!"int", !7, i64 0}
!14 = !{!"", !13, i64 0, !10, i64 8, !6, i64 16}
!15 = !{!13, !13, 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.2 = 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

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

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

declare void @mbedtls_sha1(ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_x509write_crt_set_authority_key_identifier(ptr noundef %0) local_unnamed_addr #2 {
  %2 = alloca [2068 x i8], align 16
  %3 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 2068, ptr nonnull %2) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #4
  %4 = getelementptr inbounds i8, ptr %2, i64 2068
  store ptr %4, ptr %3, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(2068) %2, i8 0, i64 2068, i1 false)
  %5 = getelementptr inbounds i8, ptr %0, i64 40
  %6 = load ptr, ptr %5, align 8, !tbaa !9
  %7 = call i32 @mbedtls_pk_write_pubkey(ptr noundef nonnull %3, ptr noundef nonnull %2, ptr noundef %6) #4
  %8 = icmp slt i32 %7, 0
  br i1 %8, label %36, label %9

9:                                                ; preds = %1
  %10 = zext nneg i32 %7 to i64
  %11 = sub nsw i64 0, %10
  %12 = getelementptr inbounds i8, ptr %4, i64 %11
  %13 = getelementptr inbounds i8, ptr %2, i64 2048
  call void @mbedtls_sha1(ptr noundef nonnull %12, i64 noundef %10, ptr noundef nonnull %13) #4
  store ptr %13, ptr %3, align 8, !tbaa !5
  %14 = call i32 @mbedtls_asn1_write_len(ptr noundef nonnull %3, ptr noundef nonnull %2, i64 noundef 20) #4
  %15 = icmp slt i32 %14, 0
  br i1 %15, label %36, label %16

16:                                               ; preds = %9
  %17 = call i32 @mbedtls_asn1_write_tag(ptr noundef nonnull %3, ptr noundef nonnull %2, i8 noundef zeroext -128) #4
  %18 = icmp slt i32 %17, 0
  br i1 %18, label %36, label %19

19:                                               ; preds = %16
  %narrow = add nuw i32 %14, 20
  %20 = zext i32 %narrow to i64
  %21 = zext nneg i32 %17 to i64
  %22 = add nuw nsw i64 %21, %20
  %23 = call i32 @mbedtls_asn1_write_len(ptr noundef nonnull %3, ptr noundef nonnull %2, i64 noundef %22) #4
  %24 = icmp slt i32 %23, 0
  br i1 %24, label %36, label %25

25:                                               ; preds = %19
  %26 = call i32 @mbedtls_asn1_write_tag(ptr noundef nonnull %3, ptr noundef nonnull %2, i8 noundef zeroext 48) #4
  %27 = icmp slt i32 %26, 0
  br i1 %27, label %36, label %28

28:                                               ; preds = %25
  %29 = zext nneg i32 %23 to i64
  %30 = add nuw nsw i64 %22, %29
  %31 = zext nneg i32 %26 to i64
  %32 = add nuw nsw i64 %30, %31
  %33 = sub nsw i64 0, %32
  %34 = getelementptr inbounds i8, ptr %4, i64 %33
  %35 = call i32 @mbedtls_x509write_crt_set_extension(ptr noundef nonnull %0, ptr noundef nonnull @.str.2, i64 noundef 3, i32 noundef 0, ptr noundef nonnull %34, i64 noundef %32)
  br label %36

36:                                               ; preds = %25, %19, %16, %9, %1, %28
  %.0 = phi i32 [ %35, %28 ], [ %7, %1 ], [ %14, %9 ], [ %17, %16 ], [ %23, %19 ], [ %26, %25 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #4
  call void @llvm.lifetime.end.p0(i64 2068, ptr nonnull %2) #4
  ret i32 %.0
}

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"}
!9 = !{!10, !6, i64 40}
!10 = !{!"mbedtls_x509write_cert", !11, i64 0, !12, i64 8, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !11, i64 64, !7, i64 68, !7, i64 84, !6, i64 104}
!11 = !{!"int", !7, i64 0}
!12 = !{!"", !11, i64 0, !13, i64 8, !6, i64 16}
!13 = !{!"long", !7, i64 0}


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