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

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

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

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) #0

; Function Attrs: nounwind uwtable
define dso_local i64 @Audio_WAV_CloseWriter(ptr noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca [4 x i8], align 1
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.WAV_Writer_s, ptr %10, i32 0, i32 0
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.WAV_Writer_s, ptr %13, i32 0, i32 1
  %15 = load i32, ptr %14, align 8, !tbaa !12
  %16 = sext i32 %15 to i64
  %17 = call i32 @fseek(ptr noundef %12, i64 noundef %16, i32 noundef 0)
  store i32 %17, ptr %8, align 4, !tbaa !13
  %18 = load i32, ptr %8, align 4, !tbaa !13
  %19 = icmp slt i32 %18, 0
  br i1 %19, label %20, label %23

20:                                               ; preds = %1
  %21 = load i32, ptr %8, align 4, !tbaa !13
  %22 = sext i32 %21 to i64
  store i64 %22, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %78

23:                                               ; preds = %1
  %24 = getelementptr inbounds [4 x i8], ptr %4, i64 0, i64 0
  store ptr %24, ptr %5, align 8, !tbaa !5
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.WAV_Writer_s, ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 4, !tbaa !14
  %28 = sext i32 %27 to i64
  call void @WriteLongLE(ptr noundef %5, i64 noundef %28)
  %29 = getelementptr inbounds [4 x i8], ptr %4, i64 0, i64 0
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.WAV_Writer_s, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !9
  %33 = call i64 @fwrite(ptr noundef %29, i64 noundef 1, i64 noundef 4, ptr noundef %32)
  %34 = trunc i64 %33 to i32
  store i32 %34, ptr %6, align 4, !tbaa !13
  %35 = load i32, ptr %6, align 4, !tbaa !13
  %36 = sext i32 %35 to i64
  %37 = icmp ne i64 %36, 4
  br i1 %37, label %38, label %39

38:                                               ; preds = %23
  store i64 -1, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %78

39:                                               ; preds = %23
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.WAV_Writer_s, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %41, align 8, !tbaa !9
  %43 = call i32 @fseek(ptr noundef %42, i64 noundef 4, i32 noundef 0)
  store i32 %43, ptr %8, align 4, !tbaa !13
  %44 = load i32, ptr %8, align 4, !tbaa !13
  %45 = icmp slt i32 %44, 0
  br i1 %45, label %46, label %49

46:                                               ; preds = %39
  %47 = load i32, ptr %8, align 4, !tbaa !13
  %48 = sext i32 %47 to i64
  store i64 %48, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %78

49:                                               ; preds = %39
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.WAV_Writer_s, ptr %50, i32 0, i32 2
  %52 = load i32, ptr %51, align 4, !tbaa !14
  %53 = add nsw i32 %52, 36
  store i32 %53, ptr %7, align 4, !tbaa !13
  %54 = getelementptr inbounds [4 x i8], ptr %4, i64 0, i64 0
  store ptr %54, ptr %5, align 8, !tbaa !5
  %55 = load i32, ptr %7, align 4, !tbaa !13
  %56 = sext i32 %55 to i64
  call void @WriteLongLE(ptr noundef %5, i64 noundef %56)
  %57 = getelementptr inbounds [4 x i8], ptr %4, i64 0, i64 0
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.WAV_Writer_s, ptr %58, i32 0, i32 0
  %60 = load ptr, ptr %59, align 8, !tbaa !9
  %61 = call i64 @fwrite(ptr noundef %57, i64 noundef 1, i64 noundef 4, ptr noundef %60)
  %62 = trunc i64 %61 to i32
  store i32 %62, ptr %6, align 4, !tbaa !13
  %63 = load i32, ptr %6, align 4, !tbaa !13
  %64 = sext i32 %63 to i64
  %65 = icmp ne i64 %64, 4
  br i1 %65, label %66, label %67

66:                                               ; preds = %49
  store i64 -1, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %78

67:                                               ; preds = %49
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.WAV_Writer_s, ptr %68, i32 0, i32 0
  %70 = load ptr, ptr %69, align 8, !tbaa !9
  %71 = call i32 @fclose(ptr noundef %70)
  %72 = load ptr, ptr %3, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.WAV_Writer_s, ptr %72, i32 0, i32 0
  store ptr null, ptr %73, align 8, !tbaa !9
  %74 = load ptr, ptr %3, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.WAV_Writer_s, ptr %74, i32 0, i32 2
  %76 = load i32, ptr %75, align 4, !tbaa !14
  %77 = sext i32 %76 to i64
  store i64 %77, ptr %2, align 8
  store i32 1, ptr %9, align 4
  br label %78

78:                                               ; preds = %67, %66, %46, %38, %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %79 = load i64, ptr %2, align 8
  ret i64 %79
}

declare i32 @fseek(ptr noundef, i64 noundef, i32 noundef) #2

declare i32 @fclose(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !6, i64 0}
!10 = !{!"WAV_Writer_s", !6, i64 0, !11, i64 8, !11, i64 12}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !11, i64 12}


; 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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

; 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) #0

; Function Attrs: nounwind uwtable
define dso_local range(i64 -2147483648, 2147483648) i64 @Audio_WAV_CloseWriter(ptr nocapture noundef %0) local_unnamed_addr #1 {
  %2 = alloca [4 x i8], align 1
  %3 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i32, ptr %5, align 8, !tbaa !11
  %7 = sext i32 %6 to i64
  %8 = tail call i32 @fseek(ptr noundef %4, i64 noundef %7, i32 noundef 0)
  %9 = icmp slt i32 %8, 0
  br i1 %9, label %10, label %12

10:                                               ; preds = %1
  %11 = sext i32 %8 to i64
  br label %35

12:                                               ; preds = %1
  store ptr %2, ptr %3, align 8, !tbaa !12
  %13 = getelementptr inbounds i8, ptr %0, i64 12
  %14 = load i32, ptr %13, align 4, !tbaa !13
  %15 = sext i32 %14 to i64
  call void @WriteLongLE(ptr noundef nonnull %3, i64 noundef %15)
  %16 = load ptr, ptr %0, align 8, !tbaa !5
  %17 = call i64 @fwrite(ptr noundef nonnull %2, i64 noundef 1, i64 noundef 4, ptr noundef %16)
  %sext.mask = and i64 %17, 4294967295
  %.not = icmp eq i64 %sext.mask, 4
  br i1 %.not, label %18, label %35

18:                                               ; preds = %12
  %19 = load ptr, ptr %0, align 8, !tbaa !5
  %20 = call i32 @fseek(ptr noundef %19, i64 noundef 4, i32 noundef 0)
  %21 = icmp slt i32 %20, 0
  br i1 %21, label %22, label %24

22:                                               ; preds = %18
  %23 = sext i32 %20 to i64
  br label %35

24:                                               ; preds = %18
  %25 = load i32, ptr %13, align 4, !tbaa !13
  %26 = add nsw i32 %25, 36
  store ptr %2, ptr %3, align 8, !tbaa !12
  %27 = sext i32 %26 to i64
  call void @WriteLongLE(ptr noundef nonnull %3, i64 noundef %27)
  %28 = load ptr, ptr %0, align 8, !tbaa !5
  %29 = call i64 @fwrite(ptr noundef nonnull %2, i64 noundef 1, i64 noundef 4, ptr noundef %28)
  %sext.mask20 = and i64 %29, 4294967295
  %.not19 = icmp eq i64 %sext.mask20, 4
  br i1 %.not19, label %30, label %35

30:                                               ; preds = %24
  %31 = load ptr, ptr %0, align 8, !tbaa !5
  %32 = call i32 @fclose(ptr noundef %31)
  store ptr null, ptr %0, align 8, !tbaa !5
  %33 = load i32, ptr %13, align 4, !tbaa !13
  %34 = sext i32 %33 to i64
  br label %35

35:                                               ; preds = %24, %12, %30, %22, %10
  %.0 = phi i64 [ %11, %10 ], [ %23, %22 ], [ %34, %30 ], [ -1, %12 ], [ -1, %24 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #3
  ret i64 %.0
}

; Function Attrs: nofree nounwind
declare noundef i32 @fseek(ptr nocapture noundef, i64 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !7, i64 0}
!6 = !{!"WAV_Writer_s", !7, i64 0, !10, i64 8, !10, i64 12}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!7, !7, i64 0}
!13 = !{!6, !10, i64 12}


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