; LLVM IR from niessner/Matterport :: code/gaps/pkgs/fglut (freeglut_joystick)

; 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.tagSFG_State = type { %struct.tagSFG_XYUse, %struct.tagSFG_XYUse, i32, i8, i32, i8, i8, i8, i8, i32, i32, i32, i32, i32, i64, %struct.tagSFG_List, %struct.tagSFG_List, ptr, i32, ptr, ptr, %struct.tagSFG_XYUse, i32, i32, i32, i32, ptr, i8, i8, i32, i32, i32, i32, i32, i32 }
%struct.tagSFG_List = type { ptr, ptr }
%struct.tagSFG_XYUse = type { i32, i32, i8 }
%struct.tagSFG_Joystick = type { %struct.js_event, i32, [16 x float], [128 x i8], i32, i32, i8, [128 x i8], i32, i32, [16 x float], [16 x float], [16 x float], [16 x float], [16 x float] }
%struct.js_event = type { i32, i16, i8, i8 }

@fgState = external global %struct.tagSFG_State, align 8
@fgJoystick = external hidden global [2 x ptr], align 16

; Function Attrs: nounwind uwtable
declare dso_local void @fgInitialiseJoysticks() #0

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

; 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
define dso_local i32 @fgJoystickDetect() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  call void @fgInitialiseJoysticks()
  %4 = load i8, ptr getelementptr inbounds (%struct.tagSFG_State, ptr @fgState, i32 0, i32 27), align 8, !tbaa !5
  %5 = icmp ne i8 %4, 0
  br i1 %5, label %7, label %6

6:                                                ; preds = %0
  store i32 0, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %31

7:                                                ; preds = %0
  store i32 0, ptr %2, align 4, !tbaa !14
  br label %8

8:                                                ; preds = %27, %7
  %9 = load i32, ptr %2, align 4, !tbaa !14
  %10 = icmp slt i32 %9, 2
  br i1 %10, label %11, label %30

11:                                               ; preds = %8
  %12 = load i32, ptr %2, align 4, !tbaa !14
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [2 x ptr], ptr @fgJoystick, i64 0, i64 %13
  %15 = load ptr, ptr %14, align 8, !tbaa !15
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %26

17:                                               ; preds = %11
  %18 = load i32, ptr %2, align 4, !tbaa !14
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [2 x ptr], ptr @fgJoystick, i64 0, i64 %19
  %21 = load ptr, ptr %20, align 8, !tbaa !15
  %22 = getelementptr inbounds %struct.tagSFG_Joystick, ptr %21, i32 0, i32 6
  %23 = load i8, ptr %22, align 4, !tbaa !16
  %24 = icmp ne i8 %23, 0
  br i1 %24, label %26, label %25

25:                                               ; preds = %17
  store i32 1, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %31

26:                                               ; preds = %17, %11
  br label %27

27:                                               ; preds = %26
  %28 = load i32, ptr %2, align 4, !tbaa !14
  %29 = add nsw i32 %28, 1
  store i32 %29, ptr %2, align 4, !tbaa !14
  br label %8, !llvm.loop !20

30:                                               ; preds = %8
  store i32 0, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %31

31:                                               ; preds = %30, %25, %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  %32 = load i32, ptr %1, align 4
  ret i32 %32
}

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 = { 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, !9, i64 176}
!6 = !{!"tagSFG_State", !7, i64 0, !7, i64 12, !8, i64 24, !9, i64 28, !8, i64 32, !9, i64 36, !9, i64 37, !9, i64 38, !9, i64 39, !8, i64 40, !8, i64 44, !8, i64 48, !8, i64 52, !8, i64 56, !11, i64 64, !12, i64 72, !12, i64 88, !13, i64 104, !8, i64 112, !13, i64 120, !13, i64 128, !7, i64 136, !8, i64 148, !8, i64 152, !8, i64 156, !8, i64 160, !13, i64 168, !9, i64 176, !9, i64 177, !8, i64 180, !8, i64 184, !8, i64 188, !8, i64 192, !8, i64 196, !8, i64 200}
!7 = !{!"tagSFG_XYUse", !8, i64 0, !8, i64 4, !9, i64 8}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!"tagSFG_List", !13, i64 0, !13, i64 8}
!13 = !{!"any pointer", !9, i64 0}
!14 = !{!8, !8, i64 0}
!15 = !{!13, !13, i64 0}
!16 = !{!17, !9, i64 212}
!17 = !{!"tagSFG_Joystick", !18, i64 0, !8, i64 8, !9, i64 12, !9, i64 76, !8, i64 204, !8, i64 208, !9, i64 212, !9, i64 213, !8, i64 344, !8, i64 348, !9, i64 352, !9, i64 416, !9, i64 480, !9, i64 544, !9, i64 608}
!18 = !{!"js_event", !8, i64 0, !19, i64 4, !9, i64 6, !9, i64 7}
!19 = !{!"short", !9, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}


; 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.tagSFG_State = type { %struct.tagSFG_XYUse, %struct.tagSFG_XYUse, i32, i8, i32, i8, i8, i8, i8, i32, i32, i32, i32, i32, i64, %struct.tagSFG_List, %struct.tagSFG_List, ptr, i32, ptr, ptr, %struct.tagSFG_XYUse, i32, i32, i32, i32, ptr, i8, i8, i32, i32, i32, i32, i32, i32 }
%struct.tagSFG_List = type { ptr, ptr }
%struct.tagSFG_XYUse = type { i32, i32, i8 }

@fgState = external local_unnamed_addr global %struct.tagSFG_State, align 8
@fgJoystick = external hidden local_unnamed_addr global [2 x ptr], align 16

; Function Attrs: nounwind uwtable
declare dso_local void @fgInitialiseJoysticks() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @fgJoystickDetect() local_unnamed_addr #0 {
  tail call void @fgInitialiseJoysticks()
  %1 = load i8, ptr getelementptr inbounds (i8, ptr @fgState, i64 176), align 8, !tbaa !5
  %.not = icmp eq i8 %1, 0
  br i1 %.not, label %.loopexit, label %.preheader.preheader

.preheader.preheader:                             ; preds = %0
  %2 = load ptr, ptr @fgJoystick, align 16, !tbaa !14
  %.not7 = icmp eq ptr %2, null
  br i1 %.not7, label %.preheader.1, label %3

3:                                                ; preds = %.preheader.preheader
  %4 = getelementptr inbounds i8, ptr %2, i64 212
  %5 = load i8, ptr %4, align 4, !tbaa !15
  %.not8 = icmp eq i8 %5, 0
  br i1 %.not8, label %.loopexit, label %.preheader.1

.preheader.1:                                     ; preds = %.preheader.preheader, %3
  %6 = load ptr, ptr getelementptr inbounds (i8, ptr @fgJoystick, i64 8), align 8, !tbaa !14
  %.not7.1 = icmp eq ptr %6, null
  br i1 %.not7.1, label %10, label %7

7:                                                ; preds = %.preheader.1
  %8 = getelementptr inbounds i8, ptr %6, i64 212
  %9 = load i8, ptr %8, align 4, !tbaa !15
  %.not8.1 = icmp eq i8 %9, 0
  br i1 %.not8.1, label %.loopexit, label %10

10:                                               ; preds = %7, %.preheader.1
  br label %.loopexit

.loopexit:                                        ; preds = %3, %7, %10, %0
  %.05 = phi i32 [ 0, %0 ], [ 1, %3 ], [ 1, %7 ], [ 0, %10 ]
  ret i32 %.05
}

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, !9, i64 176}
!6 = !{!"tagSFG_State", !7, i64 0, !7, i64 12, !8, i64 24, !9, i64 28, !8, i64 32, !9, i64 36, !9, i64 37, !9, i64 38, !9, i64 39, !8, i64 40, !8, i64 44, !8, i64 48, !8, i64 52, !8, i64 56, !11, i64 64, !12, i64 72, !12, i64 88, !13, i64 104, !8, i64 112, !13, i64 120, !13, i64 128, !7, i64 136, !8, i64 148, !8, i64 152, !8, i64 156, !8, i64 160, !13, i64 168, !9, i64 176, !9, i64 177, !8, i64 180, !8, i64 184, !8, i64 188, !8, i64 192, !8, i64 196, !8, i64 200}
!7 = !{!"tagSFG_XYUse", !8, i64 0, !8, i64 4, !9, i64 8}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"long", !9, i64 0}
!12 = !{!"tagSFG_List", !13, i64 0, !13, i64 8}
!13 = !{!"any pointer", !9, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!16, !9, i64 212}
!16 = !{!"tagSFG_Joystick", !17, i64 0, !8, i64 8, !9, i64 12, !9, i64 76, !8, i64 204, !8, i64 208, !9, i64 212, !9, i64 213, !8, i64 344, !8, i64 348, !9, i64 352, !9, i64 416, !9, i64 480, !9, i64 544, !9, i64 608}
!17 = !{!"js_event", !8, i64 0, !18, i64 4, !9, i64 6, !9, i64 7}
!18 = !{!"short", !9, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,SROAPass,SimplifyCFGPass,TailCallElimPass