; LLVM IR from libtcod/libtcod :: src/libtcod (console)

; 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.TCOD_internal_context_t = type { i32, i32, i8, i8, i8, i32, i32, i32, [512 x i8], [512 x i8], ptr, ptr, ptr, i32, i8, i32, i32, i32, i32, i32, i32, ptr, %struct.TCOD_ColorRGB, i8, %struct.TCOD_key_t, i8, i8, i8, ptr, ptr }
%struct.TCOD_ColorRGB = type { i8, i8, i8 }
%struct.TCOD_key_t = type { i32, i8, [32 x i8], i8, i8, i8, i8, i8, i8, i8, i8 }
%struct.TCOD_Console = type { i32, i32, ptr, i32, i32, %struct.TCOD_ColorRGB, %struct.TCOD_ColorRGB, i8, %struct.TCOD_ColorRGB, i32, ptr, ptr }

@TCOD_ctx = external global %struct.TCOD_internal_context_t, align 8

; 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
define dso_local void @TCOD_console_delete(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = icmp ne ptr %4, null
  br i1 %5, label %6, label %8

6:                                                ; preds = %1
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  br label %10

8:                                                ; preds = %1
  %9 = load ptr, ptr getelementptr inbounds (%struct.TCOD_internal_context_t, ptr @TCOD_ctx, i32 0, i32 12), align 8, !tbaa !9
  br label %10

10:                                               ; preds = %8, %6
  %11 = phi ptr [ %7, %6 ], [ %9, %8 ]
  store ptr %11, ptr %3, align 8, !tbaa !5
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %27

14:                                               ; preds = %10
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.TCOD_Console, ptr %15, i32 0, i32 11
  %17 = load ptr, ptr %16, align 8, !tbaa !15
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %24

19:                                               ; preds = %14
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.TCOD_Console, ptr %20, i32 0, i32 11
  %22 = load ptr, ptr %21, align 8, !tbaa !15
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  call void %22(ptr noundef %23)
  br label %24

24:                                               ; preds = %19, %14
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  call void @TCOD_console_data_free(ptr noundef %25)
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  call void @free(ptr noundef %26) #4
  br label %27

27:                                               ; preds = %24, %10
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = load ptr, ptr getelementptr inbounds (%struct.TCOD_internal_context_t, ptr @TCOD_ctx, i32 0, i32 12), align 8, !tbaa !9
  %30 = icmp eq ptr %28, %29
  br i1 %30, label %31, label %32

31:                                               ; preds = %27
  store ptr null, ptr getelementptr inbounds (%struct.TCOD_internal_context_t, ptr @TCOD_ctx, i32 0, i32 12), align 8, !tbaa !9
  br label %32

32:                                               ; preds = %31, %27
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = icmp eq ptr %33, null
  br i1 %34, label %35, label %36

35:                                               ; preds = %32
  call void @TCOD_sys_shutdown()
  br label %36

36:                                               ; preds = %35, %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @TCOD_console_data_free(ptr noundef) #1

; Function Attrs: nounwind
declare void @free(ptr noundef) #2

declare void @TCOD_sys_shutdown() #3

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 = { nounwind "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 = { "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 #4 = { 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 1064}
!10 = !{!"TCOD_internal_context_t", !11, i64 0, !11, i64 4, !12, i64 8, !12, i64 9, !12, i64 10, !11, i64 12, !11, i64 16, !11, i64 20, !7, i64 24, !7, i64 536, !6, i64 1048, !6, i64 1056, !6, i64 1064, !11, i64 1072, !12, i64 1076, !11, i64 1080, !11, i64 1084, !11, i64 1088, !11, i64 1092, !11, i64 1096, !11, i64 1100, !6, i64 1104, !13, i64 1112, !7, i64 1115, !14, i64 1116, !12, i64 1164, !12, i64 1165, !12, i64 1166, !6, i64 1168, !6, i64 1176}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_Bool", !7, i64 0}
!13 = !{!"TCOD_ColorRGB", !7, i64 0, !7, i64 1, !7, i64 2}
!14 = !{!"TCOD_key_t", !11, i64 0, !7, i64 4, !7, i64 5, !12, i64 37, !12, i64 38, !12, i64 39, !12, i64 40, !12, i64 41, !12, i64 42, !12, i64 43, !12, i64 44}
!15 = !{!16, !6, i64 48}
!16 = !{!"TCOD_Console", !11, i64 0, !11, i64 4, !6, i64 8, !11, i64 16, !11, i64 20, !13, i64 24, !13, i64 27, !12, i64 30, !13, i64 31, !11, i64 36, !6, i64 40, !6, i64 48}


; 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"

%struct.TCOD_internal_context_t = type { i32, i32, i8, i8, i8, i32, i32, i32, [512 x i8], [512 x i8], ptr, ptr, ptr, i32, i8, i32, i32, i32, i32, i32, i32, ptr, %struct.TCOD_ColorRGB, i8, %struct.TCOD_key_t, i8, i8, i8, ptr, ptr }
%struct.TCOD_ColorRGB = type { i8, i8, i8 }
%struct.TCOD_key_t = type { i32, i8, [32 x i8], i8, i8, i8, i8, i8, i8, i8, i8 }

@TCOD_ctx = external local_unnamed_addr global %struct.TCOD_internal_context_t, align 8

; Function Attrs: nounwind uwtable
define dso_local void @TCOD_console_delete(ptr noundef %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  %2 = load ptr, ptr getelementptr inbounds (i8, ptr @TCOD_ctx, i64 1064), align 8
  %3 = select i1 %.not, ptr %2, ptr %0
  %.not11 = icmp eq ptr %3, null
  br i1 %.not11, label %9, label %4

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %3, i64 48
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %.not12 = icmp eq ptr %6, null
  br i1 %.not12, label %8, label %7

7:                                                ; preds = %4
  tail call void %6(ptr noundef nonnull %3) #3
  br label %8

8:                                                ; preds = %7, %4
  tail call void @TCOD_console_data_free(ptr noundef nonnull %3)
  tail call void @free(ptr noundef nonnull %3) #3
  %.pre = load ptr, ptr getelementptr inbounds (i8, ptr @TCOD_ctx, i64 1064), align 8, !tbaa !13
  br label %9

9:                                                ; preds = %8, %1
  %10 = phi ptr [ %.pre, %8 ], [ %2, %1 ]
  %11 = icmp eq ptr %3, %10
  br i1 %11, label %12, label %13

12:                                               ; preds = %9
  store ptr null, ptr getelementptr inbounds (i8, ptr @TCOD_ctx, i64 1064), align 8, !tbaa !13
  br label %13

13:                                               ; preds = %12, %9
  br i1 %.not, label %14, label %15

14:                                               ; preds = %13
  tail call void @TCOD_sys_shutdown() #3
  br label %15

15:                                               ; preds = %14, %13
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden void @TCOD_console_data_free(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #1

declare void @TCOD_sys_shutdown() local_unnamed_addr #2

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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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, !10, i64 48}
!6 = !{!"TCOD_Console", !7, i64 0, !7, i64 4, !10, i64 8, !7, i64 16, !7, i64 20, !11, i64 24, !11, i64 27, !12, i64 30, !11, i64 31, !7, i64 36, !10, i64 40, !10, i64 48}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"TCOD_ColorRGB", !8, i64 0, !8, i64 1, !8, i64 2}
!12 = !{!"_Bool", !8, i64 0}
!13 = !{!14, !10, i64 1064}
!14 = !{!"TCOD_internal_context_t", !7, i64 0, !7, i64 4, !12, i64 8, !12, i64 9, !12, i64 10, !7, i64 12, !7, i64 16, !7, i64 20, !8, i64 24, !8, i64 536, !10, i64 1048, !10, i64 1056, !10, i64 1064, !7, i64 1072, !12, i64 1076, !7, i64 1080, !7, i64 1084, !7, i64 1088, !7, i64 1092, !7, i64 1096, !7, i64 1100, !10, i64 1104, !11, i64 1112, !8, i64 1115, !15, i64 1116, !12, i64 1164, !12, i64 1165, !12, i64 1166, !10, i64 1168, !10, i64 1176}
!15 = !{!"TCOD_key_t", !7, i64 0, !8, i64 4, !8, i64 5, !12, i64 37, !12, i64 38, !12, i64 39, !12, i64 40, !12, i64 41, !12, i64 42, !12, i64 43, !12, i64 44}


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