; LLVM IR from 0intro/wmii :: lib/libstuff/x11 (errors)

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

@_trap_errors = external hidden global i8, align 1
@nerrors = external hidden global i64, align 8
@ignored_xerrors = external global [0 x %struct.ErrorCode], align 1
@.str = external hidden unnamed_addr constant [51 x i8], align 1
@argv0 = external global ptr, align 8
@xlib_errorhandler = external global ptr, align 8
@display = external global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @errorhandler(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = ptrtoint ptr %8 to i64
  call void (i64, ...) @_used(i64 noundef %9)
  %10 = load i8, ptr @_trap_errors, align 1, !tbaa !9, !range !11, !noundef !12
  %11 = trunc i8 %10 to i1
  br i1 %11, label %12, label %15

12:                                               ; preds = %2
  %13 = load i64, ptr @nerrors, align 8, !tbaa !13
  %14 = add nsw i64 %13, 1
  store i64 %14, ptr @nerrors, align 8, !tbaa !13
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %86

15:                                               ; preds = %2
  store ptr @ignored_xerrors, ptr %6, align 8, !tbaa !5
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %71

18:                                               ; preds = %15
  br label %19

19:                                               ; preds = %67, %18
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.ErrorCode, ptr %20, i32 0, i32 0
  %22 = load i8, ptr %21, align 1, !tbaa !15
  %23 = zext i8 %22 to i32
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %31, label %25

25:                                               ; preds = %19
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.ErrorCode, ptr %26, i32 0, i32 1
  %28 = load i8, ptr %27, align 1, !tbaa !17
  %29 = zext i8 %28 to i32
  %30 = icmp ne i32 %29, 0
  br label %31

31:                                               ; preds = %25, %19
  %32 = phi i1 [ true, %19 ], [ %30, %25 ]
  br i1 %32, label %33, label %70

33:                                               ; preds = %31
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.ErrorCode, ptr %34, i32 0, i32 0
  %36 = load i8, ptr %35, align 1, !tbaa !15
  %37 = zext i8 %36 to i32
  %38 = icmp eq i32 %37, 0
  br i1 %38, label %49, label %39

39:                                               ; preds = %33
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.ErrorCode, ptr %40, i32 0, i32 0
  %42 = load i8, ptr %41, align 1, !tbaa !15
  %43 = zext i8 %42 to i32
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.XErrorEvent, ptr %44, i32 0, i32 5
  %46 = load i8, ptr %45, align 1, !tbaa !18
  %47 = zext i8 %46 to i32
  %48 = icmp eq i32 %43, %47
  br i1 %48, label %49, label %66

49:                                               ; preds = %39, %33
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.ErrorCode, ptr %50, i32 0, i32 1
  %52 = load i8, ptr %51, align 1, !tbaa !17
  %53 = zext i8 %52 to i32
  %54 = icmp eq i32 %53, 0
  br i1 %54, label %65, label %55

55:                                               ; preds = %49
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.ErrorCode, ptr %56, i32 0, i32 1
  %58 = load i8, ptr %57, align 1, !tbaa !17
  %59 = zext i8 %58 to i32
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.XErrorEvent, ptr %60, i32 0, i32 4
  %62 = load i8, ptr %61, align 8, !tbaa !21
  %63 = zext i8 %62 to i32
  %64 = icmp eq i32 %59, %63
  br i1 %64, label %65, label %66

65:                                               ; preds = %55, %49
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %86

66:                                               ; preds = %55, %39
  br label %67

67:                                               ; preds = %66
  %68 = load ptr, ptr %6, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.ErrorCode, ptr %68, i32 1
  store ptr %69, ptr %6, align 8, !tbaa !5
  br label %19, !llvm.loop !22

70:                                               ; preds = %31
  br label %71

71:                                               ; preds = %70, %15
  %72 = load ptr, ptr @argv0, align 8, !tbaa !5
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.XErrorEvent, ptr %73, i32 0, i32 5
  %75 = load i8, ptr %74, align 1, !tbaa !18
  %76 = zext i8 %75 to i32
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.XErrorEvent, ptr %77, i32 0, i32 4
  %79 = load i8, ptr %78, align 8, !tbaa !21
  %80 = zext i8 %79 to i32
  %81 = call i32 (i32, ptr, ...) @fprint(i32 noundef 2, ptr noundef @.str, ptr noundef %72, i32 noundef %76, i32 noundef %80)
  %82 = load ptr, ptr @xlib_errorhandler, align 8, !tbaa !5
  %83 = load ptr, ptr @display, align 8, !tbaa !5
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = call i32 %82(ptr noundef %83, ptr noundef %84)
  store i32 %85, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %86

86:                                               ; preds = %71, %65, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %87 = load i32, ptr %3, align 4
  ret i32 %87
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @_used(i64 noundef, ...) #2

declare i32 @fprint(i32 noundef, ptr noundef, ...) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 = { "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, !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 = !{!"_Bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!16, !7, i64 0}
!16 = !{!"ErrorCode", !7, i64 0, !7, i64 1}
!17 = !{!16, !7, i64 1}
!18 = !{!19, !7, i64 33}
!19 = !{!"", !20, i64 0, !6, i64 8, !14, i64 16, !14, i64 24, !7, i64 32, !7, i64 33, !7, i64 34}
!20 = !{!"int", !7, i64 0}
!21 = !{!19, !7, i64 32}
!22 = distinct !{!22, !23}
!23 = !{!"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.ErrorCode = type { i8, i8 }

@_trap_errors = external hidden local_unnamed_addr global i8, align 1
@nerrors = external hidden local_unnamed_addr global i64, align 8
@ignored_xerrors = external local_unnamed_addr global [0 x %struct.ErrorCode], align 1
@.str = external hidden unnamed_addr constant [51 x i8], align 1
@argv0 = external local_unnamed_addr global ptr, align 8
@xlib_errorhandler = external local_unnamed_addr global ptr, align 8
@display = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local i32 @errorhandler(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = ptrtoint ptr %0 to i64
  tail call void (i64, ...) @_used(i64 noundef %3)
  %4 = load i8, ptr @_trap_errors, align 1, !tbaa !5, !range !9, !noundef !10
  %5 = trunc nuw i8 %4 to i1
  br i1 %5, label %8, label %.preheader

.preheader:                                       ; preds = %2
  %6 = getelementptr inbounds i8, ptr %1, i64 33
  %7 = getelementptr inbounds i8, ptr %1, i64 32
  br label %11

8:                                                ; preds = %2
  %9 = load i64, ptr @nerrors, align 8, !tbaa !11
  %10 = add nsw i64 %9, 1
  store i64 %10, ptr @nerrors, align 8, !tbaa !11
  br label %.loopexit

11:                                               ; preds = %.preheader, %22
  %.0 = phi ptr [ %23, %22 ], [ @ignored_xerrors, %.preheader ]
  %12 = load i8, ptr %.0, align 1, !tbaa !13
  %.not = icmp eq i8 %12, 0
  br i1 %.not, label %13, label %.critedge

13:                                               ; preds = %11
  %14 = getelementptr inbounds i8, ptr %.0, i64 1
  %15 = load i8, ptr %14, align 1, !tbaa !15
  %.not17 = icmp eq i8 %15, 0
  br i1 %.not17, label %24, label %.critedge.thread.thread

.critedge:                                        ; preds = %11
  %16 = load i8, ptr %6, align 1, !tbaa !16
  %17 = icmp eq i8 %12, %16
  br i1 %17, label %.critedge.thread, label %22

.critedge.thread:                                 ; preds = %.critedge
  %.phi.trans.insert = getelementptr inbounds i8, ptr %.0, i64 1
  %.pre = load i8, ptr %.phi.trans.insert, align 1, !tbaa !15
  %18 = icmp eq i8 %.pre, 0
  br i1 %18, label %.loopexit, label %.critedge.thread.thread

.critedge.thread.thread:                          ; preds = %13, %.critedge.thread
  %19 = phi i8 [ %.pre, %.critedge.thread ], [ %15, %13 ]
  %20 = load i8, ptr %7, align 8, !tbaa !20
  %21 = icmp eq i8 %19, %20
  br i1 %21, label %.loopexit, label %22

22:                                               ; preds = %.critedge, %.critedge.thread.thread
  %23 = getelementptr inbounds i8, ptr %.0, i64 2
  br label %11, !llvm.loop !21

24:                                               ; preds = %13
  %25 = load ptr, ptr @argv0, align 8, !tbaa !23
  %26 = load i8, ptr %6, align 1, !tbaa !16
  %27 = zext i8 %26 to i32
  %28 = load i8, ptr %7, align 8, !tbaa !20
  %29 = zext i8 %28 to i32
  %30 = tail call i32 (i32, ptr, ...) @fprint(i32 noundef 2, ptr noundef nonnull @.str, ptr noundef %25, i32 noundef %27, i32 noundef %29) #3
  %31 = load ptr, ptr @xlib_errorhandler, align 8, !tbaa !23
  %32 = load ptr, ptr @display, align 8, !tbaa !23
  %33 = tail call i32 %31(ptr noundef %32, ptr noundef %1) #3
  br label %.loopexit

.loopexit:                                        ; preds = %.critedge.thread, %.critedge.thread.thread, %24, %8
  %.014 = phi i32 [ 0, %8 ], [ %33, %24 ], [ 0, %.critedge.thread.thread ], [ 0, %.critedge.thread ]
  ret i32 %.014
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @_used(i64 noundef, ...) local_unnamed_addr #1

declare i32 @fprint(i32 noundef, ptr noundef, ...) local_unnamed_addr #2

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 = { inlinehint 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 = !{!"_Bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{i8 0, i8 2}
!10 = !{}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !7, i64 0}
!14 = !{!"ErrorCode", !7, i64 0, !7, i64 1}
!15 = !{!14, !7, i64 1}
!16 = !{!17, !7, i64 33}
!17 = !{!"", !18, i64 0, !19, i64 8, !12, i64 16, !12, i64 24, !7, i64 32, !7, i64 33, !7, i64 34}
!18 = !{!"int", !7, i64 0}
!19 = !{!"any pointer", !7, i64 0}
!20 = !{!17, !7, i64 32}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!19, !19, i64 0}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass