; LLVM IR from Montimage/mmt-security :: src/lib/mmt_map_t.ll (mmt_map_set_data)

; 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.mmt_map_node_struct = type { ptr, ptr, ptr, ptr }

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

declare ptr @mmt_mem_alloc(i64 noundef) #1

; 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 ptr @_mmt_map_set_data(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, i1 noundef zeroext %4) #2 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i8, align 1
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = 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 ptr %3, ptr %10, align 8, !tbaa !5
  %16 = zext i1 %4 to i8
  store i8 %16, ptr %11, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  store i32 0, ptr %12, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  store ptr null, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  %18 = load ptr, ptr %17, align 8, !tbaa !5
  store ptr %18, ptr %14, align 8, !tbaa !5
  %19 = load ptr, ptr %14, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %35

21:                                               ; preds = %5
  %22 = call ptr @mmt_mem_alloc(i64 noundef 32)
  store ptr %22, ptr %14, align 8, !tbaa !5
  %23 = load ptr, ptr %14, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.mmt_map_node_struct, ptr %23, i32 0, i32 1
  store ptr null, ptr %24, align 8, !tbaa !13
  %25 = load ptr, ptr %14, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.mmt_map_node_struct, ptr %25, i32 0, i32 0
  store ptr null, ptr %26, align 8, !tbaa !15
  %27 = load ptr, ptr %9, align 8, !tbaa !5
  %28 = load ptr, ptr %14, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.mmt_map_node_struct, ptr %28, i32 0, i32 2
  store ptr %27, ptr %29, align 8, !tbaa !16
  %30 = load ptr, ptr %10, align 8, !tbaa !5
  %31 = load ptr, ptr %14, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.mmt_map_node_struct, ptr %31, i32 0, i32 3
  store ptr %30, ptr %32, align 8, !tbaa !17
  %33 = load ptr, ptr %14, align 8, !tbaa !5
  %34 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %33, ptr %34, align 8, !tbaa !5
  store ptr null, ptr %6, align 8
  store i32 1, ptr %15, align 4
  br label %78

35:                                               ; preds = %5
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = load ptr, ptr %9, align 8, !tbaa !5
  %38 = load ptr, ptr %14, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.mmt_map_node_struct, ptr %38, i32 0, i32 2
  %40 = load ptr, ptr %39, align 8, !tbaa !16
  %41 = call i32 %36(ptr noundef %37, ptr noundef %40)
  store i32 %41, ptr %12, align 4, !tbaa !11
  %42 = load i32, ptr %12, align 4, !tbaa !11
  %43 = icmp eq i32 %42, 0
  br i1 %43, label %44, label %57

44:                                               ; preds = %35
  %45 = load i8, ptr %11, align 1, !tbaa !9, !range !18, !noundef !19
  %46 = trunc i8 %45 to i1
  br i1 %46, label %47, label %55

47:                                               ; preds = %44
  %48 = load ptr, ptr %14, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.mmt_map_node_struct, ptr %48, i32 0, i32 3
  %50 = load ptr, ptr %49, align 8, !tbaa !17
  store ptr %50, ptr %13, align 8, !tbaa !5
  %51 = load ptr, ptr %10, align 8, !tbaa !5
  %52 = load ptr, ptr %14, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.mmt_map_node_struct, ptr %52, i32 0, i32 3
  store ptr %51, ptr %53, align 8, !tbaa !17
  %54 = load ptr, ptr %13, align 8, !tbaa !5
  store ptr %54, ptr %6, align 8
  store i32 1, ptr %15, align 4
  br label %78

55:                                               ; preds = %44
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %56, ptr %6, align 8
  store i32 1, ptr %15, align 4
  br label %78

57:                                               ; preds = %35
  %58 = load i32, ptr %12, align 4, !tbaa !11
  %59 = icmp slt i32 %58, 0
  br i1 %59, label %60, label %69

60:                                               ; preds = %57
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = load ptr, ptr %14, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.mmt_map_node_struct, ptr %62, i32 0, i32 0
  %64 = load ptr, ptr %9, align 8, !tbaa !5
  %65 = load ptr, ptr %10, align 8, !tbaa !5
  %66 = load i8, ptr %11, align 1, !tbaa !9, !range !18, !noundef !19
  %67 = trunc i8 %66 to i1
  %68 = call ptr @_mmt_map_set_data(ptr noundef %61, ptr noundef %63, ptr noundef %64, ptr noundef %65, i1 noundef zeroext %67)
  store ptr %68, ptr %6, align 8
  store i32 1, ptr %15, align 4
  br label %78

69:                                               ; preds = %57
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  %71 = load ptr, ptr %14, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.mmt_map_node_struct, ptr %71, i32 0, i32 1
  %73 = load ptr, ptr %9, align 8, !tbaa !5
  %74 = load ptr, ptr %10, align 8, !tbaa !5
  %75 = load i8, ptr %11, align 1, !tbaa !9, !range !18, !noundef !19
  %76 = trunc i8 %75 to i1
  %77 = call ptr @_mmt_map_set_data(ptr noundef %70, ptr noundef %72, ptr noundef %73, ptr noundef %74, i1 noundef zeroext %76)
  store ptr %77, ptr %6, align 8
  store i32 1, ptr %15, align 4
  br label %78

78:                                               ; preds = %69, %60, %55, %47, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  %79 = load ptr, ptr %6, align 8
  ret ptr %79
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 = { 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 = !{!"_Bool", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !6, i64 8}
!14 = !{!"mmt_map_node_struct", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!15 = !{!14, !6, i64 0}
!16 = !{!14, !6, i64 16}
!17 = !{!14, !6, i64 24}
!18 = !{i8 0, i8 2}
!19 = !{}


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

declare ptr @mmt_mem_alloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @_mmt_map_set_data(ptr nocapture noundef readonly %0, ptr nocapture noundef %1, ptr noundef %2, ptr noundef %3, i1 noundef zeroext %4) local_unnamed_addr #1 {
  %6 = load ptr, ptr %1, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %tailrecurse._crit_edge, label %.lr.ph

tailrecurse._crit_edge.loopexit:                  ; preds = %tailrecurse
  %spec.select.le = getelementptr inbounds i8, ptr %11, i64 %spec.select.idx
  br label %tailrecurse._crit_edge

tailrecurse._crit_edge:                           ; preds = %tailrecurse._crit_edge.loopexit, %5
  %.tr33.lcssa = phi ptr [ %1, %5 ], [ %spec.select.le, %tailrecurse._crit_edge.loopexit ]
  %8 = tail call ptr @mmt_mem_alloc(i64 noundef 32) #3
  %9 = getelementptr inbounds i8, ptr %8, i64 16
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %8, i8 0, i64 16, i1 false)
  store ptr %2, ptr %9, align 8, !tbaa !9
  %10 = getelementptr inbounds i8, ptr %8, i64 24
  store ptr %3, ptr %10, align 8, !tbaa !11
  store ptr %8, ptr %.tr33.lcssa, align 8, !tbaa !5
  br label %23

.lr.ph:                                           ; preds = %5, %tailrecurse
  %11 = phi ptr [ %21, %tailrecurse ], [ %6, %5 ]
  %12 = getelementptr inbounds i8, ptr %11, i64 16
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  %14 = tail call i32 %0(ptr noundef %2, ptr noundef %13) #3
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %tailrecurse

16:                                               ; preds = %.lr.ph
  br i1 %4, label %17, label %23

17:                                               ; preds = %16
  %18 = getelementptr inbounds i8, ptr %11, i64 24
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  store ptr %3, ptr %18, align 8, !tbaa !11
  br label %23

tailrecurse:                                      ; preds = %.lr.ph
  %20 = icmp slt i32 %14, 0
  %spec.select.idx = select i1 %20, i64 0, i64 8
  %spec.select = getelementptr inbounds i8, ptr %11, i64 %spec.select.idx
  %21 = load ptr, ptr %spec.select, align 8, !tbaa !5
  %22 = icmp eq ptr %21, null
  br i1 %22, label %tailrecurse._crit_edge.loopexit, label %.lr.ph

23:                                               ; preds = %16, %17, %tailrecurse._crit_edge
  %.0 = phi ptr [ null, %tailrecurse._crit_edge ], [ %19, %17 ], [ %3, %16 ]
  ret ptr %.0
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

attributes #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 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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, !6, i64 16}
!10 = !{!"mmt_map_node_struct", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!11 = !{!10, !6, i64 24}


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