; LLVM IR from gem5/gem5 :: src/base/atomicio.ll (ZN4gem511atomic_readEiPvm)

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN4gem511atomic_readEiPvm(i32 noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %12 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %12, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  store i64 0, ptr %9, align 8, !tbaa !11
  br label %13

13:                                               ; preds = %49, %47, %3
  %14 = load i64, ptr %7, align 8, !tbaa !11
  %15 = load i64, ptr %9, align 8, !tbaa !11
  %16 = icmp ugt i64 %14, %15
  br i1 %16, label %17, label %50

17:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = load ptr, ptr %8, align 8, !tbaa !9
  %20 = load i64, ptr %9, align 8, !tbaa !11
  %21 = getelementptr inbounds i8, ptr %19, i64 %20
  %22 = load i64, ptr %7, align 8, !tbaa !11
  %23 = load i64, ptr %9, align 8, !tbaa !11
  %24 = sub i64 %22, %23
  %25 = call i64 @read(i32 noundef %18, ptr noundef %21, i64 noundef %24)
  store i64 %25, ptr %10, align 8, !tbaa !11
  %26 = load i64, ptr %10, align 8, !tbaa !11
  %27 = icmp eq i64 %26, 0
  br i1 %27, label %28, label %29

28:                                               ; preds = %17
  store i32 3, ptr %11, align 4
  br label %47

29:                                               ; preds = %17
  %30 = load i64, ptr %10, align 8, !tbaa !11
  %31 = icmp eq i64 %30, -1
  br i1 %31, label %32, label %43

32:                                               ; preds = %29
  %33 = call ptr @__errno_location() #5
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = icmp eq i32 %34, 4
  br i1 %35, label %40, label %36

36:                                               ; preds = %32
  %37 = call ptr @__errno_location() #5
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = icmp eq i32 %38, 11
  br i1 %39, label %40, label %41

40:                                               ; preds = %36, %32
  store i32 2, ptr %11, align 4
  br label %47, !llvm.loop !13

41:                                               ; preds = %36
  %42 = load i64, ptr %10, align 8, !tbaa !11
  store i64 %42, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %47

43:                                               ; preds = %29
  %44 = load i64, ptr %10, align 8, !tbaa !11
  %45 = load i64, ptr %9, align 8, !tbaa !11
  %46 = add i64 %45, %44
  store i64 %46, ptr %9, align 8, !tbaa !11
  store i32 0, ptr %11, align 4
  br label %47

47:                                               ; preds = %43, %41, %40, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %48 = load i32, ptr %11, align 4
  switch i32 %48, label %52 [
    i32 0, label %49
    i32 3, label %50
    i32 2, label %13
  ]

49:                                               ; preds = %47
  br label %13, !llvm.loop !13

50:                                               ; preds = %47, %13
  %51 = load i64, ptr %9, align 8, !tbaa !11
  store i64 %51, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %52

52:                                               ; preds = %50, %47
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %53 = load i64, ptr %4, align 8
  ret i64 %53
}

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

declare i64 @read(i32 noundef, ptr noundef, i64 noundef) #2

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #3

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

attributes #0 = { mustprogress 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 willreturn memory(none) "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 }
attributes #5 = { nounwind willreturn memory(none) }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; 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 nofree uwtable
define dso_local noundef i64 @_ZN4gem511atomic_readEiPvm(i32 noundef %0, ptr nocapture noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %3, %12
  %.01628 = phi i64 [ %.117, %12 ], [ 0, %3 ]
  %4 = getelementptr inbounds i8, ptr %1, i64 %.01628
  %5 = sub i64 %2, %.01628
  %6 = tail call i64 @read(i32 noundef %0, ptr noundef %4, i64 noundef %5)
  switch i64 %6, label %10 [
    i64 0, label %.thread
    i64 -1, label %7
  ]

7:                                                ; preds = %.lr.ph
  %8 = tail call ptr @__errno_location() #3
  %9 = load i32, ptr %8, align 4, !tbaa !5
  switch i32 %9, label %.thread [
    i32 4, label %12
    i32 11, label %12
  ], !llvm.loop !9

10:                                               ; preds = %.lr.ph
  %11 = add i64 %6, %.01628
  br label %12

12:                                               ; preds = %7, %7, %10
  %.117 = phi i64 [ %11, %10 ], [ %.01628, %7 ], [ %.01628, %7 ]
  %13 = icmp ult i64 %.117, %2
  br i1 %13, label %.lr.ph, label %.thread

.thread:                                          ; preds = %12, %.lr.ph, %7, %3
  %.2 = phi i64 [ 0, %3 ], [ -1, %7 ], [ %.01628, %.lr.ph ], [ %.117, %12 ]
  ret i64 %.2
}

; Function Attrs: nofree
declare noundef i64 @read(i32 noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #2

attributes #0 = { mustprogress nofree 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 = { nofree "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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 willreturn memory(none) }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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