; LLVM IR from bjourne/fast_snn_fpga :: src (opencl)

; 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.ocl_ctx_buf = type { ptr, i64, i64 }
%struct.ocl_ctx = type { ptr, ptr, ptr, i64, [20 x %struct.ocl_ctx_buf], i64, ptr, i64, ptr, ptr, i32 }

@.str.101 = external hidden unnamed_addr constant [39 x i8], align 1
@.str.102 = external hidden unnamed_addr constant [8 x i8], align 1

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

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

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

; 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) #2

declare ptr @clCreateBuffer(ptr noundef, i64 noundef, i64 noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @ocl_ctx_add_buffer(ptr noundef %0, ptr noundef byval(%struct.ocl_ctx_buf) align 8 %1) #3 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.ocl_ctx, ptr %9, i32 0, i32 3
  %11 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %11, ptr %6, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %12 = getelementptr inbounds %struct.ocl_ctx_buf, ptr %1, i32 0, i32 1
  %13 = load i64, ptr %12, align 8, !tbaa !14
  store i64 %13, ptr %7, align 8, !tbaa !13
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.ocl_ctx, ptr %14, i32 0, i32 2
  %16 = load ptr, ptr %15, align 8, !tbaa !16
  %17 = getelementptr inbounds %struct.ocl_ctx_buf, ptr %1, i32 0, i32 2
  %18 = load i64, ptr %17, align 8, !tbaa !17
  %19 = load i64, ptr %7, align 8, !tbaa !13
  %20 = call ptr @clCreateBuffer(ptr noundef %16, i64 noundef %18, i64 noundef %19, ptr noundef null, ptr noundef %5)
  %21 = getelementptr inbounds %struct.ocl_ctx_buf, ptr %1, i32 0, i32 0
  store ptr %20, ptr %21, align 8, !tbaa !18
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.ocl_ctx, ptr %22, i32 0, i32 4
  %24 = load i64, ptr %6, align 8, !tbaa !13
  %25 = getelementptr inbounds [20 x %struct.ocl_ctx_buf], ptr %23, i64 0, i64 %24
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %25, ptr align 8 %1, i64 24, i1 false), !tbaa.struct !19
  %26 = load i32, ptr %5, align 4, !tbaa !20
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %30

28:                                               ; preds = %2
  %29 = load i32, ptr %5, align 4, !tbaa !20
  store i32 %29, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %40

30:                                               ; preds = %2
  %31 = load i64, ptr %7, align 8, !tbaa !13
  %32 = load i64, ptr %6, align 8, !tbaa !13
  %33 = getelementptr inbounds %struct.ocl_ctx_buf, ptr %1, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !18
  %35 = call i32 (ptr, ...) @printf(ptr noundef @.str.101, ptr noundef @.str.102, i64 noundef %31, i64 noundef %32, ptr noundef %34)
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.ocl_ctx, ptr %36, i32 0, i32 3
  %38 = load i64, ptr %37, align 8, !tbaa !9
  %39 = add i64 %38, 1
  store i64 %39, ptr %37, align 8, !tbaa !9
  store i32 0, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %40

40:                                               ; preds = %30, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  %41 = load i32, ptr %3, align 4
  ret i32 %41
}

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #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, !11, i64 24}
!10 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !11, i64 24, !7, i64 32, !11, i64 512, !6, i64 520, !11, i64 528, !6, i64 536, !6, i64 544, !12, i64 552}
!11 = !{!"long", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!15, !11, i64 8}
!15 = !{!"", !6, i64 0, !11, i64 8, !11, i64 16}
!16 = !{!10, !6, i64 16}
!17 = !{!15, !11, i64 16}
!18 = !{!15, !6, i64 0}
!19 = !{i64 0, i64 8, !5, i64 8, i64 8, !13, i64 16, i64 8, !13}
!20 = !{!12, !12, 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"

%struct.ocl_ctx_buf = type { ptr, i64, i64 }

@.str.101 = external hidden unnamed_addr constant [39 x i8], align 1
@.str.102 = external hidden unnamed_addr constant [8 x i8], align 1

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

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

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

; 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) #2

declare ptr @clCreateBuffer(ptr noundef, i64 noundef, i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local i32 @ocl_ctx_add_buffer(ptr nocapture noundef %0, ptr nocapture noundef byval(%struct.ocl_ctx_buf) align 8 %1) local_unnamed_addr #4 {
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #5
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %1, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !12
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load ptr, ptr %8, align 8, !tbaa !14
  %10 = getelementptr inbounds i8, ptr %1, i64 16
  %11 = load i64, ptr %10, align 8, !tbaa !15
  %12 = call ptr @clCreateBuffer(ptr noundef %9, i64 noundef %11, i64 noundef %7, ptr noundef null, ptr noundef nonnull %3) #5
  store ptr %12, ptr %1, align 8, !tbaa !16
  %13 = getelementptr inbounds i8, ptr %0, i64 32
  %14 = getelementptr inbounds [20 x %struct.ocl_ctx_buf], ptr %13, i64 0, i64 %5
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %14, ptr noundef nonnull align 8 dereferenceable(24) %1, i64 24, i1 false), !tbaa.struct !17
  %15 = load i32, ptr %3, align 4, !tbaa !20
  %.not = icmp eq i32 %15, 0
  br i1 %.not, label %16, label %20

16:                                               ; preds = %2
  %17 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.101, ptr noundef nonnull @.str.102, i64 noundef %7, i64 noundef %5, ptr noundef %12)
  %18 = load i64, ptr %4, align 8, !tbaa !5
  %19 = add i64 %18, 1
  store i64 %19, ptr %4, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %2, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #5
  ret i32 %15
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !10, i64 24}
!6 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24, !8, i64 32, !10, i64 512, !7, i64 520, !10, i64 528, !7, i64 536, !7, i64 544, !11, i64 552}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!13, !10, i64 8}
!13 = !{!"", !7, i64 0, !10, i64 8, !10, i64 16}
!14 = !{!6, !7, i64 16}
!15 = !{!13, !10, i64 16}
!16 = !{!13, !7, i64 0}
!17 = !{i64 0, i64 8, !18, i64 8, i64 8, !19, i64 16, i64 8, !19}
!18 = !{!7, !7, i64 0}
!19 = !{!10, !10, i64 0}
!20 = !{!11, !11, i64 0}


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