; LLVM IR from PyCee/NUS_library :: src/gpu (NUS_single_command)

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

@nus_single_command = external hidden global %struct.anon, align 8
@.str = external hidden unnamed_addr constant [62 x i8], align 1
@vkBeginCommandBuffer = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @nus_single_command_begin(ptr noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca %struct.NUS_binding, align 8
  %5 = alloca %struct.VkCommandBufferBeginInfo, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = load ptr, ptr @nus_single_command, align 8, !tbaa !9
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %18

8:                                                ; preds = %1
  %9 = call i32 @nus_allocate_command_buffer(ptr noundef @nus_single_command, i8 noundef signext 1)
  %10 = icmp ne i32 %9, 1
  br i1 %10, label %11, label %13

11:                                               ; preds = %8
  %12 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  store i32 0, ptr %2, align 4
  br label %24

13:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 32, ptr %4) #5
  call void @nus_get_binding(ptr dead_on_unwind writable sret(%struct.NUS_binding) align 8 %4)
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 getelementptr inbounds (%struct.anon, ptr @nus_single_command, i32 0, i32 2), ptr align 8 %4, i64 32, i1 false), !tbaa.struct !14
  call void @llvm.lifetime.end.p0(i64 32, ptr %4) #5
  call void @llvm.memset.p0.i64(ptr align 8 %5, i8 0, i64 32, i1 false)
  %14 = getelementptr inbounds %struct.VkCommandBufferBeginInfo, ptr %5, i32 0, i32 0
  store i32 42, ptr %14, align 8, !tbaa !16
  %15 = getelementptr inbounds %struct.VkCommandBufferBeginInfo, ptr %5, i32 0, i32 1
  %16 = getelementptr inbounds %struct.VkCommandBufferBeginInfo, ptr %5, i32 0, i32 2
  store i32 1, ptr %16, align 8, !tbaa !17
  %17 = getelementptr inbounds %struct.VkCommandBufferBeginInfo, ptr %5, i32 0, i32 3
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 getelementptr inbounds (%struct.anon, ptr @nus_single_command, i32 0, i32 1), ptr align 8 %5, i64 32, i1 false), !tbaa.struct !18
  br label %18

18:                                               ; preds = %13, %1
  %19 = load ptr, ptr @vkBeginCommandBuffer, align 8, !tbaa !5
  %20 = load ptr, ptr @nus_single_command, align 8, !tbaa !9
  %21 = call i32 %19(ptr noundef %20, ptr noundef getelementptr inbounds (%struct.anon, ptr @nus_single_command, i32 0, i32 1))
  %22 = load ptr, ptr @nus_single_command, align 8, !tbaa !9
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %22, ptr %23, align 8, !tbaa !5
  store i32 1, ptr %2, align 4
  br label %24

24:                                               ; preds = %18, %11
  %25 = load i32, ptr %2, align 4
  ret i32 %25
}

declare i32 @nus_allocate_command_buffer(ptr noundef, i8 noundef signext) #1

declare i32 @printf(ptr noundef, ...) #1

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

declare void @nus_get_binding(ptr dead_on_unwind writable sret(%struct.NUS_binding) align 8) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { 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 0}
!10 = !{!"", !6, i64 0, !11, i64 8, !13, i64 40}
!11 = !{!"VkCommandBufferBeginInfo", !12, i64 0, !6, i64 8, !12, i64 16, !6, i64 24}
!12 = !{!"int", !7, i64 0}
!13 = !{!"NUS_binding", !6, i64 0, !6, i64 8, !6, i64 16, !12, i64 24}
!14 = !{i64 0, i64 8, !5, i64 8, i64 8, !5, i64 16, i64 8, !5, i64 24, i64 4, !15}
!15 = !{!12, !12, i64 0}
!16 = !{!11, !12, i64 0}
!17 = !{!11, !12, i64 16}
!18 = !{i64 0, i64 4, !15, i64 8, i64 8, !5, i64 16, i64 4, !15, i64 24, i64 8, !5}


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

@nus_single_command = external hidden global %struct.anon, align 8
@.str = external hidden unnamed_addr constant [62 x i8], align 1
@vkBeginCommandBuffer = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @nus_single_command_begin(ptr nocapture noundef writeonly %0) local_unnamed_addr #0 {
  %2 = alloca %struct.NUS_binding, align 8
  %3 = load ptr, ptr @nus_single_command, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %10

5:                                                ; preds = %1
  %6 = tail call i32 @nus_allocate_command_buffer(ptr noundef nonnull @nus_single_command, i8 noundef signext 1) #6
  %.not = icmp eq i32 %6, 1
  br i1 %.not, label %9, label %7

7:                                                ; preds = %5
  %8 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  br label %15

9:                                                ; preds = %5
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %2) #6
  call void @nus_get_binding(ptr dead_on_unwind nonnull writable sret(%struct.NUS_binding) align 8 %2) #6
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) getelementptr inbounds (i8, ptr @nus_single_command, i64 40), ptr noundef nonnull align 8 dereferenceable(32) %2, i64 32, i1 false), !tbaa.struct !13
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %2) #6
  store i32 42, ptr getelementptr inbounds (i8, ptr @nus_single_command, i64 8), align 8, !tbaa !15
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(12) getelementptr inbounds (i8, ptr @nus_single_command, i64 12), i8 0, i64 12, i1 false)
  store i32 1, ptr getelementptr inbounds (i8, ptr @nus_single_command, i64 24), align 8, !tbaa !15
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(12) getelementptr inbounds (i8, ptr @nus_single_command, i64 28), i8 0, i64 12, i1 false)
  %.pre = load ptr, ptr @nus_single_command, align 8, !tbaa !5
  br label %10

10:                                               ; preds = %9, %1
  %11 = phi ptr [ %.pre, %9 ], [ %3, %1 ]
  %12 = load ptr, ptr @vkBeginCommandBuffer, align 8, !tbaa !14
  %13 = call i32 %12(ptr noundef %11, ptr noundef nonnull getelementptr inbounds (i8, ptr @nus_single_command, i64 8)) #6
  %14 = load ptr, ptr @nus_single_command, align 8, !tbaa !5
  store ptr %14, ptr %0, align 8, !tbaa !14
  br label %15

15:                                               ; preds = %10, %7
  %.0 = phi i32 [ 0, %7 ], [ 1, %10 ]
  ret i32 %.0
}

declare i32 @nus_allocate_command_buffer(ptr noundef, i8 noundef signext) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

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

declare void @nus_get_binding(ptr dead_on_unwind writable sret(%struct.NUS_binding) align 8) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #4

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #5

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 = { nofree 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #6 = { 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 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !12, i64 40}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"VkCommandBufferBeginInfo", !11, i64 0, !7, i64 8, !11, i64 16, !7, i64 24}
!11 = !{!"int", !8, i64 0}
!12 = !{!"NUS_binding", !7, i64 0, !7, i64 8, !7, i64 16, !11, i64 24}
!13 = !{i64 0, i64 8, !14, i64 8, i64 8, !14, i64 16, i64 8, !14, i64 24, i64 4, !15}
!14 = !{!7, !7, i64 0}
!15 = !{!11, !11, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,MemCpyOptPass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass