; LLVM IR from jingweizhanghuai/Morn :: src/image (morn_image)

; 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.HandleMornImage = type { ptr, ptr, ptr }
%struct.MImage = type { i32, i32, i32, [4 x ptr], ptr }

@.str = external hidden unnamed_addr constant [78 x i8], align 1
@.str.12 = external hidden unnamed_addr constant [19 x i8], align 1
@__FUNCTION__.mornImageBuff = external hidden unnamed_addr constant [14 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

declare void @m_Exception(i32 noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, ...) #1

; 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 ptr @ImageCreate(i32 noundef, i32 noundef, i32 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @ImageRedefine(ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @mornImageInit() #2

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

15:                                               ; preds = %3
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = add nsw i32 %16, 0
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = call ptr @ImageCreate(i32 noundef %17, i32 noundef %18, i32 noundef %19, ptr noundef null)
  %21 = load ptr, ptr %7, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.HandleMornImage, ptr %21, i32 0, i32 0
  store ptr %20, ptr %22, align 8, !tbaa !11
  br label %80

23:                                               ; preds = %3
  br label %24

24:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 3, ptr %8, align 4, !tbaa !5
  %25 = load i32, ptr %8, align 4, !tbaa !5
  %26 = icmp eq i32 %25, 2
  br i1 %26, label %27, label %39

27:                                               ; preds = %24
  %28 = load ptr, ptr %7, align 8, !tbaa !9
  %29 = getelementptr inbounds %struct.HandleMornImage, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !11
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = add nsw i32 %31, 0
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = load ptr, ptr %7, align 8, !tbaa !9
  %35 = getelementptr inbounds %struct.HandleMornImage, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !11
  %37 = getelementptr inbounds %struct.MImage, ptr %36, i32 0, i32 3
  %38 = getelementptr inbounds [4 x ptr], ptr %37, i64 0, i64 0
  call void @ImageRedefine(ptr noundef %30, i32 noundef -1, i32 noundef %32, i32 noundef %33, ptr noundef %38)
  br label %77

39:                                               ; preds = %24
  %40 = load i32, ptr %8, align 4, !tbaa !5
  %41 = icmp eq i32 %40, 3
  br i1 %41, label %42, label %55

42:                                               ; preds = %39
  %43 = load ptr, ptr %7, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.HandleMornImage, ptr %43, i32 0, i32 0
  %45 = load ptr, ptr %44, align 8, !tbaa !11
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = add nsw i32 %46, 0
  %48 = load i32, ptr %5, align 4, !tbaa !5
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = load ptr, ptr %7, align 8, !tbaa !9
  %51 = getelementptr inbounds %struct.HandleMornImage, ptr %50, i32 0, i32 0
  %52 = load ptr, ptr %51, align 8, !tbaa !11
  %53 = getelementptr inbounds %struct.MImage, ptr %52, i32 0, i32 3
  %54 = getelementptr inbounds [4 x ptr], ptr %53, i64 0, i64 0
  call void @ImageRedefine(ptr noundef %45, i32 noundef %47, i32 noundef %48, i32 noundef %49, ptr noundef %54)
  br label %76

55:                                               ; preds = %39
  %56 = load i32, ptr %8, align 4, !tbaa !5
  %57 = icmp eq i32 %56, 4
  br i1 %57, label %58, label %66

58:                                               ; preds = %55
  %59 = load ptr, ptr %7, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.HandleMornImage, ptr %59, i32 0, i32 0
  %61 = load ptr, ptr %60, align 8, !tbaa !11
  %62 = load i32, ptr %4, align 4, !tbaa !5
  %63 = add nsw i32 %62, 0
  %64 = load i32, ptr %5, align 4, !tbaa !5
  %65 = load i32, ptr %6, align 4, !tbaa !5
  call void @ImageRedefine(ptr noundef %61, i32 noundef %63, i32 noundef %64, i32 noundef %65, ptr noundef inttoptr (i64 -1 to ptr))
  br label %75

66:                                               ; preds = %55
  br label %67

67:                                               ; preds = %66
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 1, ptr %9, align 4, !tbaa !5
  %68 = load i32, ptr %9, align 4, !tbaa !5
  %69 = icmp ne i32 %68, 0
  br i1 %69, label %70, label %72

70:                                               ; preds = %67
  %71 = load i32, ptr %9, align 4, !tbaa !5
  call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef %71, i32 noundef -1, ptr noundef @.str, i32 noundef 878, ptr noundef @__FUNCTION__.mornImageBuff, ptr noundef @.str.12)
  br label %72

72:                                               ; preds = %70, %67
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %73

73:                                               ; preds = %72
  br label %74

74:                                               ; preds = %73
  br label %75

75:                                               ; preds = %74, %58
  br label %76

76:                                               ; preds = %75, %42
  br label %77

77:                                               ; preds = %76, %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %78

78:                                               ; preds = %77
  br label %79

79:                                               ; preds = %78
  br label %80

80:                                               ; preds = %79, %15
  %81 = load ptr, ptr %7, align 8, !tbaa !9
  %82 = getelementptr inbounds %struct.HandleMornImage, ptr %81, i32 0, i32 0
  %83 = load ptr, ptr %82, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret ptr %83
}

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @ImageCreate(i32 noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @ImageRedefine(ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @mornImageInit() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @mornImageBuff(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = tail call ptr @mornImageInit()
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %7, label %9

7:                                                ; preds = %3
  %8 = tail call ptr @ImageCreate(i32 noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef null)
  store ptr %8, ptr %4, align 8, !tbaa !5
  br label %11

9:                                                ; preds = %3
  %10 = getelementptr inbounds i8, ptr %5, i64 16
  tail call void @ImageRedefine(ptr noundef nonnull %5, i32 noundef %0, i32 noundef %1, i32 noundef %2, ptr noundef nonnull %10)
  %.pre = load ptr, ptr %4, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %9, %7
  %12 = phi ptr [ %.pre, %9 ], [ %8, %7 ]
  ret ptr %12
}

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

!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 = !{!"HandleMornImage", !7, i64 0, !7, i64 8, !7, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}


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