; LLVM IR from ejoy/ejoy2d :: posix (window)

; 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.X_context = type { ptr, i32, i64 }
%union._XEvent = type { [24 x i64] }
%struct.XExposeEvent = type { i32, i64, i32, ptr, i64, i32, i32, i32, i32, i32 }
%struct.XButtonEvent = type { i32, i64, i32, ptr, i64, i64, i64, i64, i32, i32, i32, i32, i32, i32, i32 }
%struct.XClientMessageEvent = type { i32, i64, i32, ptr, i64, i64, i32, %union.anon }
%union.anon = type { [5 x i64] }

@g_X = external dso_local global %struct.X_context, align 8
@wm_delete_window = external hidden global i64, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @main(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca %union._XEvent, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca [255 x i8], align 16
  %10 = alloca i32, align 4
  store i32 0, ptr %3, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 192, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 255, ptr %9) #3
  call void @init_x()
  call void (...) @font_init()
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = load ptr, ptr %5, align 8, !tbaa !9
  call void (i32, ptr, ...) @ejoy2d_win_init(i32 noundef %11, ptr noundef %12)
  br label %13

13:                                               ; preds = %80, %2
  br label %14

14:                                               ; preds = %69, %23, %13
  %15 = load ptr, ptr @g_X, align 8, !tbaa !11
  %16 = call i32 @XPending(ptr noundef %15)
  %17 = icmp sgt i32 %16, 0
  br i1 %17, label %18, label %70

18:                                               ; preds = %14
  %19 = load ptr, ptr @g_X, align 8, !tbaa !11
  %20 = call i32 @XNextEvent(ptr noundef %19, ptr noundef %6)
  %21 = call i32 @XFilterEvent(ptr noundef %6, i64 noundef 0)
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %24

23:                                               ; preds = %18
  br label %14, !llvm.loop !14

24:                                               ; preds = %18
  %25 = load i32, ptr %6, align 8, !tbaa !16
  switch i32 %25, label %69 [
    i32 12, label %26
    i32 2, label %32
    i32 4, label %46
    i32 5, label %51
    i32 6, label %56
    i32 33, label %61
  ]

26:                                               ; preds = %24
  %27 = getelementptr inbounds %struct.XExposeEvent, ptr %6, i32 0, i32 9
  %28 = load i32, ptr %27, align 8, !tbaa !16
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %26
  call void @update_frame()
  br label %31

31:                                               ; preds = %30, %26
  br label %69

32:                                               ; preds = %24
  %33 = getelementptr inbounds [255 x i8], ptr %9, i64 0, i64 0
  %34 = call i32 @XLookupString(ptr noundef %6, ptr noundef %33, i32 noundef 255, ptr noundef %8, ptr noundef null)
  %35 = getelementptr inbounds [255 x i8], ptr %9, i64 0, i64 0
  %36 = load i8, ptr %35, align 16, !tbaa !16
  %37 = sext i8 %36 to i32
  %38 = icmp eq i32 %37, 113
  br i1 %38, label %44, label %39

39:                                               ; preds = %32
  %40 = getelementptr inbounds [255 x i8], ptr %9, i64 0, i64 0
  %41 = load i8, ptr %40, align 16, !tbaa !16
  %42 = sext i8 %41 to i32
  %43 = icmp eq i32 %42, 81
  br i1 %43, label %44, label %45

44:                                               ; preds = %39, %32
  call void @close_x()
  br label %45

45:                                               ; preds = %44, %39
  br label %69

46:                                               ; preds = %24
  %47 = getelementptr inbounds %struct.XButtonEvent, ptr %6, i32 0, i32 8
  %48 = load i32, ptr %47, align 8, !tbaa !16
  %49 = getelementptr inbounds %struct.XButtonEvent, ptr %6, i32 0, i32 9
  %50 = load i32, ptr %49, align 4, !tbaa !16
  call void @ejoy2d_win_touch(i32 noundef %48, i32 noundef %50, i32 noundef 0)
  br label %69

51:                                               ; preds = %24
  %52 = getelementptr inbounds %struct.XButtonEvent, ptr %6, i32 0, i32 8
  %53 = load i32, ptr %52, align 8, !tbaa !16
  %54 = getelementptr inbounds %struct.XButtonEvent, ptr %6, i32 0, i32 9
  %55 = load i32, ptr %54, align 4, !tbaa !16
  call void @ejoy2d_win_touch(i32 noundef %53, i32 noundef %55, i32 noundef 1)
  br label %69

56:                                               ; preds = %24
  %57 = getelementptr inbounds %struct.XButtonEvent, ptr %6, i32 0, i32 8
  %58 = load i32, ptr %57, align 8, !tbaa !16
  %59 = getelementptr inbounds %struct.XButtonEvent, ptr %6, i32 0, i32 9
  %60 = load i32, ptr %59, align 4, !tbaa !16
  call void @ejoy2d_win_touch(i32 noundef %58, i32 noundef %60, i32 noundef 2)
  br label %69

61:                                               ; preds = %24
  %62 = getelementptr inbounds %struct.XClientMessageEvent, ptr %6, i32 0, i32 7
  %63 = getelementptr inbounds [5 x i64], ptr %62, i64 0, i64 0
  %64 = load i64, ptr %63, align 8, !tbaa !16
  %65 = load i64, ptr @wm_delete_window, align 8, !tbaa !17
  %66 = icmp eq i64 %64, %65
  br i1 %66, label %67, label %68

67:                                               ; preds = %61
  call void @close_x()
  br label %68

68:                                               ; preds = %67, %61
  br label %69

69:                                               ; preds = %68, %56, %51, %46, %45, %31, %24
  br label %14, !llvm.loop !14

70:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %71 = call i32 @_gettime()
  store i32 %71, ptr %10, align 4, !tbaa !5
  %72 = load i32, ptr %10, align 4, !tbaa !5
  %73 = load i32, ptr %7, align 4, !tbaa !5
  %74 = sub i32 %72, %73
  %75 = icmp uge i32 %74, 1
  br i1 %75, label %76, label %78

76:                                               ; preds = %70
  %77 = load i32, ptr %10, align 4, !tbaa !5
  store i32 %77, ptr %7, align 4, !tbaa !5
  call void (...) @ejoy2d_win_update()
  call void @update_frame()
  br label %80

78:                                               ; preds = %70
  %79 = call i32 @usleep(i32 noundef 1000)
  br label %80

80:                                               ; preds = %78, %76
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %13
}

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

declare void @font_init(...) #2

declare void @ejoy2d_win_init(...) #2

declare i32 @XPending(ptr noundef) #2

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

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

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

declare void @ejoy2d_win_touch(i32 noundef, i32 noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @_gettime() #0

declare void @ejoy2d_win_update(...) #2

declare i32 @usleep(i32 noundef) #2

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

; Function Attrs: nounwind uwtable
declare hidden void @init_x() #0

; Function Attrs: nounwind uwtable
declare hidden void @update_frame() #0

; Function Attrs: nounwind uwtable
declare hidden void @close_x() #0

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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 = !{!"X_context", !10, i64 0, !6, i64 8, !13, i64 16}
!13 = !{!"long", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!7, !7, i64 0}
!17 = !{!13, !13, 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.X_context = type { ptr, i32, i64 }
%union._XEvent = type { [24 x i64] }

@g_X = external dso_local local_unnamed_addr global %struct.X_context, align 8
@wm_delete_window = external hidden local_unnamed_addr global i64, align 8

; Function Attrs: noreturn nounwind uwtable
define dso_local noundef i32 @main(i32 noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = alloca %union._XEvent, align 8
  %4 = alloca i64, align 8
  %5 = alloca [255 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 192, ptr nonnull %3) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #4
  call void @llvm.lifetime.start.p0(i64 255, ptr nonnull %5) #4
  tail call void @init_x()
  tail call void (...) @font_init() #4
  tail call void (i32, ptr, ...) @ejoy2d_win_init(i32 noundef %0, ptr noundef %1) #4
  %6 = getelementptr inbounds i8, ptr %3, i64 56
  %7 = getelementptr inbounds i8, ptr %3, i64 64
  %8 = getelementptr inbounds i8, ptr %3, i64 68
  br label %.outer

.outer:                                           ; preds = %45, %2
  %.0.ph = phi i32 [ %44, %45 ], [ 0, %2 ]
  br label %9

9:                                                ; preds = %.outer, %46
  %10 = load ptr, ptr @g_X, align 8, !tbaa !5
  %11 = call i32 @XPending(ptr noundef %10) #4
  %12 = icmp sgt i32 %11, 0
  br i1 %12, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %9, %.backedge
  %13 = load ptr, ptr @g_X, align 8, !tbaa !5
  %14 = call i32 @XNextEvent(ptr noundef %13, ptr noundef nonnull %3) #4
  %15 = call i32 @XFilterEvent(ptr noundef nonnull %3, i64 noundef 0) #4
  %.not7 = icmp eq i32 %15, 0
  br i1 %.not7, label %16, label %.backedge

16:                                               ; preds = %.lr.ph
  %17 = load i32, ptr %3, align 8, !tbaa !12
  switch i32 %17, label %.backedge [
    i32 12, label %18
    i32 2, label %22
    i32 4, label %27
    i32 5, label %30
    i32 6, label %33
    i32 33, label %36
  ]

18:                                               ; preds = %16
  %19 = load i32, ptr %6, align 8, !tbaa !12
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %.backedge

21:                                               ; preds = %18
  call void @update_frame()
  br label %.backedge

22:                                               ; preds = %16
  %23 = call i32 @XLookupString(ptr noundef nonnull %3, ptr noundef nonnull %5, i32 noundef 255, ptr noundef nonnull %4, ptr noundef null) #4
  %24 = load i8, ptr %5, align 16
  %25 = and i8 %24, -33
  %or.cond = icmp eq i8 %25, 81
  br i1 %or.cond, label %26, label %.backedge

26:                                               ; preds = %22
  call void @close_x()
  br label %.backedge

27:                                               ; preds = %16
  %28 = load i32, ptr %7, align 8, !tbaa !12
  %29 = load i32, ptr %8, align 4, !tbaa !12
  call void @ejoy2d_win_touch(i32 noundef %28, i32 noundef %29, i32 noundef 0) #4
  br label %.backedge

30:                                               ; preds = %16
  %31 = load i32, ptr %7, align 8, !tbaa !12
  %32 = load i32, ptr %8, align 4, !tbaa !12
  call void @ejoy2d_win_touch(i32 noundef %31, i32 noundef %32, i32 noundef 1) #4
  br label %.backedge

33:                                               ; preds = %16
  %34 = load i32, ptr %7, align 8, !tbaa !12
  %35 = load i32, ptr %8, align 4, !tbaa !12
  call void @ejoy2d_win_touch(i32 noundef %34, i32 noundef %35, i32 noundef 2) #4
  br label %.backedge

36:                                               ; preds = %16
  %37 = load i64, ptr %6, align 8, !tbaa !12
  %38 = load i64, ptr @wm_delete_window, align 8, !tbaa !13
  %39 = icmp eq i64 %37, %38
  br i1 %39, label %40, label %.backedge

40:                                               ; preds = %36
  call void @close_x()
  br label %.backedge

.backedge:                                        ; preds = %16, %27, %30, %33, %21, %18, %22, %26, %40, %36, %.lr.ph
  %41 = load ptr, ptr @g_X, align 8, !tbaa !5
  %42 = call i32 @XPending(ptr noundef %41) #4
  %43 = icmp sgt i32 %42, 0
  br i1 %43, label %.lr.ph, label %._crit_edge, !llvm.loop !14

._crit_edge:                                      ; preds = %.backedge, %9
  %44 = call i32 @_gettime()
  %.not = icmp eq i32 %44, %.0.ph
  br i1 %.not, label %46, label %45

45:                                               ; preds = %._crit_edge
  call void (...) @ejoy2d_win_update() #4
  call void @update_frame()
  br label %.outer

46:                                               ; preds = %._crit_edge
  %47 = call i32 @usleep(i32 noundef 1000) #4
  br label %9
}

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

declare void @font_init(...) local_unnamed_addr #2

declare void @ejoy2d_win_init(...) local_unnamed_addr #2

declare i32 @XPending(ptr noundef) local_unnamed_addr #2

declare i32 @XNextEvent(ptr noundef, ptr noundef) local_unnamed_addr #2

declare i32 @XFilterEvent(ptr noundef, i64 noundef) local_unnamed_addr #2

declare i32 @XLookupString(ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

declare void @ejoy2d_win_touch(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden i32 @_gettime() local_unnamed_addr #3

declare void @ejoy2d_win_update(...) local_unnamed_addr #2

declare i32 @usleep(i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden void @init_x() local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare hidden void @update_frame() local_unnamed_addr #3

; Function Attrs: nounwind uwtable
declare hidden void @close_x() local_unnamed_addr #3

attributes #0 = { noreturn 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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, !7, i64 0}
!6 = !{!"X_context", !7, i64 0, !10, i64 8, !11, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!8, !8, i64 0}
!13 = !{!11, !11, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass