; LLVM IR from Stephane-D/SGDK :: src (tools)

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

; 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: nounwind uwtable
declare dso_local ptr @allocateTileSet(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i64 @unpack(i16 noundef zeroext, ptr noundef, ptr noundef) #1

declare void @memcpy(ptr noundef, ptr noundef, i16 noundef zeroext) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @unpackTileSet(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i16, align 2
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = icmp ne ptr %7, null
  br i1 %8, label %9, label %11

9:                                                ; preds = %2
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %10, ptr %5, align 8, !tbaa !5
  br label %14

11:                                               ; preds = %2
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = call ptr @allocateTileSet(ptr noundef %12)
  store ptr %13, ptr %5, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %11, %9
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %65

17:                                               ; preds = %14
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.TileSet, ptr %18, i32 0, i32 1
  %20 = load i16, ptr %19, align 2, !tbaa !9
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.TileSet, ptr %21, i32 0, i32 1
  store i16 %20, ptr %22, align 2, !tbaa !9
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.TileSet, ptr %23, i32 0, i32 0
  store i16 0, ptr %24, align 8, !tbaa !12
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.TileSet, ptr %25, i32 0, i32 0
  %27 = load i16, ptr %26, align 8, !tbaa !12
  %28 = zext i16 %27 to i32
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %41

30:                                               ; preds = %17
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.TileSet, ptr %31, i32 0, i32 0
  %33 = load i16, ptr %32, align 8, !tbaa !12
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.TileSet, ptr %34, i32 0, i32 2
  %36 = load ptr, ptr %35, align 8, !tbaa !13
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.TileSet, ptr %37, i32 0, i32 2
  %39 = load ptr, ptr %38, align 8, !tbaa !13
  %40 = call i64 @unpack(i16 noundef zeroext %33, ptr noundef %36, ptr noundef %39)
  br label %64

41:                                               ; preds = %17
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.TileSet, ptr %42, i32 0, i32 2
  %44 = load ptr, ptr %43, align 8, !tbaa !13
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.TileSet, ptr %45, i32 0, i32 2
  %47 = load ptr, ptr %46, align 8, !tbaa !13
  %48 = icmp ne ptr %44, %47
  br i1 %48, label %49, label %63

49:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #3
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.TileSet, ptr %50, i32 0, i32 1
  %52 = load i16, ptr %51, align 2, !tbaa !9
  %53 = zext i16 %52 to i32
  %54 = mul nsw i32 %53, 32
  %55 = trunc i32 %54 to i16
  store i16 %55, ptr %6, align 2, !tbaa !14
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.TileSet, ptr %56, i32 0, i32 2
  %58 = load ptr, ptr %57, align 8, !tbaa !13
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.TileSet, ptr %59, i32 0, i32 2
  %61 = load ptr, ptr %60, align 8, !tbaa !13
  %62 = load i16, ptr %6, align 2, !tbaa !14
  call void @memcpy(ptr noundef %58, ptr noundef %61, i16 noundef zeroext %62)
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #3
  br label %63

63:                                               ; preds = %49, %41
  br label %64

64:                                               ; preds = %63, %30
  br label %65

65:                                               ; preds = %64, %14
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret ptr %66
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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, !11, i64 2}
!10 = !{!"", !11, i64 0, !11, i64 2, !6, i64 8}
!11 = !{!"short", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!10, !6, i64 8}
!14 = !{!11, !11, 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
declare dso_local ptr @allocateTileSet(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i64 @unpack(i16 noundef zeroext, ptr noundef, ptr noundef) local_unnamed_addr #0

declare void @memcpy(ptr noundef, ptr noundef, i16 noundef zeroext) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @unpackTileSet(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %.not = icmp eq ptr %1, null
  br i1 %.not, label %3, label %.thread

3:                                                ; preds = %2
  %4 = tail call ptr @allocateTileSet(ptr noundef %0)
  %.not20 = icmp eq ptr %4, null
  br i1 %.not20, label %18, label %.thread

.thread:                                          ; preds = %2, %3
  %.025 = phi ptr [ %4, %3 ], [ %1, %2 ]
  %5 = getelementptr inbounds i8, ptr %0, i64 2
  %6 = load i16, ptr %5, align 2, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %.025, i64 2
  store i16 %6, ptr %7, align 2, !tbaa !5
  store i16 0, ptr %.025, align 8, !tbaa !11
  %8 = load i16, ptr %0, align 8, !tbaa !11
  %.not21 = icmp eq i16 %8, 0
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %11 = getelementptr inbounds i8, ptr %.025, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !12
  br i1 %.not21, label %15, label %13

13:                                               ; preds = %.thread
  %14 = tail call i64 @unpack(i16 noundef zeroext %8, ptr noundef %10, ptr noundef %12)
  br label %18

15:                                               ; preds = %.thread
  %.not22 = icmp eq ptr %10, %12
  br i1 %.not22, label %18, label %16

16:                                               ; preds = %15
  %17 = shl i16 %6, 5
  tail call void @memcpy(ptr noundef %12, ptr noundef %10, i16 noundef zeroext %17) #2
  br label %18

18:                                               ; preds = %13, %16, %15, %3
  %.026 = phi ptr [ %.025, %13 ], [ %.025, %16 ], [ %.025, %15 ], [ null, %3 ]
  ret ptr %.026
}

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 = { "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, !7, i64 2}
!6 = !{!"", !7, i64 0, !7, i64 2, !10, i64 8}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!6, !10, i64 8}


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