; 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.Image = type { ptr, ptr, 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

declare ptr @MEM_alloc(i16 noundef zeroext) #1

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

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @getTileSetAllocSize(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @getTileMapAllocSize(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @allocateImage(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.Image, ptr %9, i32 0, i32 1
  %11 = load ptr, ptr %10, align 8, !tbaa !9
  store ptr %11, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.Image, ptr %12, i32 0, i32 2
  %14 = load ptr, ptr %13, align 8, !tbaa !11
  store ptr %14, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #3
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = call zeroext i16 @getTileSetAllocSize(ptr noundef %15)
  %17 = zext i16 %16 to i64
  %18 = add i64 %17, 16
  %19 = trunc i64 %18 to i16
  store i16 %19, ptr %5, align 2, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #3
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = call zeroext i16 @getTileMapAllocSize(ptr noundef %20)
  %22 = zext i16 %21 to i64
  %23 = add i64 %22, 16
  %24 = trunc i64 %23 to i16
  store i16 %24, ptr %6, align 2, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %25 = load i16, ptr %5, align 2, !tbaa !12
  %26 = zext i16 %25 to i32
  %27 = load i16, ptr %6, align 2, !tbaa !12
  %28 = zext i16 %27 to i32
  %29 = add nsw i32 %26, %28
  %30 = sext i32 %29 to i64
  %31 = add i64 %30, 24
  %32 = trunc i64 %31 to i16
  %33 = call ptr @MEM_alloc(i16 noundef zeroext %32)
  store ptr %33, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %34, ptr %8, align 8, !tbaa !5
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %52

37:                                               ; preds = %1
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %38, i64 24
  %40 = call ptr @allocateTileSetInternal(ptr noundef %39)
  %41 = load ptr, ptr %8, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.Image, ptr %41, i32 0, i32 1
  store ptr %40, ptr %42, align 8, !tbaa !9
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %43, i64 24
  %45 = load i16, ptr %5, align 2, !tbaa !12
  %46 = zext i16 %45 to i32
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i8, ptr %44, i64 %47
  %49 = call ptr @allocateTileMapInternal(ptr noundef %48)
  %50 = load ptr, ptr %8, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.Image, ptr %50, i32 0, i32 2
  store ptr %49, ptr %51, align 8, !tbaa !11
  br label %52

52:                                               ; preds = %37, %1
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret ptr %53
}

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

declare ptr @MEM_alloc(i16 noundef zeroext) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @getTileSetAllocSize(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @getTileMapAllocSize(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local ptr @allocateImage(ptr nocapture noundef readonly %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %0, i64 16
  %5 = load ptr, ptr %4, align 8, !tbaa !10
  %6 = tail call zeroext i16 @getTileSetAllocSize(ptr noundef %3)
  %7 = add i16 %6, 16
  %8 = tail call zeroext i16 @getTileMapAllocSize(ptr noundef %5)
  %9 = add i16 %8, 40
  %narrow = add i16 %9, %7
  %10 = tail call ptr @MEM_alloc(i16 noundef zeroext %narrow) #2
  %.not = icmp eq ptr %10, null
  br i1 %.not, label %19, label %11

11:                                               ; preds = %1
  %12 = getelementptr inbounds i8, ptr %10, i64 24
  %13 = tail call ptr @allocateTileSetInternal(ptr noundef nonnull %12)
  %14 = getelementptr inbounds i8, ptr %10, i64 8
  store ptr %13, ptr %14, align 8, !tbaa !5
  %15 = zext i16 %7 to i64
  %16 = getelementptr inbounds i8, ptr %12, i64 %15
  %17 = tail call ptr @allocateTileMapInternal(ptr noundef nonnull %16)
  %18 = getelementptr inbounds i8, ptr %10, i64 16
  store ptr %17, ptr %18, align 8, !tbaa !10
  br label %19

19:                                               ; preds = %11, %1
  ret ptr %10
}

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


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