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

; 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_cookie_ctx = type { %struct.mbedtls_md_context_t, i64 }
%struct.mbedtls_md_context_t = type { ptr, ptr, 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
declare i64 @time(ptr noundef) #1

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

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

23:                                               ; preds = %5
  %24 = load ptr, ptr %10, align 8, !tbaa !5
  %25 = icmp eq ptr %24, null
  br i1 %25, label %26, label %27

26:                                               ; preds = %23, %5
  store i32 -28928, ptr %6, align 4
  store i32 1, ptr %18, align 4
  br label %92

27:                                               ; preds = %23
  %28 = load i64, ptr %9, align 8, !tbaa !9
  %29 = icmp ne i64 %28, 32
  br i1 %29, label %30, label %31

30:                                               ; preds = %27
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %18, align 4
  br label %92

31:                                               ; preds = %27
  %32 = load ptr, ptr %15, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.mbedtls_ssl_cookie_ctx, ptr %32, i32 0, i32 0
  %34 = load ptr, ptr %8, align 8, !tbaa !5
  %35 = load ptr, ptr %14, align 8, !tbaa !5
  %36 = getelementptr inbounds i8, ptr %35, i64 28
  %37 = load ptr, ptr %10, align 8, !tbaa !5
  %38 = load i64, ptr %11, align 8, !tbaa !9
  %39 = call i32 @ssl_cookie_hmac(ptr noundef %33, ptr noundef %34, ptr noundef %14, ptr noundef %36, ptr noundef %37, i64 noundef %38)
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %41, label %42

41:                                               ; preds = %31
  store i32 -1, ptr %13, align 4, !tbaa !11
  br label %42

42:                                               ; preds = %41, %31
  %43 = load i32, ptr %13, align 4, !tbaa !11
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %47

45:                                               ; preds = %42
  %46 = load i32, ptr %13, align 4, !tbaa !11
  store i32 %46, ptr %6, align 4
  store i32 1, ptr %18, align 4
  br label %92

47:                                               ; preds = %42
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = getelementptr inbounds i8, ptr %48, i64 4
  %50 = getelementptr inbounds [28 x i8], ptr %12, i64 0, i64 0
  %51 = call i32 @mbedtls_ssl_safer_memcmp(ptr noundef %49, ptr noundef %50, i64 noundef 28)
  %52 = icmp ne i32 %51, 0
  br i1 %52, label %53, label %54

53:                                               ; preds = %47
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %18, align 4
  br label %92

54:                                               ; preds = %47
  %55 = call i64 @time(ptr noundef null) #4
  store i64 %55, ptr %16, align 8, !tbaa !9
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  %57 = getelementptr inbounds i8, ptr %56, i64 0
  %58 = load i8, ptr %57, align 1, !tbaa !13
  %59 = zext i8 %58 to i64
  %60 = shl i64 %59, 24
  %61 = load ptr, ptr %8, align 8, !tbaa !5
  %62 = getelementptr inbounds i8, ptr %61, i64 1
  %63 = load i8, ptr %62, align 1, !tbaa !13
  %64 = zext i8 %63 to i64
  %65 = shl i64 %64, 16
  %66 = or i64 %60, %65
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = getelementptr inbounds i8, ptr %67, i64 2
  %69 = load i8, ptr %68, align 1, !tbaa !13
  %70 = zext i8 %69 to i64
  %71 = shl i64 %70, 8
  %72 = or i64 %66, %71
  %73 = load ptr, ptr %8, align 8, !tbaa !5
  %74 = getelementptr inbounds i8, ptr %73, i64 3
  %75 = load i8, ptr %74, align 1, !tbaa !13
  %76 = zext i8 %75 to i64
  %77 = or i64 %72, %76
  store i64 %77, ptr %17, align 8, !tbaa !9
  %78 = load ptr, ptr %15, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.mbedtls_ssl_cookie_ctx, ptr %78, i32 0, i32 1
  %80 = load i64, ptr %79, align 8, !tbaa !14
  %81 = icmp ne i64 %80, 0
  br i1 %81, label %82, label %91

82:                                               ; preds = %54
  %83 = load i64, ptr %16, align 8, !tbaa !9
  %84 = load i64, ptr %17, align 8, !tbaa !9
  %85 = sub i64 %83, %84
  %86 = load ptr, ptr %15, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.mbedtls_ssl_cookie_ctx, ptr %86, i32 0, i32 1
  %88 = load i64, ptr %87, align 8, !tbaa !14
  %89 = icmp ugt i64 %85, %88
  br i1 %89, label %90, label %91

90:                                               ; preds = %82
  store i32 -1, ptr %6, align 4
  store i32 1, ptr %18, align 4
  br label %92

91:                                               ; preds = %82, %54
  store i32 0, ptr %6, align 4
  store i32 1, ptr %18, align 4
  br label %92

92:                                               ; preds = %91, %90, %53, %45, %30, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 28, ptr %12) #4
  %93 = load i32, ptr %6, align 4
  ret i32 %93
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @mbedtls_ssl_safer_memcmp(ptr noundef, ptr noundef, i64 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind "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 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 = { inlinehint 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 #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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !10, i64 24}
!15 = !{!"", !16, i64 0, !10, i64 24}
!16 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16}


; 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: 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
declare i64 @time(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 -28928, 1) i32 @mbedtls_ssl_cookie_check(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, i64 noundef %4) local_unnamed_addr #2 {
  %6 = alloca [28 x i8], align 16
  %7 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 28, ptr nonnull %6) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #4
  store ptr %6, ptr %7, align 8, !tbaa !5
  %8 = icmp eq ptr %0, null
  %9 = icmp eq ptr %3, null
  %or.cond = or i1 %8, %9
  br i1 %or.cond, label %42, label %10

10:                                               ; preds = %5
  %.not = icmp eq i64 %2, 32
  br i1 %.not, label %11, label %42

11:                                               ; preds = %10
  %12 = getelementptr inbounds i8, ptr %6, i64 28
  %13 = call i32 @ssl_cookie_hmac(ptr noundef nonnull %0, ptr noundef %1, ptr noundef nonnull %7, ptr noundef nonnull %12, ptr noundef nonnull %3, i64 noundef %4)
  %.not24 = icmp eq i32 %13, 0
  br i1 %.not24, label %14, label %42

14:                                               ; preds = %11
  %15 = getelementptr inbounds i8, ptr %1, i64 4
  %16 = call i32 @mbedtls_ssl_safer_memcmp(ptr noundef nonnull %15, ptr noundef nonnull %6, i64 noundef 28)
  %.not26 = icmp eq i32 %16, 0
  br i1 %.not26, label %17, label %42

17:                                               ; preds = %14
  %18 = call i64 @time(ptr noundef null) #4
  %19 = getelementptr inbounds i8, ptr %0, i64 24
  %20 = load i64, ptr %19, align 8, !tbaa !9
  %.not27 = icmp eq i64 %20, 0
  br i1 %.not27, label %41, label %21

21:                                               ; preds = %17
  %22 = load i8, ptr %1, align 1, !tbaa !13
  %23 = zext i8 %22 to i64
  %24 = shl nuw nsw i64 %23, 24
  %25 = getelementptr inbounds i8, ptr %1, i64 1
  %26 = load i8, ptr %25, align 1, !tbaa !13
  %27 = zext i8 %26 to i64
  %28 = shl nuw nsw i64 %27, 16
  %29 = or disjoint i64 %28, %24
  %30 = getelementptr inbounds i8, ptr %1, i64 2
  %31 = load i8, ptr %30, align 1, !tbaa !13
  %32 = zext i8 %31 to i64
  %33 = shl nuw nsw i64 %32, 8
  %34 = or disjoint i64 %29, %33
  %35 = getelementptr inbounds i8, ptr %1, i64 3
  %36 = load i8, ptr %35, align 1, !tbaa !13
  %37 = zext i8 %36 to i64
  %38 = or disjoint i64 %34, %37
  %39 = sub i64 %18, %38
  %40 = icmp ugt i64 %39, %20
  br i1 %40, label %42, label %41

41:                                               ; preds = %21, %17
  br label %42

42:                                               ; preds = %21, %14, %11, %10, %5, %41
  %.0 = phi i32 [ 0, %41 ], [ -28928, %5 ], [ -1, %10 ], [ -1, %11 ], [ -1, %14 ], [ -1, %21 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #4
  call void @llvm.lifetime.end.p0(i64 28, ptr nonnull %6) #4
  ret i32 %.0
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @mbedtls_ssl_safer_memcmp(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind "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 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 = { inlinehint 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 #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, !12, i64 24}
!10 = !{!"", !11, i64 0, !12, i64 24}
!11 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16}
!12 = !{!"long", !7, i64 0}
!13 = !{!7, !7, i64 0}


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