; LLVM IR from chdb-io/chdb :: base/glibc-compatibility/musl (futimens)

; 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.timeval = type { i64, i64 }
%struct.timespec = type { i64, i64 }

; Function Attrs: nounwind uwtable
define dso_local i32 @futimens(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca [2 x %struct.timeval], align 16
  %10 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = load ptr, ptr %5, align 8, !tbaa !9
  %13 = call i64 (i64, ...) @__syscall(i64 noundef 280, i32 noundef %11, i32 noundef 0, ptr noundef %12, i32 noundef 0)
  %14 = trunc i64 %13 to i32
  store i32 %14, ptr %6, align 4, !tbaa !5
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = icmp ne i32 %15, -38
  br i1 %16, label %17, label %22

17:                                               ; preds = %2
  %18 = load i32, ptr %6, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = call i64 @__syscall_ret(i64 noundef %19)
  %21 = trunc i64 %20 to i32
  store i32 %21, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %123

22:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store ptr null, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 32, ptr %9) #3
  %23 = load ptr, ptr %5, align 8, !tbaa !9
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %99

25:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %26 = getelementptr inbounds [2 x %struct.timeval], ptr %9, i64 0, i64 0
  store ptr %26, ptr %8, align 8, !tbaa !9
  store i32 0, ptr %10, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %92, %25
  %28 = load i32, ptr %10, align 4, !tbaa !5
  %29 = icmp slt i32 %28, 2
  br i1 %29, label %30, label %95

30:                                               ; preds = %27
  %31 = load ptr, ptr %5, align 8, !tbaa !9
  %32 = load i32, ptr %10, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds %struct.timespec, ptr %31, i64 %33
  %35 = getelementptr inbounds %struct.timespec, ptr %34, i32 0, i32 1
  %36 = load i64, ptr %35, align 8, !tbaa !11
  %37 = icmp uge i64 %36, 1000000000
  br i1 %37, label %38, label %70

38:                                               ; preds = %30
  %39 = load ptr, ptr %5, align 8, !tbaa !9
  %40 = load i32, ptr %10, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds %struct.timespec, ptr %39, i64 %41
  %43 = getelementptr inbounds %struct.timespec, ptr %42, i32 0, i32 1
  %44 = load i64, ptr %43, align 8, !tbaa !11
  %45 = icmp eq i64 %44, 1073741823
  br i1 %45, label %46, label %56

46:                                               ; preds = %38
  %47 = load ptr, ptr %5, align 8, !tbaa !9
  %48 = load i32, ptr %10, align 4, !tbaa !5
  %49 = sub nsw i32 1, %48
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds %struct.timespec, ptr %47, i64 %50
  %52 = getelementptr inbounds %struct.timespec, ptr %51, i32 0, i32 1
  %53 = load i64, ptr %52, align 8, !tbaa !11
  %54 = icmp eq i64 %53, 1073741823
  br i1 %54, label %55, label %56

55:                                               ; preds = %46
  store ptr null, ptr %8, align 8, !tbaa !9
  br label %95

56:                                               ; preds = %46, %38
  %57 = load ptr, ptr %5, align 8, !tbaa !9
  %58 = load i32, ptr %10, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds %struct.timespec, ptr %57, i64 %59
  %61 = getelementptr inbounds %struct.timespec, ptr %60, i32 0, i32 1
  %62 = load i64, ptr %61, align 8, !tbaa !11
  %63 = icmp eq i64 %62, 1073741822
  br i1 %63, label %64, label %67

64:                                               ; preds = %56
  %65 = call i64 @__syscall_ret(i64 noundef -38)
  %66 = trunc i64 %65 to i32
  store i32 %66, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %96

67:                                               ; preds = %56
  %68 = call i64 @__syscall_ret(i64 noundef -22)
  %69 = trunc i64 %68 to i32
  store i32 %69, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %96

70:                                               ; preds = %30
  %71 = load ptr, ptr %5, align 8, !tbaa !9
  %72 = load i32, ptr %10, align 4, !tbaa !5
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds %struct.timespec, ptr %71, i64 %73
  %75 = getelementptr inbounds %struct.timespec, ptr %74, i32 0, i32 0
  %76 = load i64, ptr %75, align 8, !tbaa !14
  %77 = load i32, ptr %10, align 4, !tbaa !5
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds [2 x %struct.timeval], ptr %9, i64 0, i64 %78
  %80 = getelementptr inbounds %struct.timeval, ptr %79, i32 0, i32 0
  store i64 %76, ptr %80, align 16, !tbaa !15
  %81 = load ptr, ptr %5, align 8, !tbaa !9
  %82 = load i32, ptr %10, align 4, !tbaa !5
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds %struct.timespec, ptr %81, i64 %83
  %85 = getelementptr inbounds %struct.timespec, ptr %84, i32 0, i32 1
  %86 = load i64, ptr %85, align 8, !tbaa !11
  %87 = sdiv i64 %86, 1000
  %88 = load i32, ptr %10, align 4, !tbaa !5
  %89 = sext i32 %88 to i64
  %90 = getelementptr inbounds [2 x %struct.timeval], ptr %9, i64 0, i64 %89
  %91 = getelementptr inbounds %struct.timeval, ptr %90, i32 0, i32 1
  store i64 %87, ptr %91, align 8, !tbaa !17
  br label %92

92:                                               ; preds = %70
  %93 = load i32, ptr %10, align 4, !tbaa !5
  %94 = add nsw i32 %93, 1
  store i32 %94, ptr %10, align 4, !tbaa !5
  br label %27, !llvm.loop !18

95:                                               ; preds = %55, %27
  store i32 0, ptr %7, align 4
  br label %96

96:                                               ; preds = %95, %67, %64
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %97 = load i32, ptr %7, align 4
  switch i32 %97, label %122 [
    i32 0, label %98
  ]

98:                                               ; preds = %96
  br label %99

99:                                               ; preds = %98, %22
  %100 = load i32, ptr %4, align 4, !tbaa !5
  %101 = load ptr, ptr %8, align 8, !tbaa !9
  %102 = call i64 (i64, ...) @__syscall(i64 noundef 261, i32 noundef %100, i32 noundef 0, ptr noundef %101)
  %103 = trunc i64 %102 to i32
  store i32 %103, ptr %6, align 4, !tbaa !5
  %104 = load i32, ptr %6, align 4, !tbaa !5
  %105 = icmp ne i32 %104, -38
  br i1 %105, label %109, label %106

106:                                              ; preds = %99
  %107 = load i32, ptr %4, align 4, !tbaa !5
  %108 = icmp ne i32 %107, -100
  br i1 %108, label %109, label %114

109:                                              ; preds = %106, %99
  %110 = load i32, ptr %6, align 4, !tbaa !5
  %111 = sext i32 %110 to i64
  %112 = call i64 @__syscall_ret(i64 noundef %111)
  %113 = trunc i64 %112 to i32
  store i32 %113, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %122

114:                                              ; preds = %106
  %115 = load ptr, ptr %8, align 8, !tbaa !9
  %116 = call i64 (i64, ...) @__syscall(i64 noundef 235, i32 noundef 0, ptr noundef %115)
  %117 = trunc i64 %116 to i32
  store i32 %117, ptr %6, align 4, !tbaa !5
  %118 = load i32, ptr %6, align 4, !tbaa !5
  %119 = sext i32 %118 to i64
  %120 = call i64 @__syscall_ret(i64 noundef %119)
  %121 = trunc i64 %120 to i32
  store i32 %121, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %122

122:                                              ; preds = %114, %109, %96
  call void @llvm.lifetime.end.p0(i64 32, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %123

123:                                              ; preds = %122, %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %124 = load i32, ptr %3, align 4
  ret i32 %124
}

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

declare hidden i64 @__syscall(i64 noundef, ...) #2

declare hidden i64 @__syscall_ret(i64 noundef) #2

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 0}
!6 = !{!"int", !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, !13, i64 8}
!12 = !{!"timespec", !13, i64 0, !13, i64 8}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !13, i64 0}
!15 = !{!16, !13, i64 0}
!16 = !{!"timeval", !13, i64 0, !13, i64 8}
!17 = !{!16, !13, i64 8}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; 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.timeval = type { i64, i64 }
%struct.timespec = type { i64, i64 }

; Function Attrs: nounwind uwtable
define dso_local i32 @futimens(i32 noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = alloca [2 x %struct.timeval], align 16
  %4 = tail call i64 (i64, ...) @__syscall(i64 noundef 280, i32 noundef %0, i32 noundef 0, ptr noundef %1, i32 noundef 0) #3
  %5 = and i64 %4, 4294967295
  %.not = icmp eq i64 %5, 4294967258
  br i1 %.not, label %9, label %6

6:                                                ; preds = %2
  %sext40 = shl i64 %4, 32
  %7 = ashr exact i64 %sext40, 32
  %8 = tail call i64 @__syscall_ret(i64 noundef %7) #3
  br label %44

9:                                                ; preds = %2
  call void @llvm.lifetime.start.p0(i64 32, ptr nonnull %3) #3
  %.not38 = icmp eq ptr %1, null
  br i1 %.not38, label %.thread41, label %.preheader.preheader

.preheader.preheader:                             ; preds = %9
  %10 = getelementptr inbounds i8, ptr %1, i64 8
  %11 = load i64, ptr %10, align 8, !tbaa !5
  %12 = icmp ugt i64 %11, 999999999
  br i1 %12, label %13, label %.preheader.1

13:                                               ; preds = %.preheader.1, %.preheader.preheader
  %.046.lcssa.wide.neg = phi i64 [ 1, %.preheader.preheader ], [ 0, %.preheader.1 ]
  %.lcssa = phi i64 [ %11, %.preheader.preheader ], [ %25, %.preheader.1 ]
  switch i64 %.lcssa, label %.thread [
    i64 1073741823, label %14
    i64 1073741822, label %18
  ]

14:                                               ; preds = %13
  %15 = getelementptr inbounds %struct.timespec, ptr %1, i64 %.046.lcssa.wide.neg, i32 1
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = icmp eq i64 %16, 1073741823
  br i1 %17, label %.thread41, label %.thread

18:                                               ; preds = %13
  %19 = tail call i64 @__syscall_ret(i64 noundef -38) #3
  br label %43

.thread:                                          ; preds = %13, %14
  %20 = tail call i64 @__syscall_ret(i64 noundef -22) #3
  br label %43

.preheader.1:                                     ; preds = %.preheader.preheader
  %21 = load i64, ptr %1, align 8, !tbaa !10
  store i64 %21, ptr %3, align 16, !tbaa !11
  %.lhs.trunc = trunc nuw i64 %11 to i32
  %22 = udiv i32 %.lhs.trunc, 1000
  %.zext = zext nneg i32 %22 to i64
  %23 = getelementptr inbounds i8, ptr %3, i64 8
  store i64 %.zext, ptr %23, align 8, !tbaa !13
  %24 = getelementptr inbounds i8, ptr %1, i64 24
  %25 = load i64, ptr %24, align 8, !tbaa !5
  %26 = icmp ugt i64 %25, 999999999
  br i1 %26, label %13, label %.thread41.loopexit

.thread41.loopexit:                               ; preds = %.preheader.1
  %27 = getelementptr inbounds i8, ptr %1, i64 16
  %28 = load i64, ptr %27, align 8, !tbaa !10
  %29 = getelementptr inbounds i8, ptr %3, i64 16
  store i64 %28, ptr %29, align 16, !tbaa !11
  %.lhs.trunc.1 = trunc nuw i64 %25 to i32
  %30 = udiv i32 %.lhs.trunc.1, 1000
  %.zext.1 = zext nneg i32 %30 to i64
  %31 = getelementptr inbounds i8, ptr %3, i64 24
  store i64 %.zext.1, ptr %31, align 8, !tbaa !13
  br label %.thread41

.thread41:                                        ; preds = %.thread41.loopexit, %14, %9
  %.2 = phi ptr [ null, %9 ], [ null, %14 ], [ %3, %.thread41.loopexit ]
  %32 = call i64 (i64, ...) @__syscall(i64 noundef 261, i32 noundef %0, i32 noundef 0, ptr noundef %.2) #3
  %33 = and i64 %32, 4294967295
  %34 = icmp ne i64 %33, 4294967258
  %35 = icmp ne i32 %0, -100
  %or.cond = or i1 %35, %34
  br i1 %or.cond, label %36, label %39

36:                                               ; preds = %.thread41
  %sext39 = shl i64 %32, 32
  %37 = ashr exact i64 %sext39, 32
  %38 = call i64 @__syscall_ret(i64 noundef %37) #3
  br label %43

39:                                               ; preds = %.thread41
  %40 = call i64 (i64, ...) @__syscall(i64 noundef 235, i32 noundef 0, ptr noundef %.2) #3
  %sext = shl i64 %40, 32
  %41 = ashr exact i64 %sext, 32
  %42 = call i64 @__syscall_ret(i64 noundef %41) #3
  br label %43

43:                                               ; preds = %18, %.thread, %39, %36
  %.133.in = phi i64 [ %38, %36 ], [ %42, %39 ], [ %19, %18 ], [ %20, %.thread ]
  call void @llvm.lifetime.end.p0(i64 32, ptr nonnull %3) #3
  br label %44

44:                                               ; preds = %43, %6
  %.234.in = phi i64 [ %8, %6 ], [ %.133.in, %43 ]
  %.234 = trunc i64 %.234.in to i32
  ret i32 %.234
}

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

declare hidden i64 @__syscall(i64 noundef, ...) local_unnamed_addr #2

declare hidden i64 @__syscall_ret(i64 noundef) local_unnamed_addr #2

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

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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !7, i64 8}
!6 = !{!"timespec", !7, i64 0, !7, i64 8}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 0}
!11 = !{!12, !7, i64 0}
!12 = !{!"timeval", !7, i64 0, !7, i64 8}
!13 = !{!12, !7, i64 8}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass