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

; 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_ssl_ticket_context = type { [2 x %struct.mbedtls_ssl_ticket_key], i8, i32, ptr, ptr }
%struct.mbedtls_ssl_ticket_key = type { [4 x i8], i32, %struct.mbedtls_cipher_context_t }
%struct.mbedtls_cipher_context_t = type { ptr, i32, i32, ptr, ptr, [16 x i8], i64, [16 x i8], i64, ptr }
%struct.mbedtls_cipher_info_t = type { i32, i32, i32, ptr, i32, i32, i32, ptr }

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ssl_ticket_setup(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4) #0 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !9
  store i32 %4, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %15 = load ptr, ptr %8, align 8, !tbaa !5
  %16 = load ptr, ptr %7, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.mbedtls_ssl_ticket_context, ptr %16, i32 0, i32 3
  store ptr %15, ptr %17, align 8, !tbaa !11
  %18 = load ptr, ptr %9, align 8, !tbaa !5
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.mbedtls_ssl_ticket_context, ptr %19, i32 0, i32 4
  store ptr %18, ptr %20, align 8, !tbaa !13
  %21 = load i32, ptr %11, align 4, !tbaa !9
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.mbedtls_ssl_ticket_context, ptr %22, i32 0, i32 2
  store i32 %21, ptr %23, align 4, !tbaa !14
  %24 = load i32, ptr %10, align 4, !tbaa !9
  %25 = call ptr @mbedtls_cipher_info_from_type(i32 noundef %24)
  store ptr %25, ptr %13, align 8, !tbaa !5
  %26 = load ptr, ptr %13, align 8, !tbaa !5
  %27 = icmp eq ptr %26, null
  br i1 %27, label %28, label %29

28:                                               ; preds = %5
  store i32 -28928, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %75

29:                                               ; preds = %5
  %30 = load ptr, ptr %13, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.mbedtls_cipher_info_t, ptr %30, i32 0, i32 1
  %32 = load i32, ptr %31, align 4, !tbaa !15
  %33 = icmp ne i32 %32, 6
  br i1 %33, label %34, label %40

34:                                               ; preds = %29
  %35 = load ptr, ptr %13, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.mbedtls_cipher_info_t, ptr %35, i32 0, i32 1
  %37 = load i32, ptr %36, align 4, !tbaa !15
  %38 = icmp ne i32 %37, 8
  br i1 %38, label %39, label %40

39:                                               ; preds = %34
  store i32 -28928, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %75

40:                                               ; preds = %34, %29
  %41 = load ptr, ptr %13, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.mbedtls_cipher_info_t, ptr %41, i32 0, i32 2
  %43 = load i32, ptr %42, align 8, !tbaa !17
  %44 = icmp ugt i32 %43, 256
  br i1 %44, label %45, label %46

45:                                               ; preds = %40
  store i32 -28928, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %75

46:                                               ; preds = %40
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.mbedtls_ssl_ticket_context, ptr %47, i32 0, i32 0
  %49 = getelementptr inbounds [2 x %struct.mbedtls_ssl_ticket_key], ptr %48, i64 0, i64 0
  %50 = getelementptr inbounds %struct.mbedtls_ssl_ticket_key, ptr %49, i32 0, i32 2
  %51 = load ptr, ptr %13, align 8, !tbaa !5
  %52 = call i32 @mbedtls_cipher_setup(ptr noundef %50, ptr noundef %51)
  store i32 %52, ptr %12, align 4, !tbaa !9
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %62, label %54

54:                                               ; preds = %46
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.mbedtls_ssl_ticket_context, ptr %55, i32 0, i32 0
  %57 = getelementptr inbounds [2 x %struct.mbedtls_ssl_ticket_key], ptr %56, i64 0, i64 1
  %58 = getelementptr inbounds %struct.mbedtls_ssl_ticket_key, ptr %57, i32 0, i32 2
  %59 = load ptr, ptr %13, align 8, !tbaa !5
  %60 = call i32 @mbedtls_cipher_setup(ptr noundef %58, ptr noundef %59)
  store i32 %60, ptr %12, align 4, !tbaa !9
  %61 = icmp ne i32 %60, 0
  br i1 %61, label %62, label %64

62:                                               ; preds = %54, %46
  %63 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %63, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %75

64:                                               ; preds = %54
  %65 = load ptr, ptr %7, align 8, !tbaa !5
  %66 = call i32 @ssl_ticket_gen_key(ptr noundef %65, i8 noundef zeroext 0)
  store i32 %66, ptr %12, align 4, !tbaa !9
  %67 = icmp ne i32 %66, 0
  br i1 %67, label %72, label %68

68:                                               ; preds = %64
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = call i32 @ssl_ticket_gen_key(ptr noundef %69, i8 noundef zeroext 1)
  store i32 %70, ptr %12, align 4, !tbaa !9
  %71 = icmp ne i32 %70, 0
  br i1 %71, label %72, label %74

72:                                               ; preds = %68, %64
  %73 = load i32, ptr %12, align 4, !tbaa !9
  store i32 %73, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %75

74:                                               ; preds = %68
  store i32 0, ptr %6, align 4
  store i32 1, ptr %14, align 4
  br label %75

75:                                               ; preds = %74, %72, %62, %45, %39, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  %76 = load i32, ptr %6, align 4
  ret i32 %76
}

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

declare ptr @mbedtls_cipher_info_from_type(i32 noundef) #2

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

; Function Attrs: nounwind uwtable
declare hidden i32 @ssl_ticket_gen_key(ptr noundef, i8 noundef zeroext) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 200}
!12 = !{!"", !7, i64 0, !7, i64 192, !10, i64 196, !6, i64 200, !6, i64 208}
!13 = !{!12, !6, i64 208}
!14 = !{!12, !10, i64 196}
!15 = !{!16, !10, i64 4}
!16 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !6, i64 16, !10, i64 24, !10, i64 28, !10, i64 32, !6, i64 40}
!17 = !{!16, !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"

; Function Attrs: nounwind uwtable
define dso_local i32 @mbedtls_ssl_ticket_setup(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = getelementptr inbounds i8, ptr %0, i64 200
  store ptr %1, ptr %6, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 208
  store ptr %2, ptr %7, align 8, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %0, i64 196
  store i32 %4, ptr %8, align 4, !tbaa !12
  %9 = tail call ptr @mbedtls_cipher_info_from_type(i32 noundef %3) #2
  %10 = icmp eq ptr %9, null
  br i1 %10, label %28, label %11

11:                                               ; preds = %5
  %12 = getelementptr inbounds i8, ptr %9, i64 4
  %13 = load i32, ptr %12, align 4, !tbaa !13
  switch i32 %13, label %28 [
    i32 6, label %14
    i32 8, label %14
  ]

14:                                               ; preds = %11, %11
  %15 = getelementptr inbounds i8, ptr %9, i64 8
  %16 = load i32, ptr %15, align 8, !tbaa !15
  %17 = icmp ugt i32 %16, 256
  br i1 %17, label %28, label %18

18:                                               ; preds = %14
  %19 = getelementptr inbounds i8, ptr %0, i64 8
  %20 = tail call i32 @mbedtls_cipher_setup(ptr noundef nonnull %19, ptr noundef nonnull %9) #2
  %.not27 = icmp eq i32 %20, 0
  br i1 %.not27, label %21, label %28

21:                                               ; preds = %18
  %22 = getelementptr inbounds i8, ptr %0, i64 104
  %23 = tail call i32 @mbedtls_cipher_setup(ptr noundef nonnull %22, ptr noundef nonnull %9) #2
  %.not28 = icmp eq i32 %23, 0
  br i1 %.not28, label %24, label %28

24:                                               ; preds = %21
  %25 = tail call i32 @ssl_ticket_gen_key(ptr noundef nonnull %0, i8 noundef zeroext 0)
  %.not29 = icmp eq i32 %25, 0
  br i1 %.not29, label %26, label %28

26:                                               ; preds = %24
  %27 = tail call i32 @ssl_ticket_gen_key(ptr noundef nonnull %0, i8 noundef zeroext 1)
  br label %28

28:                                               ; preds = %26, %24, %18, %21, %14, %11, %5
  %.0 = phi i32 [ -28928, %5 ], [ -28928, %11 ], [ -28928, %14 ], [ %20, %18 ], [ %23, %21 ], [ %25, %24 ], [ %27, %26 ]
  ret i32 %.0
}

declare ptr @mbedtls_cipher_info_from_type(i32 noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare hidden i32 @ssl_ticket_gen_key(ptr noundef, i8 noundef zeroext) local_unnamed_addr #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, !10, i64 200}
!6 = !{!"", !7, i64 0, !7, i64 192, !9, i64 196, !10, i64 200, !10, i64 208}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!6, !10, i64 208}
!12 = !{!6, !9, i64 196}
!13 = !{!14, !9, i64 4}
!14 = !{!"", !9, i64 0, !9, i64 4, !9, i64 8, !10, i64 16, !9, i64 24, !9, i64 28, !9, i64 32, !10, i64 40}
!15 = !{!14, !9, i64 8}


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