; LLVM IR from taichi-dev/taichi :: taichi/runtime/llvm/runtime_module (runtime)

; 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: 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: mustprogress nounwind uwtable
define dso_local zeroext i16 @atomic_add_partial_bits_b16(ptr noundef %0, i32 noundef %1, i32 noundef %2, i16 noundef zeroext %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i16, align 2
  %9 = alloca i16, align 2
  %10 = alloca i16, align 2
  %11 = alloca i16, align 2
  %12 = alloca i8, align 1
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i16 %3, ptr %8, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #2
  %13 = load i32, ptr %7, align 4, !tbaa !9
  %14 = sub i32 16, %13
  %15 = shl i32 -1, %14
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = sub i32 16, %16
  %18 = load i32, ptr %7, align 4, !tbaa !9
  %19 = sub i32 %17, %18
  %20 = ashr i32 %15, %19
  %21 = trunc i32 %20 to i16
  store i16 %21, ptr %9, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #2
  store i16 0, ptr %10, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #2
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = load i16, ptr %22, align 2, !tbaa !11
  store i16 %23, ptr %11, align 2, !tbaa !11
  br label %24

24:                                               ; preds = %56, %4
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = load i16, ptr %25, align 2, !tbaa !11
  store i16 %26, ptr %11, align 2, !tbaa !11
  %27 = load i16, ptr %11, align 2, !tbaa !11
  %28 = zext i16 %27 to i32
  %29 = load i16, ptr %8, align 2, !tbaa !11
  %30 = zext i16 %29 to i32
  %31 = load i32, ptr %6, align 4, !tbaa !9
  %32 = shl i32 %30, %31
  %33 = add nsw i32 %28, %32
  %34 = trunc i32 %33 to i16
  store i16 %34, ptr %10, align 2, !tbaa !11
  %35 = load i16, ptr %11, align 2, !tbaa !11
  %36 = zext i16 %35 to i32
  %37 = load i16, ptr %9, align 2, !tbaa !11
  %38 = zext i16 %37 to i32
  %39 = xor i32 %38, -1
  %40 = and i32 %36, %39
  %41 = load i16, ptr %10, align 2, !tbaa !11
  %42 = zext i16 %41 to i32
  %43 = load i16, ptr %9, align 2, !tbaa !11
  %44 = zext i16 %43 to i32
  %45 = and i32 %42, %44
  %46 = or i32 %40, %45
  %47 = trunc i32 %46 to i16
  store i16 %47, ptr %10, align 2, !tbaa !11
  br label %48

48:                                               ; preds = %24
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = load i16, ptr %11, align 2
  %51 = load i16, ptr %10, align 2
  %52 = cmpxchg weak ptr %49, i16 %50, i16 %51 seq_cst seq_cst, align 2
  %53 = extractvalue { i16, i1 } %52, 0
  %54 = extractvalue { i16, i1 } %52, 1
  br i1 %54, label %56, label %55

55:                                               ; preds = %48
  store i16 %53, ptr %11, align 2
  br label %56

56:                                               ; preds = %55, %48
  %57 = zext i1 %54 to i8
  store i8 %57, ptr %12, align 1, !tbaa !13
  %58 = load i8, ptr %12, align 1, !tbaa !13, !range !15, !noundef !16
  %59 = trunc i8 %58 to i1
  %60 = xor i1 %59, true
  br i1 %60, label %24, label %61, !llvm.loop !17

61:                                               ; preds = %56
  %62 = load i16, ptr %11, align 2, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #2
  ret i16 %62
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"bool", !7, i64 0}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = distinct !{!17, !18}
!18 = !{!"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 norecurse nounwind memory(argmem: readwrite) uwtable
define dso_local zeroext i16 @atomic_add_partial_bits_b16(ptr nocapture noundef %0, i32 noundef %1, i32 noundef %2, i16 noundef zeroext %3) local_unnamed_addr #0 {
  %5 = sub i32 16, %2
  %6 = shl nsw i32 -1, %5
  %7 = add i32 %1, %2
  %8 = sub i32 16, %7
  %9 = ashr i32 %6, %8
  %10 = zext i16 %3 to i32
  %11 = shl i32 %10, %1
  %12 = trunc i32 %11 to i16
  %13 = xor i32 %9, -1
  br label %14

14:                                               ; preds = %14, %4
  %15 = load i16, ptr %0, align 2, !tbaa !5
  %16 = zext i16 %15 to i32
  %17 = add i16 %15, %12
  %18 = and i32 %16, %13
  %19 = zext i16 %17 to i32
  %20 = and i32 %9, %19
  %21 = or disjoint i32 %20, %18
  %22 = trunc nuw i32 %21 to i16
  %23 = cmpxchg weak ptr %0, i16 %15, i16 %22 seq_cst seq_cst, align 2
  %24 = extractvalue { i16, i1 } %23, 1
  br i1 %24, label %25, label %14, !llvm.loop !9

25:                                               ; preds = %14
  ret i16 %15
}

attributes #0 = { mustprogress nofree norecurse nounwind memory(argmem: readwrite) 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" }

!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 = !{!"short", !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,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass