; LLVM IR from steps3d/graphics-book :: Code/Core/TgaImage.ll (ZN8TgaImage11writeToFileEPKc)

; 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.TgaHeader = type <{ i8, i8, i8, i16, i16, i8, i16, i16, i16, i16, i8, i8 }>
%class.TgaImage = 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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN8TgaImage11writeToFileEPKc(ptr noundef nonnull align 8 dereferenceable(16) %0, ptr noundef %1) #2 align 2 {
  %3 = alloca i1, align 1
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca %struct.TgaHeader, align 1
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %10 = load ptr, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = call i32 (ptr, i32, ...) @open(ptr noundef %11, i32 noundef 66, i32 noundef 384)
  store i32 %12, ptr %6, align 4, !tbaa !9
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = icmp eq i32 %13, -1
  br i1 %14, label %15, label %16

15:                                               ; preds = %2
  store i1 false, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %43

16:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 18, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %17 = getelementptr inbounds %class.TgaImage, ptr %10, i32 0, i32 1
  %18 = load i32, ptr %17, align 8, !tbaa !11
  %19 = getelementptr inbounds %class.TgaImage, ptr %10, i32 0, i32 2
  %20 = load i32, ptr %19, align 4, !tbaa !13
  %21 = mul nsw i32 %18, %20
  %22 = mul nsw i32 %21, 3
  store i32 %22, ptr %9, align 4, !tbaa !9
  call void @llvm.memset.p0.i64(ptr align 1 %8, i8 0, i64 18, i1 false)
  %23 = getelementptr inbounds %struct.TgaHeader, ptr %8, i32 0, i32 2
  store i8 2, ptr %23, align 1, !tbaa !14
  %24 = getelementptr inbounds %class.TgaImage, ptr %10, i32 0, i32 1
  %25 = load i32, ptr %24, align 8, !tbaa !11
  %26 = trunc i32 %25 to i16
  %27 = getelementptr inbounds %struct.TgaHeader, ptr %8, i32 0, i32 8
  store i16 %26, ptr %27, align 1, !tbaa !17
  %28 = getelementptr inbounds %class.TgaImage, ptr %10, i32 0, i32 2
  %29 = load i32, ptr %28, align 4, !tbaa !13
  %30 = trunc i32 %29 to i16
  %31 = getelementptr inbounds %struct.TgaHeader, ptr %8, i32 0, i32 9
  store i16 %30, ptr %31, align 1, !tbaa !18
  %32 = getelementptr inbounds %struct.TgaHeader, ptr %8, i32 0, i32 10
  store i8 24, ptr %32, align 1, !tbaa !19
  %33 = load i32, ptr %6, align 4, !tbaa !9
  %34 = call i64 @write(i32 noundef %33, ptr noundef %8, i64 noundef 18)
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = getelementptr inbounds %class.TgaImage, ptr %10, i32 0, i32 0
  %37 = load ptr, ptr %36, align 8, !tbaa !20
  %38 = load i32, ptr %9, align 4, !tbaa !9
  %39 = sext i32 %38 to i64
  %40 = call i64 @write(i32 noundef %35, ptr noundef %37, i64 noundef %39)
  %41 = load i32, ptr %6, align 4, !tbaa !9
  %42 = call i32 @close(i32 noundef %41)
  store i1 true, ptr %3, align 1
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 18, ptr %8) #4
  br label %43

43:                                               ; preds = %16, %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  %44 = load i1, ptr %3, align 1
  ret i1 %44
}

declare i32 @open(ptr noundef, i32 noundef, ...) #3

declare i64 @write(i32 noundef, ptr noundef, i64 noundef) #3

declare i32 @close(i32 noundef) #3

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

!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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"_ZTS8TgaImage", !6, i64 0, !10, i64 8, !10, i64 12}
!13 = !{!12, !10, i64 12}
!14 = !{!15, !7, i64 2}
!15 = !{!"_ZTS9TgaHeader", !7, i64 0, !7, i64 1, !7, i64 2, !16, i64 3, !16, i64 5, !7, i64 7, !16, i64 8, !16, i64 10, !16, i64 12, !16, i64 14, !7, i64 16, !7, i64 17}
!16 = !{!"short", !7, i64 0}
!17 = !{!15, !16, i64 12}
!18 = !{!15, !16, i64 14}
!19 = !{!15, !7, i64 16}
!20 = !{!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"

%struct.TgaHeader = type <{ i8, i8, i8, i16, i16, i8, i16, i16, i16, i16, i8, i8 }>

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN8TgaImage11writeToFileEPKc(ptr nocapture noundef nonnull readonly align 8 dereferenceable(16) %0, ptr nocapture noundef readonly %1) local_unnamed_addr #2 align 2 {
  %3 = alloca %struct.TgaHeader, align 1
  %4 = tail call i32 (ptr, i32, ...) @open(ptr noundef %1, i32 noundef 66, i32 noundef 384)
  %5 = icmp ne i32 %4, -1
  br i1 %5, label %6, label %24

6:                                                ; preds = %2
  call void @llvm.lifetime.start.p0(i64 18, ptr nonnull %3) #5
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load i32, ptr %7, align 8, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %0, i64 12
  %10 = load i32, ptr %9, align 4, !tbaa !11
  %11 = mul i32 %8, 3
  %12 = mul i32 %11, %10
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(18) %3, i8 0, i64 18, i1 false)
  %13 = getelementptr inbounds i8, ptr %3, i64 2
  store i8 2, ptr %13, align 1, !tbaa !12
  %14 = trunc i32 %8 to i16
  %15 = getelementptr inbounds i8, ptr %3, i64 12
  store i16 %14, ptr %15, align 1, !tbaa !15
  %16 = trunc i32 %10 to i16
  %17 = getelementptr inbounds i8, ptr %3, i64 14
  store i16 %16, ptr %17, align 1, !tbaa !16
  %18 = getelementptr inbounds i8, ptr %3, i64 16
  store i8 24, ptr %18, align 1, !tbaa !17
  %19 = call i64 @write(i32 noundef %4, ptr noundef nonnull %3, i64 noundef 18)
  %20 = load ptr, ptr %0, align 8, !tbaa !18
  %21 = sext i32 %12 to i64
  %22 = tail call i64 @write(i32 noundef %4, ptr noundef %20, i64 noundef %21)
  %23 = tail call i32 @close(i32 noundef %4)
  call void @llvm.lifetime.end.p0(i64 18, ptr nonnull %3) #5
  br label %24

24:                                               ; preds = %2, %6
  ret i1 %5
}

; Function Attrs: nofree
declare noundef i32 @open(ptr nocapture noundef readonly, i32 noundef, ...) local_unnamed_addr #3

; Function Attrs: nofree
declare noundef i64 @write(i32 noundef, ptr nocapture noundef readonly, i64 noundef) local_unnamed_addr #3

declare i32 @close(i32 noundef) local_unnamed_addr #4

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 #3 = { nofree "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 = { "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 #5 = { 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, !10, i64 8}
!6 = !{!"_ZTS8TgaImage", !7, i64 0, !10, i64 8, !10, i64 12}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 12}
!12 = !{!13, !8, i64 2}
!13 = !{!"_ZTS9TgaHeader", !8, i64 0, !8, i64 1, !8, i64 2, !14, i64 3, !14, i64 5, !8, i64 7, !14, i64 8, !14, i64 10, !14, i64 12, !14, i64 14, !8, i64 16, !8, i64 17}
!14 = !{!"short", !8, i64 0}
!15 = !{!13, !14, i64 12}
!16 = !{!13, !14, i64 14}
!17 = !{!13, !8, i64 16}
!18 = !{!6, !7, i64 0}


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