; LLVM IR from chdb-io/chdb :: base/glibc-compatibility/musl/getauxval.ll_ (getauxval_environ)

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

@__auxv_secure = external hidden global i64, align 8
@__auxv_environ = external hidden global ptr, 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: nounwind willreturn memory(none)
declare ptr @__errno_location() #1

; 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 i64 @__getauxval_environ(i64 noundef %0) #2 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  store i64 %0, ptr %3, align 8, !tbaa !5
  %6 = load i64, ptr %3, align 8, !tbaa !5
  %7 = icmp eq i64 %6, 23
  br i1 %7, label %8, label %10

8:                                                ; preds = %1
  %9 = load i64, ptr @__auxv_secure, align 8, !tbaa !5
  store i64 %9, ptr %2, align 8
  br label %29

10:                                               ; preds = %1
  %11 = load ptr, ptr @__auxv_environ, align 8, !tbaa !9
  %12 = icmp ne ptr %11, null
  br i1 %12, label %13, label %27

13:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %14 = load i64, ptr %3, align 8, !tbaa !5
  %15 = call i64 @__find_auxv(i64 noundef %14)
  store i64 %15, ptr %4, align 8, !tbaa !5
  %16 = load i64, ptr %4, align 8, !tbaa !5
  %17 = icmp ne i64 %16, -1
  br i1 %17, label %18, label %23

18:                                               ; preds = %13
  %19 = load ptr, ptr @__auxv_environ, align 8, !tbaa !9
  %20 = load i64, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds i64, ptr %19, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !5
  store i64 %22, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %24

23:                                               ; preds = %13
  store i32 0, ptr %5, align 4
  br label %24

24:                                               ; preds = %23, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %25 = load i32, ptr %5, align 4
  switch i32 %25, label %31 [
    i32 0, label %26
    i32 1, label %29
  ]

26:                                               ; preds = %24
  br label %27

27:                                               ; preds = %26, %10
  %28 = call ptr @__errno_location() #4
  store i32 2, ptr %28, align 4, !tbaa !11
  store i64 0, ptr %2, align 8
  br label %29

29:                                               ; preds = %27, %24, %8
  %30 = load i64, ptr %2, align 8
  ret i64 %30

31:                                               ; preds = %24
  unreachable
}

; Function Attrs: nounwind uwtable
declare hidden i64 @__find_auxv(i64 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind willreturn memory(none) "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 }
attributes #4 = { nounwind willreturn memory(none) }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !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"

@__auxv_secure = external hidden local_unnamed_addr global i64, align 8
@__auxv_environ = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i64 @__getauxval_environ(i64 noundef %0) local_unnamed_addr #1 {
  %2 = icmp eq i64 %0, 23
  br i1 %2, label %3, label %5

3:                                                ; preds = %1
  %4 = load i64, ptr @__auxv_secure, align 8, !tbaa !5
  br label %14

5:                                                ; preds = %1
  %6 = load ptr, ptr @__auxv_environ, align 8, !tbaa !9
  %.not = icmp eq ptr %6, null
  br i1 %.not, label %.thread, label %7

7:                                                ; preds = %5
  %8 = tail call i64 @__find_auxv(i64 noundef %0)
  %.not9.not = icmp eq i64 %8, -1
  br i1 %.not9.not, label %.thread, label %9

9:                                                ; preds = %7
  %10 = load ptr, ptr @__auxv_environ, align 8, !tbaa !9
  %11 = getelementptr inbounds i64, ptr %10, i64 %8
  %12 = load i64, ptr %11, align 8, !tbaa !5
  br label %14

.thread:                                          ; preds = %7, %5
  %13 = tail call ptr @__errno_location() #2
  store i32 2, ptr %13, align 4, !tbaa !11
  br label %14

14:                                               ; preds = %9, %.thread, %3
  %.1 = phi i64 [ %4, %3 ], [ %12, %9 ], [ 0, %.thread ]
  ret i64 %.1
}

; Function Attrs: nounwind uwtable
declare hidden i64 @__find_auxv(i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 willreturn memory(none) }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}


; Effective O3 passes: GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass