; LLVM IR from TobiasKozel/VAE :: external/portaudio/qa/loopback/src (write_wav)

; 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.WAV_Writer_s = type { ptr, i32, i32 }

@.str = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i64 @Audio_WAV_OpenWriter(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca [44 x i8], align 16
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 44, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %15 = getelementptr inbounds [44 x i8], ptr %11, i64 0, i64 0
  store ptr %15, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.WAV_Writer_s, ptr %16, i32 0, i32 2
  store i32 0, ptr %17, align 4, !tbaa !11
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.WAV_Writer_s, ptr %18, i32 0, i32 1
  store i32 0, ptr %19, align 8, !tbaa !13
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = call noalias ptr @fopen(ptr noundef %20, ptr noundef @.str)
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.WAV_Writer_s, ptr %22, i32 0, i32 0
  store ptr %21, ptr %23, align 8, !tbaa !14
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.WAV_Writer_s, ptr %24, i32 0, i32 0
  %26 = load ptr, ptr %25, align 8, !tbaa !14
  %27 = icmp eq ptr %26, null
  br i1 %27, label %28, label %29

28:                                               ; preds = %4
  store i64 -1, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %67

29:                                               ; preds = %4
  call void @WriteChunkType(ptr noundef %12, i64 noundef 1380533830)
  call void @WriteLongLE(ptr noundef %12, i64 noundef 0)
  call void @WriteChunkType(ptr noundef %12, i64 noundef 1463899717)
  call void @WriteChunkType(ptr noundef %12, i64 noundef 1718449184)
  call void @WriteLongLE(ptr noundef %12, i64 noundef 16)
  call void @WriteShortLE(ptr noundef %12, i16 noundef zeroext 1)
  %30 = load i32, ptr %8, align 4, !tbaa !9
  %31 = load i32, ptr %9, align 4, !tbaa !9
  %32 = mul nsw i32 %30, %31
  %33 = sext i32 %32 to i64
  %34 = mul i64 %33, 2
  %35 = trunc i64 %34 to i32
  store i32 %35, ptr %10, align 4, !tbaa !9
  %36 = load i32, ptr %9, align 4, !tbaa !9
  %37 = trunc i32 %36 to i16
  call void @WriteShortLE(ptr noundef %12, i16 noundef zeroext %37)
  %38 = load i32, ptr %8, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  call void @WriteLongLE(ptr noundef %12, i64 noundef %39)
  %40 = load i32, ptr %10, align 4, !tbaa !9
  %41 = zext i32 %40 to i64
  call void @WriteLongLE(ptr noundef %12, i64 noundef %41)
  %42 = load i32, ptr %9, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = mul i64 %43, 2
  %45 = trunc i64 %44 to i16
  call void @WriteShortLE(ptr noundef %12, i16 noundef zeroext %45)
  call void @WriteShortLE(ptr noundef %12, i16 noundef zeroext 16)
  call void @WriteChunkType(ptr noundef %12, i64 noundef 1684108385)
  %46 = load ptr, ptr %12, align 8, !tbaa !5
  %47 = getelementptr inbounds [44 x i8], ptr %11, i64 0, i64 0
  %48 = ptrtoint ptr %46 to i64
  %49 = ptrtoint ptr %47 to i64
  %50 = sub i64 %48, %49
  %51 = trunc i64 %50 to i32
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.WAV_Writer_s, ptr %52, i32 0, i32 1
  store i32 %51, ptr %53, align 8, !tbaa !13
  call void @WriteLongLE(ptr noundef %12, i64 noundef 0)
  %54 = getelementptr inbounds [44 x i8], ptr %11, i64 0, i64 0
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.WAV_Writer_s, ptr %55, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !14
  %58 = call i64 @fwrite(ptr noundef %54, i64 noundef 1, i64 noundef 44, ptr noundef %57)
  %59 = trunc i64 %58 to i32
  store i32 %59, ptr %13, align 4, !tbaa !9
  %60 = load i32, ptr %13, align 4, !tbaa !9
  %61 = sext i32 %60 to i64
  %62 = icmp ne i64 %61, 44
  br i1 %62, label %63, label %64

63:                                               ; preds = %29
  store i64 -1, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %67

64:                                               ; preds = %29
  %65 = load i32, ptr %13, align 4, !tbaa !9
  %66 = sext i32 %65 to i64
  store i64 %66, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %67

67:                                               ; preds = %64, %63, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 44, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %68 = load i64, ptr %5, align 8
  ret i64 %68
}

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

declare noalias ptr @fopen(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @WriteChunkType(ptr noundef, i64 noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @WriteLongLE(ptr noundef, i64 noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @WriteShortLE(ptr noundef, i16 noundef zeroext) #0

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, ptr 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 12}
!12 = !{!"WAV_Writer_s", !6, i64 0, !10, i64 8, !10, i64 12}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !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"

@.str = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i64 -1, 45) i64 @Audio_WAV_OpenWriter(ptr nocapture noundef %0, ptr nocapture noundef readonly %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = alloca [44 x i8], align 16
  %6 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 44, ptr nonnull %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #3
  store ptr %5, ptr %6, align 8, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 12
  store i32 0, ptr %7, align 4, !tbaa !9
  %8 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 0, ptr %8, align 8, !tbaa !12
  %9 = call noalias ptr @fopen(ptr noundef %1, ptr noundef nonnull @.str)
  store ptr %9, ptr %0, align 8, !tbaa !13
  %10 = icmp eq ptr %9, null
  br i1 %10, label %25, label %11

11:                                               ; preds = %4
  call void @WriteChunkType(ptr noundef nonnull %6, i64 noundef 1380533830)
  call void @WriteLongLE(ptr noundef nonnull %6, i64 noundef 0)
  call void @WriteChunkType(ptr noundef nonnull %6, i64 noundef 1463899717)
  call void @WriteChunkType(ptr noundef nonnull %6, i64 noundef 1718449184)
  call void @WriteLongLE(ptr noundef nonnull %6, i64 noundef 16)
  call void @WriteShortLE(ptr noundef nonnull %6, i16 noundef zeroext 1)
  %12 = shl i32 %2, 1
  %13 = mul i32 %12, %3
  %14 = trunc i32 %3 to i16
  call void @WriteShortLE(ptr noundef nonnull %6, i16 noundef zeroext %14)
  %15 = sext i32 %2 to i64
  call void @WriteLongLE(ptr noundef nonnull %6, i64 noundef %15)
  %16 = zext i32 %13 to i64
  call void @WriteLongLE(ptr noundef nonnull %6, i64 noundef %16)
  %17 = shl i16 %14, 1
  call void @WriteShortLE(ptr noundef nonnull %6, i16 noundef zeroext %17)
  call void @WriteShortLE(ptr noundef nonnull %6, i16 noundef zeroext 16)
  call void @WriteChunkType(ptr noundef nonnull %6, i64 noundef 1684108385)
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = ptrtoint ptr %18 to i64
  %20 = ptrtoint ptr %5 to i64
  %21 = sub i64 %19, %20
  %22 = trunc i64 %21 to i32
  store i32 %22, ptr %8, align 8, !tbaa !12
  call void @WriteLongLE(ptr noundef nonnull %6, i64 noundef 0)
  %23 = load ptr, ptr %0, align 8, !tbaa !13
  %24 = call i64 @fwrite(ptr noundef nonnull %5, i64 noundef 1, i64 noundef 44, ptr noundef %23)
  %sext.mask = and i64 %24, 4294967295
  %.not = icmp eq i64 %sext.mask, 44
  %. = select i1 %.not, i64 44, i64 -1
  br label %25

25:                                               ; preds = %11, %4
  %.0 = phi i64 [ -1, %4 ], [ %., %11 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #3
  call void @llvm.lifetime.end.p0(i64 44, ptr nonnull %5) #3
  ret i64 %.0
}

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

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden void @WriteChunkType(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @WriteLongLE(ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @WriteShortLE(ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nofree nounwind
declare noundef i64 @fwrite(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture 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 = { 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 12}
!10 = !{!"WAV_Writer_s", !6, i64 0, !11, i64 8, !11, i64 12}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!10, !6, i64 0}


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