; LLVM IR from steps3d/graphics-book :: Code/Libs/SOIL (SOIL)

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

@result_string_pointer = external dso_local global ptr, align 8
@.str.24 = external hidden unnamed_addr constant [24 x i8], align 1
@.str.25 = external hidden unnamed_addr constant [12 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
define dso_local i32 @SOIL_save_image(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef %5) #1 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store i32 %1, ptr %9, align 4, !tbaa !9
  store i32 %2, ptr %10, align 4, !tbaa !9
  store i32 %3, ptr %11, align 4, !tbaa !9
  store i32 %4, ptr %12, align 4, !tbaa !9
  store ptr %5, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %16 = load i32, ptr %10, align 4, !tbaa !9
  %17 = icmp slt i32 %16, 1
  br i1 %17, label %33, label %18

18:                                               ; preds = %6
  %19 = load i32, ptr %11, align 4, !tbaa !9
  %20 = icmp slt i32 %19, 1
  br i1 %20, label %33, label %21

21:                                               ; preds = %18
  %22 = load i32, ptr %12, align 4, !tbaa !9
  %23 = icmp slt i32 %22, 1
  br i1 %23, label %33, label %24

24:                                               ; preds = %21
  %25 = load i32, ptr %12, align 4, !tbaa !9
  %26 = icmp sgt i32 %25, 4
  br i1 %26, label %33, label %27

27:                                               ; preds = %24
  %28 = load ptr, ptr %13, align 8, !tbaa !5
  %29 = icmp eq ptr %28, null
  br i1 %29, label %33, label %30

30:                                               ; preds = %27
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = icmp eq ptr %31, null
  br i1 %32, label %33, label %34

33:                                               ; preds = %30, %27, %24, %21, %18, %6
  store i32 0, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %74

34:                                               ; preds = %30
  %35 = load i32, ptr %9, align 4, !tbaa !9
  %36 = icmp eq i32 %35, 1
  br i1 %36, label %37, label %44

37:                                               ; preds = %34
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = load i32, ptr %10, align 4, !tbaa !9
  %40 = load i32, ptr %11, align 4, !tbaa !9
  %41 = load i32, ptr %12, align 4, !tbaa !9
  %42 = load ptr, ptr %13, align 8, !tbaa !5
  %43 = call i32 @stbi_write_bmp(ptr noundef %38, i32 noundef %39, i32 noundef %40, i32 noundef %41, ptr noundef %42)
  store i32 %43, ptr %14, align 4, !tbaa !9
  br label %67

44:                                               ; preds = %34
  %45 = load i32, ptr %9, align 4, !tbaa !9
  %46 = icmp eq i32 %45, 0
  br i1 %46, label %47, label %54

47:                                               ; preds = %44
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = load i32, ptr %10, align 4, !tbaa !9
  %50 = load i32, ptr %11, align 4, !tbaa !9
  %51 = load i32, ptr %12, align 4, !tbaa !9
  %52 = load ptr, ptr %13, align 8, !tbaa !5
  %53 = call i32 @stbi_write_tga(ptr noundef %48, i32 noundef %49, i32 noundef %50, i32 noundef %51, ptr noundef %52)
  store i32 %53, ptr %14, align 4, !tbaa !9
  br label %66

54:                                               ; preds = %44
  %55 = load i32, ptr %9, align 4, !tbaa !9
  %56 = icmp eq i32 %55, 2
  br i1 %56, label %57, label %64

57:                                               ; preds = %54
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = load i32, ptr %10, align 4, !tbaa !9
  %60 = load i32, ptr %11, align 4, !tbaa !9
  %61 = load i32, ptr %12, align 4, !tbaa !9
  %62 = load ptr, ptr %13, align 8, !tbaa !5
  %63 = call i32 @save_image_as_DDS(ptr noundef %58, i32 noundef %59, i32 noundef %60, i32 noundef %61, ptr noundef %62)
  store i32 %63, ptr %14, align 4, !tbaa !9
  br label %65

64:                                               ; preds = %54
  store i32 0, ptr %14, align 4, !tbaa !9
  br label %65

65:                                               ; preds = %64, %57
  br label %66

66:                                               ; preds = %65, %47
  br label %67

67:                                               ; preds = %66, %37
  %68 = load i32, ptr %14, align 4, !tbaa !9
  %69 = icmp eq i32 %68, 0
  br i1 %69, label %70, label %71

70:                                               ; preds = %67
  store ptr @.str.24, ptr @result_string_pointer, align 8, !tbaa !5
  br label %72

71:                                               ; preds = %67
  store ptr @.str.25, ptr @result_string_pointer, align 8, !tbaa !5
  br label %72

72:                                               ; preds = %71, %70
  %73 = load i32, ptr %14, align 4, !tbaa !9
  store i32 %73, ptr %7, align 4
  store i32 1, ptr %15, align 4
  br label %74

74:                                               ; preds = %72, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  %75 = load i32, ptr %7, align 4
  ret i32 %75
}

declare i32 @stbi_write_bmp(ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef) #2

declare i32 @stbi_write_tga(ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef) #2

declare i32 @save_image_as_DDS(ptr noundef, i32 noundef, i32 noundef, i32 noundef, 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, !10, i64 0}
!10 = !{!"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"

@result_string_pointer = external dso_local local_unnamed_addr global ptr, align 8
@.str.24 = external hidden unnamed_addr constant [24 x i8], align 1
@.str.25 = external hidden unnamed_addr constant [12 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @SOIL_save_image(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef %5) local_unnamed_addr #0 {
  %7 = icmp slt i32 %2, 1
  %8 = icmp slt i32 %3, 1
  %or.cond = or i1 %7, %8
  %9 = add i32 %4, -5
  %10 = icmp ult i32 %9, -4
  %or.cond5 = or i1 %or.cond, %10
  %11 = icmp eq ptr %5, null
  %or.cond7 = or i1 %11, %or.cond5
  %12 = icmp eq ptr %0, null
  %or.cond9 = or i1 %12, %or.cond7
  br i1 %or.cond9, label %23, label %13

13:                                               ; preds = %6
  switch i32 %1, label %.thread [
    i32 1, label %14
    i32 0, label %16
    i32 2, label %18
  ]

14:                                               ; preds = %13
  %15 = tail call i32 @stbi_write_bmp(ptr noundef nonnull %0, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef nonnull %5) #2
  br label %20

16:                                               ; preds = %13
  %17 = tail call i32 @stbi_write_tga(ptr noundef nonnull %0, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef nonnull %5) #2
  br label %20

18:                                               ; preds = %13
  %19 = tail call i32 @save_image_as_DDS(ptr noundef nonnull %0, i32 noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef nonnull %5) #2
  br label %20

20:                                               ; preds = %16, %18, %14
  %.0 = phi i32 [ %15, %14 ], [ %17, %16 ], [ %19, %18 ]
  %.0.fr = freeze i32 %.0
  %21 = icmp eq i32 %.0.fr, 0
  %spec.select = select i1 %21, ptr @.str.24, ptr @.str.25
  br label %.thread

.thread:                                          ; preds = %20, %13
  %.042 = phi i32 [ 0, %13 ], [ %.0.fr, %20 ]
  %22 = phi ptr [ @.str.24, %13 ], [ %spec.select, %20 ]
  store ptr %22, ptr @result_string_pointer, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %6, %.thread
  %.035 = phi i32 [ %.042, %.thread ], [ 0, %6 ]
  ret i32 %.035
}

declare i32 @stbi_write_bmp(ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare i32 @stbi_write_tga(ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare i32 @save_image_as_DDS(ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #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 = { "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 }

!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"}


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