; LLVM IR from parallella/pal :: src/base (p_rank_to_coords)

; 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.team = type { ptr, ptr, i32, i32, i32, %union.p_coords, %union.p_coords, %union.p_coords }
%union.p_coords = type { %struct.anon }
%struct.anon = type { i32, i32, i32 }

; Function Attrs: nounwind uwtable
define dso_local i32 @p_rank_to_coords(ptr noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = call ptr @p_to_team(ptr noundef %12)
  store ptr %13, ptr %10, align 8, !tbaa !5
  %14 = load ptr, ptr %10, align 8, !tbaa !5
  %15 = call i32 @p_error(ptr noundef %14)
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %20

17:                                               ; preds = %4
  %18 = load ptr, ptr %10, align 8, !tbaa !5
  %19 = call i32 @p_error(ptr noundef %18)
  store i32 %19, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %71

20:                                               ; preds = %4
  %21 = load ptr, ptr %10, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.team, ptr %21, i32 0, i32 4
  %23 = load i32, ptr %22, align 8, !tbaa !11
  switch i32 %23, label %69 [
    i32 0, label %24
    i32 2, label %27
    i32 1, label %52
  ]

24:                                               ; preds = %20
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  store i32 %25, ptr %26, align 4, !tbaa !13
  br label %70

27:                                               ; preds = %20
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = load ptr, ptr %10, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.team, ptr %29, i32 0, i32 6
  %31 = getelementptr inbounds %struct.anon, ptr %30, i32 0, i32 1
  %32 = load i32, ptr %31, align 4, !tbaa !13
  %33 = load ptr, ptr %10, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.team, ptr %33, i32 0, i32 6
  %35 = getelementptr inbounds %struct.anon, ptr %34, i32 0, i32 0
  %36 = load i32, ptr %35, align 8, !tbaa !13
  %37 = mul nsw i32 %32, %36
  %38 = sdiv i32 %28, %37
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.anon, ptr %39, i32 0, i32 2
  store i32 %38, ptr %40, align 4, !tbaa !13
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.team, ptr %42, i32 0, i32 6
  %44 = getelementptr inbounds %struct.anon, ptr %43, i32 0, i32 1
  %45 = load i32, ptr %44, align 4, !tbaa !13
  %46 = srem i32 %41, %45
  %47 = load ptr, ptr %10, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.team, ptr %47, i32 0, i32 6
  %49 = getelementptr inbounds %struct.anon, ptr %48, i32 0, i32 0
  %50 = load i32, ptr %49, align 8, !tbaa !13
  %51 = mul nsw i32 %46, %50
  store i32 %51, ptr %7, align 4, !tbaa !9
  br label %52

52:                                               ; preds = %27, %20
  %53 = load i32, ptr %7, align 4, !tbaa !9
  %54 = load ptr, ptr %10, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.team, ptr %54, i32 0, i32 6
  %56 = getelementptr inbounds %struct.anon, ptr %55, i32 0, i32 0
  %57 = load i32, ptr %56, align 8, !tbaa !13
  %58 = sdiv i32 %53, %57
  %59 = load ptr, ptr %8, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.anon, ptr %59, i32 0, i32 1
  store i32 %58, ptr %60, align 4, !tbaa !13
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = load ptr, ptr %10, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.team, ptr %62, i32 0, i32 6
  %64 = getelementptr inbounds %struct.anon, ptr %63, i32 0, i32 0
  %65 = load i32, ptr %64, align 8, !tbaa !13
  %66 = srem i32 %61, %65
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.anon, ptr %67, i32 0, i32 0
  store i32 %66, ptr %68, align 4, !tbaa !13
  br label %70

69:                                               ; preds = %20
  store i32 -22, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %71

70:                                               ; preds = %52, %24
  store i32 0, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %71

71:                                               ; preds = %70, %69, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %72 = load i32, ptr %5, align 4
  ret i32 %72
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @p_to_team(ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @p_error(ptr noundef) #2

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

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 24}
!12 = !{!"team", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !7, i64 28, !7, i64 40, !7, i64 52}
!13 = !{!7, !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"

; Function Attrs: nounwind uwtable
define dso_local i32 @p_rank_to_coords(ptr noundef %0, i32 noundef %1, ptr nocapture noundef writeonly %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = tail call ptr @p_to_team(ptr noundef %0)
  %6 = tail call i32 @p_error(ptr noundef %5)
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %9, label %7

7:                                                ; preds = %4
  %8 = tail call i32 @p_error(ptr noundef %5)
  br label %32

9:                                                ; preds = %4
  %10 = getelementptr inbounds i8, ptr %5, i64 24
  %11 = load i32, ptr %10, align 8, !tbaa !5
  switch i32 %11, label %32 [
    i32 0, label %31
    i32 2, label %12
    i32 1, label %._crit_edge
  ]

._crit_edge:                                      ; preds = %9
  %.phi.trans.insert = getelementptr inbounds i8, ptr %5, i64 40
  %.pre = load i32, ptr %.phi.trans.insert, align 8, !tbaa !11
  br label %24

12:                                               ; preds = %9
  %13 = getelementptr inbounds i8, ptr %5, i64 40
  %14 = getelementptr inbounds i8, ptr %5, i64 44
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %16 = load i32, ptr %13, align 8, !tbaa !11
  %17 = mul nsw i32 %16, %15
  %18 = sdiv i32 %1, %17
  %19 = getelementptr inbounds i8, ptr %2, i64 8
  store i32 %18, ptr %19, align 4, !tbaa !11
  %20 = load i32, ptr %14, align 4, !tbaa !11
  %21 = srem i32 %1, %20
  %22 = load i32, ptr %13, align 8, !tbaa !11
  %23 = mul nsw i32 %22, %21
  br label %24

24:                                               ; preds = %._crit_edge, %12
  %25 = phi i32 [ %.pre, %._crit_edge ], [ %22, %12 ]
  %.019 = phi i32 [ %1, %._crit_edge ], [ %23, %12 ]
  %26 = getelementptr inbounds i8, ptr %5, i64 40
  %27 = sdiv i32 %.019, %25
  %28 = getelementptr inbounds i8, ptr %2, i64 4
  store i32 %27, ptr %28, align 4, !tbaa !11
  %29 = load i32, ptr %26, align 8, !tbaa !11
  %30 = srem i32 %.019, %29
  br label %31

31:                                               ; preds = %9, %24
  %storemerge = phi i32 [ %30, %24 ], [ %1, %9 ]
  store i32 %storemerge, ptr %2, align 4, !tbaa !11
  br label %32

32:                                               ; preds = %9, %31, %7
  %.0 = phi i32 [ %8, %7 ], [ 0, %31 ], [ -22, %9 ]
  ret i32 %.0
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @p_to_team(ptr noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @p_error(ptr noundef) local_unnamed_addr #1

attributes #0 = { 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 = { 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" }

!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, !10, i64 24}
!6 = !{!"team", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !8, i64 28, !8, i64 40, !8, i64 52}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!8, !8, i64 0}


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