; LLVM IR from kkumatani/distant_speech_recognition :: btk20_src/common/mach_ind_io.ll (Z11write_shortP8_IO_FILEs)

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

@_ZL7machine = external hidden global i32, align 4
@.str.3 = external hidden unnamed_addr constant [32 x i8], align 1
@_ZTI9jio_error = external dso_local constant { ptr, ptr, 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

declare ptr @__cxa_allocate_exception(i64)

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN9jio_errorC2EPKcz(ptr noundef nonnull align 8 dereferenceable(44), ptr noundef, ...) unnamed_addr #1 align 2

declare i32 @__gxx_personality_v0(...)

declare void @__cxa_free_exception(ptr)

; Function Attrs: nounwind
declare void @_ZN7j_errorD2Ev(ptr noundef nonnull align 8 dereferenceable(44)) unnamed_addr #2

declare void @__cxa_throw(ptr, ptr, ptr)

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #3

; Function Attrs: mustprogress uwtable
define dso_local void @_Z11write_shortP8_IO_FILEs(ptr noundef %0, i16 noundef signext %1) #1 personality ptr @__gxx_personality_v0 {
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i16 %1, ptr %4, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #4
  %8 = load i32, ptr @_ZL7machine, align 4, !tbaa !11
  switch i32 %8, label %30 [
    i32 1, label %9
    i32 2, label %11
    i32 3, label %11
  ]

9:                                                ; preds = %2
  %10 = load i16, ptr %4, align 2, !tbaa !9
  store i16 %10, ptr %5, align 2, !tbaa !9
  br label %38

11:                                               ; preds = %2, %2
  %12 = load i16, ptr %4, align 2, !tbaa !9
  %13 = sext i16 %12 to i32
  %14 = shl i32 %13, 8
  %15 = trunc i32 %14 to i16
  %16 = sext i16 %15 to i32
  %17 = and i32 65280, %16
  %18 = trunc i32 %17 to i16
  %19 = sext i16 %18 to i32
  %20 = load i16, ptr %4, align 2, !tbaa !9
  %21 = sext i16 %20 to i32
  %22 = ashr i32 %21, 8
  %23 = trunc i32 %22 to i16
  %24 = sext i16 %23 to i32
  %25 = and i32 255, %24
  %26 = trunc i32 %25 to i16
  %27 = sext i16 %26 to i32
  %28 = or i32 %19, %27
  %29 = trunc i32 %28 to i16
  store i16 %29, ptr %5, align 2, !tbaa !9
  br label %38

30:                                               ; preds = %2
  %31 = call ptr @__cxa_allocate_exception(i64 48) #4
  %32 = load i32, ptr @_ZL7machine, align 4, !tbaa !11
  invoke void (ptr, ptr, ...) @_ZN9jio_errorC2EPKcz(ptr noundef nonnull align 8 dereferenceable(44) %31, ptr noundef @.str.3, i32 noundef %32)
          to label %33 unwind label %34

33:                                               ; preds = %30
  call void @__cxa_throw(ptr %31, ptr @_ZTI9jio_error, ptr @_ZN7j_errorD2Ev) #5
  unreachable

34:                                               ; preds = %30
  %35 = landingpad { ptr, i32 }
          cleanup
  %36 = extractvalue { ptr, i32 } %35, 0
  store ptr %36, ptr %6, align 8
  %37 = extractvalue { ptr, i32 } %35, 1
  store i32 %37, ptr %7, align 4
  call void @__cxa_free_exception(ptr %31) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #4
  br label %41

38:                                               ; preds = %11, %9
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = call i64 @fwrite(ptr noundef %5, i64 noundef 2, i64 noundef 1, ptr noundef %39)
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #4
  ret void

41:                                               ; preds = %34
  %42 = load ptr, ptr %6, align 8
  %43 = load i32, ptr %7, align 4
  %44 = insertvalue { ptr, i32 } poison, ptr %42, 0
  %45 = insertvalue { ptr, i32 } %44, i32 %43, 1
  resume { ptr, i32 } %45
}

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

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !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"

@_ZL7machine = external hidden local_unnamed_addr global i32, align 4
@.str.3 = external hidden unnamed_addr constant [32 x i8], align 1
@_ZTI9jio_error = external dso_local constant { ptr, ptr, ptr }, align 8

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

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

declare ptr @__cxa_allocate_exception(i64) local_unnamed_addr

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN9jio_errorC2EPKcz(ptr noundef nonnull align 8 dereferenceable(44), ptr noundef, ...) unnamed_addr #1 align 2

declare i32 @__gxx_personality_v0(...)

declare void @__cxa_free_exception(ptr) local_unnamed_addr

; Function Attrs: nounwind
declare void @_ZN7j_errorD2Ev(ptr noundef nonnull align 8 dereferenceable(44)) unnamed_addr #2

declare void @__cxa_throw(ptr, ptr, ptr) local_unnamed_addr

; Function Attrs: nofree nounwind
declare noundef i64 @fwrite(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #3

; Function Attrs: mustprogress uwtable
define dso_local void @_Z11write_shortP8_IO_FILEs(ptr nocapture noundef %0, i16 noundef signext %1) local_unnamed_addr #1 personality ptr @__gxx_personality_v0 {
  %3 = alloca i16, align 2
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %3) #5
  %4 = load i32, ptr @_ZL7machine, align 4, !tbaa !5
  switch i32 %4, label %7 [
    i32 1, label %13
    i32 2, label %5
    i32 3, label %5
  ]

5:                                                ; preds = %2, %2
  %6 = tail call i16 @llvm.bswap.i16(i16 %1)
  br label %13

7:                                                ; preds = %2
  %8 = tail call ptr @__cxa_allocate_exception(i64 48) #5
  %9 = load i32, ptr @_ZL7machine, align 4, !tbaa !5
  invoke void (ptr, ptr, ...) @_ZN9jio_errorC2EPKcz(ptr noundef nonnull align 8 dereferenceable(44) %8, ptr noundef nonnull @.str.3, i32 noundef %9)
          to label %10 unwind label %11

10:                                               ; preds = %7
  tail call void @__cxa_throw(ptr nonnull %8, ptr nonnull @_ZTI9jio_error, ptr nonnull @_ZN7j_errorD2Ev) #6
  unreachable

11:                                               ; preds = %7
  %12 = landingpad { ptr, i32 }
          cleanup
  tail call void @__cxa_free_exception(ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %3) #5
  resume { ptr, i32 } %12

13:                                               ; preds = %2, %5
  %storemerge = phi i16 [ %6, %5 ], [ %1, %2 ]
  store i16 %storemerge, ptr %3, align 2, !tbaa !9
  %14 = call i64 @fwrite(ptr noundef nonnull %3, i64 noundef 2, i64 noundef 1, ptr noundef %0)
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %3) #5
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.bswap.i16(i16) #4

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

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}


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