; LLVM IR from gem5/gem5 :: ext/softfloat (f16_to_i16)

; 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.float16_t = type { i16 }

@softfloat_exceptionFlags = external global i8, align 1

; Function Attrs: nounwind uwtable
define dso_local i64 @f16_to_i16(i16 %0, i8 noundef zeroext %1, i1 noundef zeroext %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca %struct.float16_t, align 2
  %6 = alloca i8, align 1
  %7 = alloca i8, align 1
  %8 = alloca i8, align 1
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = getelementptr inbounds %struct.float16_t, ptr %5, i32 0, i32 0
  store i16 %0, ptr %11, align 2
  store i8 %1, ptr %6, align 1, !tbaa !5
  %12 = zext i1 %2 to i8
  store i8 %12, ptr %7, align 1, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #3
  %13 = load i8, ptr @softfloat_exceptionFlags, align 1, !tbaa !5
  store i8 %13, ptr %8, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %14 = load i8, ptr %6, align 1, !tbaa !5
  %15 = load i8, ptr %7, align 1, !tbaa !8, !range !10, !noundef !11
  %16 = trunc i8 %15 to i1
  %17 = getelementptr inbounds %struct.float16_t, ptr %5, i32 0, i32 0
  %18 = load i16, ptr %17, align 2
  %19 = call i64 @f16_to_i32(i16 %18, i8 noundef zeroext %14, i1 noundef zeroext %16)
  store i64 %19, ptr %9, align 8, !tbaa !12
  %20 = load i64, ptr %9, align 8, !tbaa !12
  %21 = icmp sgt i64 %20, 32767
  br i1 %21, label %22, label %27

22:                                               ; preds = %3
  %23 = load i8, ptr %8, align 1, !tbaa !5
  %24 = zext i8 %23 to i32
  %25 = or i32 %24, 16
  %26 = trunc i32 %25 to i8
  store i8 %26, ptr @softfloat_exceptionFlags, align 1, !tbaa !5
  store i64 32767, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %37

27:                                               ; preds = %3
  %28 = load i64, ptr %9, align 8, !tbaa !12
  %29 = icmp slt i64 %28, -32768
  br i1 %29, label %30, label %35

30:                                               ; preds = %27
  %31 = load i8, ptr %8, align 1, !tbaa !5
  %32 = zext i8 %31 to i32
  %33 = or i32 %32, 16
  %34 = trunc i32 %33 to i8
  store i8 %34, ptr @softfloat_exceptionFlags, align 1, !tbaa !5
  store i64 -32768, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %37

35:                                               ; preds = %27
  %36 = load i64, ptr %9, align 8, !tbaa !12
  store i64 %36, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %37

37:                                               ; preds = %35, %30, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #3
  %38 = load i64, ptr %4, align 8
  ret i64 %38
}

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

declare i64 @f16_to_i32(i16, i8 noundef zeroext, i1 noundef zeroext) #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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"_Bool", !6, i64 0}
!10 = !{i8 0, i8 2}
!11 = !{}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !6, 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"

@softfloat_exceptionFlags = external local_unnamed_addr global i8, align 1

; Function Attrs: nounwind uwtable
define dso_local range(i64 -32768, 32768) i64 @f16_to_i16(i16 %0, i8 noundef zeroext %1, i1 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = load i8, ptr @softfloat_exceptionFlags, align 1, !tbaa !5
  %5 = tail call i64 @f16_to_i32(i16 %0, i8 noundef zeroext %1, i1 noundef zeroext %2) #2
  %6 = icmp sgt i64 %5, 32767
  br i1 %6, label %.sink.split, label %7

7:                                                ; preds = %3
  %8 = icmp slt i64 %5, -32768
  br i1 %8, label %.sink.split, label %10

.sink.split:                                      ; preds = %7, %3
  %.0.ph = phi i64 [ 32767, %3 ], [ -32768, %7 ]
  %9 = or i8 %4, 16
  store i8 %9, ptr @softfloat_exceptionFlags, align 1, !tbaa !5
  br label %10

10:                                               ; preds = %.sink.split, %7
  %.0 = phi i64 [ %5, %7 ], [ %.0.ph, %.sink.split ]
  ret i64 %.0
}

declare i64 @f16_to_i32(i16, i8 noundef zeroext, i1 noundef zeroext) 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 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}


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