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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @dup3(i32 noundef %0, i32 noundef %1, i32 noundef %2) #0 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %11 = load i32, ptr %6, align 4, !tbaa !5
  %12 = icmp eq i32 %10, %11
  br i1 %12, label %13, label %16

13:                                               ; preds = %3
  %14 = call i64 @__syscall_ret(i64 noundef -22)
  %15 = trunc i64 %14 to i32
  store i32 %15, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %58

16:                                               ; preds = %3
  %17 = load i32, ptr %7, align 4, !tbaa !5
  %18 = and i32 %17, 524288
  %19 = icmp ne i32 %18, 0
  br i1 %19, label %20, label %38

20:                                               ; preds = %16
  br label %21

21:                                               ; preds = %28, %20
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = call i64 (i64, ...) @__syscall(i64 noundef 292, i32 noundef %22, i32 noundef %23, i32 noundef %24)
  %26 = trunc i64 %25 to i32
  store i32 %26, ptr %8, align 4, !tbaa !5
  %27 = icmp eq i32 %26, -16
  br i1 %27, label %28, label %29

28:                                               ; preds = %21
  br label %21, !llvm.loop !9

29:                                               ; preds = %21
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = icmp ne i32 %30, -38
  br i1 %31, label %32, label %37

32:                                               ; preds = %29
  %33 = load i32, ptr %8, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = call i64 @__syscall_ret(i64 noundef %34)
  %36 = trunc i64 %35 to i32
  store i32 %36, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %58

37:                                               ; preds = %29
  br label %38

38:                                               ; preds = %37, %16
  br label %39

39:                                               ; preds = %45, %38
  %40 = load i32, ptr %5, align 4, !tbaa !5
  %41 = load i32, ptr %6, align 4, !tbaa !5
  %42 = call i64 (i64, ...) @__syscall(i64 noundef 33, i32 noundef %40, i32 noundef %41)
  %43 = trunc i64 %42 to i32
  store i32 %43, ptr %8, align 4, !tbaa !5
  %44 = icmp eq i32 %43, -16
  br i1 %44, label %45, label %46

45:                                               ; preds = %39
  br label %39, !llvm.loop !11

46:                                               ; preds = %39
  %47 = load i32, ptr %7, align 4, !tbaa !5
  %48 = and i32 %47, 524288
  %49 = icmp ne i32 %48, 0
  br i1 %49, label %50, label %53

50:                                               ; preds = %46
  %51 = load i32, ptr %6, align 4, !tbaa !5
  %52 = call i64 (i64, ...) @__syscall(i64 noundef 72, i32 noundef %51, i32 noundef 2, i32 noundef 1)
  br label %53

53:                                               ; preds = %50, %46
  %54 = load i32, ptr %8, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = call i64 @__syscall_ret(i64 noundef %55)
  %57 = trunc i64 %56 to i32
  store i32 %57, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %58

58:                                               ; preds = %53, %32, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %59 = load i32, ptr %4, align 4
  ret i32 %59
}

; 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_ret(i64 noundef) #2

declare hidden i64 @__syscall(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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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

; Function Attrs: nounwind uwtable
define dso_local i32 @dup3(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq i32 %0, %1
  br i1 %4, label %19, label %5

5:                                                ; preds = %3
  %6 = and i32 %2, 524288
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %.loopexit.preheader, label %.preheader

.loopexit.preheader:                              ; preds = %.preheader, %5
  br label %.loopexit

.preheader:                                       ; preds = %5, %.preheader
  %7 = tail call i64 (i64, ...) @__syscall(i64 noundef 292, i32 noundef %0, i32 noundef %1, i32 noundef %2) #2
  %8 = trunc i64 %7 to i32
  switch i32 %8, label %9 [
    i32 -16, label %.preheader
    i32 -38, label %.loopexit.preheader
  ]

9:                                                ; preds = %.preheader
  %sext19 = shl i64 %7, 32
  %10 = ashr exact i64 %sext19, 32
  br label %19

.loopexit:                                        ; preds = %.loopexit.preheader, %.loopexit
  %11 = tail call i64 (i64, ...) @__syscall(i64 noundef 33, i32 noundef %0, i32 noundef %1) #2
  %12 = and i64 %11, 4294967295
  %13 = icmp eq i64 %12, 4294967280
  br i1 %13, label %.loopexit, label %14, !llvm.loop !5

14:                                               ; preds = %.loopexit
  br i1 %.not, label %17, label %15

15:                                               ; preds = %14
  %16 = tail call i64 (i64, ...) @__syscall(i64 noundef 72, i32 noundef %1, i32 noundef 2, i32 noundef 1) #2
  br label %17

17:                                               ; preds = %15, %14
  %sext = shl i64 %11, 32
  %18 = ashr exact i64 %sext, 32
  br label %19

19:                                               ; preds = %3, %17, %9
  %.sink = phi i64 [ %18, %17 ], [ %10, %9 ], [ -22, %3 ]
  %20 = tail call i64 @__syscall_ret(i64 noundef %.sink) #2
  %.0 = trunc i64 %20 to i32
  ret i32 %.0
}

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

declare hidden i64 @__syscall(i64 noundef, ...) local_unnamed_addr #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 = { "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 }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass