; 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.TileMap = type { i16, 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 @allocateTileMap(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 @unpackTileMap(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 @allocateTileMap(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 %75

17:                                               ; preds = %14
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.TileMap, 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.TileMap, ptr %21, i32 0, i32 1
  store i16 %20, ptr %22, align 2, !tbaa !9
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.TileMap, ptr %23, i32 0, i32 2
  %25 = load i16, ptr %24, align 4, !tbaa !12
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.TileMap, ptr %26, i32 0, i32 2
  store i16 %25, ptr %27, align 4, !tbaa !12
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.TileMap, ptr %28, i32 0, i32 0
  store i16 0, ptr %29, align 8, !tbaa !13
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.TileMap, ptr %30, i32 0, i32 0
  %32 = load i16, ptr %31, align 8, !tbaa !13
  %33 = zext i16 %32 to i32
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %35, label %46

35:                                               ; preds = %17
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.TileMap, ptr %36, i32 0, i32 0
  %38 = load i16, ptr %37, align 8, !tbaa !13
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.TileMap, ptr %39, i32 0, i32 3
  %41 = load ptr, ptr %40, align 8, !tbaa !14
  %42 = load ptr, ptr %5, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.TileMap, ptr %42, i32 0, i32 3
  %44 = load ptr, ptr %43, align 8, !tbaa !14
  %45 = call i64 @unpack(i16 noundef zeroext %38, ptr noundef %41, ptr noundef %44)
  br label %74

46:                                               ; preds = %17
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.TileMap, ptr %47, i32 0, i32 3
  %49 = load ptr, ptr %48, align 8, !tbaa !14
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.TileMap, ptr %50, i32 0, i32 3
  %52 = load ptr, ptr %51, align 8, !tbaa !14
  %53 = icmp ne ptr %49, %52
  br i1 %53, label %54, label %73

54:                                               ; preds = %46
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #3
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.TileMap, ptr %55, i32 0, i32 1
  %57 = load i16, ptr %56, align 2, !tbaa !9
  %58 = zext i16 %57 to i32
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.TileMap, ptr %59, i32 0, i32 2
  %61 = load i16, ptr %60, align 4, !tbaa !12
  %62 = zext i16 %61 to i32
  %63 = mul nsw i32 %58, %62
  %64 = mul nsw i32 %63, 2
  %65 = trunc i32 %64 to i16
  store i16 %65, ptr %6, align 2, !tbaa !15
  %66 = load ptr, ptr %5, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.TileMap, ptr %66, i32 0, i32 3
  %68 = load ptr, ptr %67, align 8, !tbaa !14
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.TileMap, ptr %69, i32 0, i32 3
  %71 = load ptr, ptr %70, align 8, !tbaa !14
  %72 = load i16, ptr %6, align 2, !tbaa !15
  call void @memcpy(ptr noundef %68, ptr noundef %71, i16 noundef zeroext %72)
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #3
  br label %73

73:                                               ; preds = %54, %46
  br label %74

74:                                               ; preds = %73, %35
  br label %75

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

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, !11, i64 4, !6, i64 8}
!11 = !{!"short", !7, i64 0}
!12 = !{!10, !11, i64 4}
!13 = !{!10, !11, i64 0}
!14 = !{!10, !6, i64 8}
!15 = !{!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 @allocateTileMap(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 @unpackTileMap(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 @allocateTileMap(ptr noundef %0)
  %.not23 = icmp eq ptr %4, null
  br i1 %.not23, label %22, label %.thread

.thread:                                          ; preds = %2, %3
  %.028 = 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 %.028, i64 2
  store i16 %6, ptr %7, align 2, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %0, i64 4
  %9 = load i16, ptr %8, align 4, !tbaa !11
  %10 = getelementptr inbounds i8, ptr %.028, i64 4
  store i16 %9, ptr %10, align 4, !tbaa !11
  store i16 0, ptr %.028, align 8, !tbaa !12
  %11 = load i16, ptr %0, align 8, !tbaa !12
  %.not24 = icmp eq i16 %11, 0
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !13
  %14 = getelementptr inbounds i8, ptr %.028, i64 8
  %15 = load ptr, ptr %14, align 8, !tbaa !13
  br i1 %.not24, label %18, label %16

16:                                               ; preds = %.thread
  %17 = tail call i64 @unpack(i16 noundef zeroext %11, ptr noundef %13, ptr noundef %15)
  br label %22

18:                                               ; preds = %.thread
  %.not25 = icmp eq ptr %13, %15
  br i1 %.not25, label %22, label %19

19:                                               ; preds = %18
  %20 = shl i16 %6, 1
  %21 = mul i16 %20, %9
  tail call void @memcpy(ptr noundef %15, ptr noundef %13, i16 noundef zeroext %21) #2
  br label %22

22:                                               ; preds = %16, %19, %18, %3
  %.029 = phi ptr [ %.028, %16 ], [ %.028, %19 ], [ %.028, %18 ], [ null, %3 ]
  ret ptr %.029
}

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, !7, i64 4, !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 4}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !10, i64 8}


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