; LLVM IR from danginsburg/opengles3-book :: Common/Source (esUtil)

; 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.TGA_HEADER = type <{ i8, i8, i8, i16, i16, i8, i16, i16, i16, i16, i8, i8 }>

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

; 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

; Function Attrs: nounwind uwtable
declare dso_local void @esLogMessage(ptr noundef, ...) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @esLoadTGA(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca %struct.TGA_HEADER, align 1
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 18, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = call ptr @esFileOpen(ptr noundef %16, ptr noundef %17)
  store ptr %18, ptr %11, align 8, !tbaa !5
  %19 = load ptr, ptr %11, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %23

21:                                               ; preds = %4
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  call void (ptr, ...) @esLogMessage(ptr noundef @.str.2, ptr noundef %22)
  store ptr null, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %80

23:                                               ; preds = %4
  %24 = load ptr, ptr %11, align 8, !tbaa !5
  %25 = call i32 @esFileRead(ptr noundef %24, i32 noundef 18, ptr noundef %12)
  store i32 %25, ptr %13, align 4, !tbaa !9
  %26 = getelementptr inbounds %struct.TGA_HEADER, ptr %12, i32 0, i32 8
  %27 = load i16, ptr %26, align 1, !tbaa !11
  %28 = zext i16 %27 to i32
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  store i32 %28, ptr %29, align 4, !tbaa !9
  %30 = getelementptr inbounds %struct.TGA_HEADER, ptr %12, i32 0, i32 9
  %31 = load i16, ptr %30, align 1, !tbaa !14
  %32 = zext i16 %31 to i32
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 %32, ptr %33, align 4, !tbaa !9
  %34 = getelementptr inbounds %struct.TGA_HEADER, ptr %12, i32 0, i32 10
  %35 = load i8, ptr %34, align 1, !tbaa !15
  %36 = zext i8 %35 to i32
  %37 = icmp eq i32 %36, 8
  br i1 %37, label %48, label %38

38:                                               ; preds = %23
  %39 = getelementptr inbounds %struct.TGA_HEADER, ptr %12, i32 0, i32 10
  %40 = load i8, ptr %39, align 1, !tbaa !15
  %41 = zext i8 %40 to i32
  %42 = icmp eq i32 %41, 24
  br i1 %42, label %48, label %43

43:                                               ; preds = %38
  %44 = getelementptr inbounds %struct.TGA_HEADER, ptr %12, i32 0, i32 10
  %45 = load i8, ptr %44, align 1, !tbaa !15
  %46 = zext i8 %45 to i32
  %47 = icmp eq i32 %46, 32
  br i1 %47, label %48, label %79

48:                                               ; preds = %43, %38, %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  %49 = load ptr, ptr %8, align 8, !tbaa !5
  %50 = load i32, ptr %49, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = mul i64 1, %51
  %53 = load ptr, ptr %9, align 8, !tbaa !5
  %54 = load i32, ptr %53, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = mul i64 %52, %55
  %57 = getelementptr inbounds %struct.TGA_HEADER, ptr %12, i32 0, i32 10
  %58 = load i8, ptr %57, align 1, !tbaa !15
  %59 = zext i8 %58 to i64
  %60 = mul i64 %56, %59
  %61 = udiv i64 %60, 8
  %62 = trunc i64 %61 to i32
  store i32 %62, ptr %15, align 4, !tbaa !9
  %63 = load i32, ptr %15, align 4, !tbaa !9
  %64 = sext i32 %63 to i64
  %65 = call noalias ptr @malloc(i64 noundef %64) #4
  store ptr %65, ptr %10, align 8, !tbaa !5
  %66 = load ptr, ptr %10, align 8, !tbaa !5
  %67 = icmp ne ptr %66, null
  br i1 %67, label %68, label %75

68:                                               ; preds = %48
  %69 = load ptr, ptr %11, align 8, !tbaa !5
  %70 = load i32, ptr %15, align 4, !tbaa !9
  %71 = load ptr, ptr %10, align 8, !tbaa !5
  %72 = call i32 @esFileRead(ptr noundef %69, i32 noundef %70, ptr noundef %71)
  store i32 %72, ptr %13, align 4, !tbaa !9
  %73 = load ptr, ptr %11, align 8, !tbaa !5
  call void @esFileClose(ptr noundef %73)
  %74 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %74, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %76

75:                                               ; preds = %48
  store i32 0, ptr %14, align 4
  br label %76

76:                                               ; preds = %75, %68
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  %77 = load i32, ptr %14, align 4
  switch i32 %77, label %80 [
    i32 0, label %78
  ]

78:                                               ; preds = %76
  br label %79

79:                                               ; preds = %78, %43
  store ptr null, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %80

80:                                               ; preds = %79, %76, %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 18, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %81 = load ptr, ptr %5, align 8
  ret ptr %81
}

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

; Function Attrs: nounwind uwtable
declare hidden i32 @esFileRead(ptr noundef, i32 noundef, ptr noundef) #1

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

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

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 = { nounwind allocsize(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 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

!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, !13, i64 12}
!12 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !13, i64 3, !13, i64 5, !7, i64 7, !13, i64 8, !13, i64 10, !13, i64 12, !13, i64 14, !7, i64 16, !7, i64 17}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !13, i64 14}
!15 = !{!12, !7, i64 16}


; 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.TGA_HEADER = type <{ i8, i8, i8, i16, i16, i8, i16, i16, i16, i16, i8, i8 }>

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

; 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

; Function Attrs: nounwind uwtable
declare dso_local void @esLogMessage(ptr noundef, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @esLoadTGA(ptr noundef %0, ptr noundef %1, ptr nocapture noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #1 {
  %5 = alloca %struct.TGA_HEADER, align 1
  call void @llvm.lifetime.start.p0(i64 18, ptr nonnull %5) #3
  %6 = tail call ptr @esFileOpen(ptr noundef %0, ptr noundef %1)
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %9

8:                                                ; preds = %4
  tail call void (ptr, ...) @esLogMessage(ptr noundef nonnull @.str.2, ptr noundef %1)
  br label %.critedge

9:                                                ; preds = %4
  %10 = call i32 @esFileRead(ptr noundef nonnull %6, i32 noundef 18, ptr noundef nonnull %5)
  %11 = getelementptr inbounds i8, ptr %5, i64 12
  %12 = load i16, ptr %11, align 1, !tbaa !5
  %13 = zext i16 %12 to i32
  store i32 %13, ptr %2, align 4, !tbaa !10
  %14 = getelementptr inbounds i8, ptr %5, i64 14
  %15 = load i16, ptr %14, align 1, !tbaa !12
  %16 = zext i16 %15 to i32
  store i32 %16, ptr %3, align 4, !tbaa !10
  %17 = getelementptr inbounds i8, ptr %5, i64 16
  %18 = load i8, ptr %17, align 1
  switch i8 %18, label %.critedge [
    i8 32, label %19
    i8 24, label %19
    i8 8, label %19
  ]

19:                                               ; preds = %9, %9, %9
  %20 = load i32, ptr %2, align 4, !tbaa !10
  %21 = sext i32 %20 to i64
  %22 = zext i16 %15 to i64
  %23 = zext nneg i8 %18 to i64
  %24 = mul nuw nsw i64 %23, %22
  %25 = mul nsw i64 %24, %21
  %sext = shl i64 %25, 29
  %26 = ashr exact i64 %sext, 32
  %27 = call noalias ptr @malloc(i64 noundef %26) #4
  %.not = icmp eq ptr %27, null
  br i1 %.not, label %.critedge, label %28

28:                                               ; preds = %19
  %29 = lshr exact i64 %25, 3
  %30 = trunc i64 %29 to i32
  %31 = call i32 @esFileRead(ptr noundef nonnull %6, i32 noundef %30, ptr noundef nonnull %27)
  call void @esFileClose(ptr noundef nonnull %6)
  br label %.critedge

.critedge:                                        ; preds = %9, %19, %28, %8
  %.1 = phi ptr [ null, %8 ], [ %27, %28 ], [ null, %19 ], [ null, %9 ]
  call void @llvm.lifetime.end.p0(i64 18, ptr nonnull %5) #3
  ret ptr %.1
}

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

; Function Attrs: nounwind uwtable
declare hidden i32 @esFileRead(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #2

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

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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 }
attributes #4 = { nounwind allocsize(0) }

!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, !9, i64 12}
!6 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2, !9, i64 3, !9, i64 5, !7, i64 7, !9, i64 8, !9, i64 10, !9, i64 12, !9, i64 14, !7, i64 16, !7, i64 17}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"short", !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!6, !9, i64 14}


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