; LLVM IR from gem5/gem5 :: ext/systemc/src/sysc/kernel/sc_cor_qt.ll (ZN7sc_core9sc_cor_qt13stack_protectEb)

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

%"class.sc_core::sc_cor_qt" = type { %"class.sc_core::sc_cor", i64, ptr, ptr, ptr }
%"class.sc_core::sc_cor" = type { ptr }

@_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize = external hidden global i64, align 8
@.str = external hidden unnamed_addr constant [14 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [85 x i8], align 1
@__PRETTY_FUNCTION__._ZN7sc_core9sc_cor_qt13stack_protectEb = external hidden unnamed_addr constant [53 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [32 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [9 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN7sc_core9sc_cor_qt13stack_protectEb(ptr noundef nonnull align 8 dereferenceable(40) %0, i1 noundef zeroext %1) unnamed_addr #0 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %7 = zext i1 %1 to i8
  store i8 %7, ptr %4, align 1, !tbaa !9
  %8 = load ptr, ptr %3, align 8
  %9 = load i64, ptr @_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize, align 8, !tbaa !11
  %10 = icmp eq i64 %9, 0
  br i1 %10, label %11, label %13

11:                                               ; preds = %2
  %12 = call i64 @sysconf(i32 noundef 30) #4
  store i64 %12, ptr @_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize, align 8, !tbaa !11
  br label %13

13:                                               ; preds = %11, %2
  %14 = load i64, ptr @_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize, align 8, !tbaa !11
  %15 = icmp ne i64 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %13
  br label %19

17:                                               ; preds = %13
  call void @__assert_fail(ptr noundef @.str, ptr noundef @.str.2, i32 noundef 77, ptr noundef @__PRETTY_FUNCTION__._ZN7sc_core9sc_cor_qt13stack_protectEb) #5
  unreachable

18:                                               ; No predecessors!
  br label %19

19:                                               ; preds = %18, %16
  %20 = getelementptr inbounds %"class.sc_core::sc_cor_qt", ptr %8, i32 0, i32 1
  %21 = load i64, ptr %20, align 8, !tbaa !13
  %22 = load i64, ptr @_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize, align 8, !tbaa !11
  %23 = mul i64 2, %22
  %24 = icmp ugt i64 %21, %23
  br i1 %24, label %25, label %26

25:                                               ; preds = %19
  br label %28

26:                                               ; preds = %19
  call void @__assert_fail(ptr noundef @.str.3, ptr noundef @.str.2, i32 noundef 78, ptr noundef @__PRETTY_FUNCTION__._ZN7sc_core9sc_cor_qt13stack_protectEb) #5
  unreachable

27:                                               ; No predecessors!
  br label %28

28:                                               ; preds = %27, %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %29 = getelementptr inbounds %"class.sc_core::sc_cor_qt", ptr %8, i32 0, i32 2
  %30 = load ptr, ptr %29, align 8, !tbaa !16
  %31 = ptrtoint ptr %30 to i64
  %32 = load i64, ptr @_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize, align 8, !tbaa !11
  %33 = add i64 %31, %32
  %34 = sub i64 %33, 1
  %35 = load i64, ptr @_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize, align 8, !tbaa !11
  %36 = udiv i64 %34, %35
  %37 = load i64, ptr @_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize, align 8, !tbaa !11
  %38 = mul i64 %36, %37
  %39 = inttoptr i64 %38 to ptr
  store ptr %39, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %40 = load i8, ptr %4, align 1, !tbaa !9, !range !17, !noundef !18
  %41 = trunc i8 %40 to i1
  br i1 %41, label %42, label %47

42:                                               ; preds = %28
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = load i64, ptr @_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize, align 8, !tbaa !11
  %45 = sub i64 %44, 1
  %46 = call i32 @mprotect(ptr noundef %43, i64 noundef %45, i32 noundef 0) #4
  store i32 %46, ptr %6, align 4, !tbaa !19
  br label %60

47:                                               ; preds = %28
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = load i64, ptr @_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize, align 8, !tbaa !11
  %50 = sub i64 %49, 1
  %51 = call i32 @mprotect(ptr noundef %48, i64 noundef %50, i32 noundef 7) #4
  store i32 %51, ptr %6, align 4, !tbaa !19
  %52 = load i32, ptr %6, align 4, !tbaa !19
  %53 = icmp ne i32 %52, 0
  br i1 %53, label %54, label %59

54:                                               ; preds = %47
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = load i64, ptr @_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize, align 8, !tbaa !11
  %57 = sub i64 %56, 1
  %58 = call i32 @mprotect(ptr noundef %55, i64 noundef %57, i32 noundef 3) #4
  store i32 %58, ptr %6, align 4, !tbaa !19
  br label %59

59:                                               ; preds = %54, %47
  br label %60

60:                                               ; preds = %59, %42
  %61 = load i32, ptr %6, align 4, !tbaa !19
  %62 = icmp eq i32 %61, 0
  br i1 %62, label %63, label %64

63:                                               ; preds = %60
  br label %66

64:                                               ; preds = %60
  call void @__assert_fail(ptr noundef @.str.4, ptr noundef @.str.2, i32 noundef 109, ptr noundef @__PRETTY_FUNCTION__._ZN7sc_core9sc_cor_qt13stack_protectEb) #5
  unreachable

65:                                               ; No predecessors!
  br label %66

66:                                               ; preds = %65, %63
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret void
}

; Function Attrs: nounwind
declare i64 @sysconf(i32 noundef) #1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

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

; Function Attrs: nounwind
declare i32 @mprotect(ptr noundef, i64 noundef, i32 noundef) #1

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

attributes #0 = { mustprogress 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 = { 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 = { noreturn 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 #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind }
attributes #5 = { noreturn 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"bool", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !12, i64 8}
!14 = !{!"_ZTSN7sc_core9sc_cor_qtE", !15, i64 0, !12, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}
!15 = !{!"_ZTSN7sc_core6sc_corE"}
!16 = !{!14, !6, i64 16}
!17 = !{i8 0, i8 2}
!18 = !{}
!19 = !{!20, !20, i64 0}
!20 = !{!"int", !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"

@_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize = external hidden local_unnamed_addr global i64, align 8
@.str = external hidden unnamed_addr constant [14 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [85 x i8], align 1
@__PRETTY_FUNCTION__._ZN7sc_core9sc_cor_qt13stack_protectEb = external hidden unnamed_addr constant [53 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [32 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [9 x i8], align 1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN7sc_core9sc_cor_qt13stack_protectEb(ptr nocapture noundef nonnull readonly align 8 dereferenceable(40) %0, i1 noundef zeroext %1) unnamed_addr #0 align 2 {
  %3 = load i64, ptr @_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize, align 8, !tbaa !5
  %4 = icmp eq i64 %3, 0
  br i1 %4, label %5, label %.thread

5:                                                ; preds = %2
  %6 = tail call i64 @sysconf(i32 noundef 30) #3
  store i64 %6, ptr @_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize, align 8, !tbaa !5
  %.not = icmp eq i64 %6, 0
  br i1 %.not, label %7, label %.thread

7:                                                ; preds = %5
  tail call void @__assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.2, i32 noundef 77, ptr noundef nonnull @__PRETTY_FUNCTION__._ZN7sc_core9sc_cor_qt13stack_protectEb) #4
  unreachable

.thread:                                          ; preds = %2, %5
  %8 = phi i64 [ %6, %5 ], [ %3, %2 ]
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load i64, ptr %9, align 8, !tbaa !9
  %11 = shl i64 %8, 1
  %12 = icmp ugt i64 %10, %11
  br i1 %12, label %14, label %13

13:                                               ; preds = %.thread
  tail call void @__assert_fail(ptr noundef nonnull @.str.3, ptr noundef nonnull @.str.2, i32 noundef 78, ptr noundef nonnull @__PRETTY_FUNCTION__._ZN7sc_core9sc_cor_qt13stack_protectEb) #4
  unreachable

14:                                               ; preds = %.thread
  %15 = getelementptr inbounds i8, ptr %0, i64 16
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  %17 = ptrtoint ptr %16 to i64
  %18 = add i64 %8, %17
  %.fr8 = freeze i64 %18
  %19 = add i64 %.fr8, -1
  %20 = urem i64 %19, %8
  %21 = sub nuw i64 %19, %20
  %22 = inttoptr i64 %21 to ptr
  %23 = add i64 %8, -1
  br i1 %1, label %24, label %26

24:                                               ; preds = %14
  %25 = tail call i32 @mprotect(ptr noundef %22, i64 noundef %23, i32 noundef 0) #3
  br label %32

26:                                               ; preds = %14
  %27 = tail call i32 @mprotect(ptr noundef %22, i64 noundef %23, i32 noundef 7) #3
  %.not9 = icmp eq i32 %27, 0
  br i1 %.not9, label %.thread11, label %28

28:                                               ; preds = %26
  %29 = load i64, ptr @_ZZN7sc_core9sc_cor_qt13stack_protectEbE8pagesize, align 8, !tbaa !5
  %30 = add i64 %29, -1
  %31 = tail call i32 @mprotect(ptr noundef %22, i64 noundef %30, i32 noundef 3) #3
  br label %32

32:                                               ; preds = %28, %24
  %.0 = phi i32 [ %25, %24 ], [ %31, %28 ]
  %33 = icmp eq i32 %.0, 0
  br i1 %33, label %.thread11, label %34

34:                                               ; preds = %32
  tail call void @__assert_fail(ptr noundef nonnull @.str.4, ptr noundef nonnull @.str.2, i32 noundef 109, ptr noundef nonnull @__PRETTY_FUNCTION__._ZN7sc_core9sc_cor_qt13stack_protectEb) #4
  unreachable

.thread11:                                        ; preds = %26, %32
  ret void
}

; Function Attrs: nounwind
declare i64 @sysconf(i32 noundef) local_unnamed_addr #1

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare i32 @mprotect(ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = { 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 = { noreturn 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 #3 = { nounwind }
attributes #4 = { noreturn 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"_ZTSN7sc_core9sc_cor_qtE", !11, i64 0, !6, i64 8, !12, i64 16, !12, i64 24, !12, i64 32}
!11 = !{!"_ZTSN7sc_core6sc_corE"}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!10, !12, i64 16}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass