; LLVM IR from s-matyukevich/raspberry-pi-os :: exercises/lesson01/2/gcrisis/uart-boot/downloader (raspiloader)

; 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.termios = type { i32, i32, i32, i32, i8, [32 x i8], i32, i32 }

@.str = external hidden unnamed_addr constant [8 x i8], align 1
@stderr = external global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [24 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [12 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @open_serial(ptr noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca %struct.termios, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 60, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = call i32 (ptr, i32, ...) @open(ptr noundef %7, i32 noundef 258)
  store i32 %8, ptr %5, align 4, !tbaa !9
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %9)
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = icmp eq i32 %11, -1
  br i1 %12, label %13, label %14

13:                                               ; preds = %1
  store i32 -1, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %53

14:                                               ; preds = %1
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = call i32 @isatty(i32 noundef %15) #4
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %23, label %18

18:                                               ; preds = %14
  %19 = load ptr, ptr @stderr, align 8, !tbaa !5
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %19, ptr noundef @.str.1, ptr noundef %20) #4
  %22 = load i32, ptr %5, align 4, !tbaa !9
  call void @do_exit(i32 noundef %22, i32 noundef 1)
  br label %23

23:                                               ; preds = %18, %14
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = call i32 @tcgetattr(i32 noundef %24, ptr noundef %4) #4
  %26 = icmp eq i32 %25, -1
  br i1 %26, label %27, label %29

27:                                               ; preds = %23
  call void @perror(ptr noundef @.str.2)
  %28 = load i32, ptr %5, align 4, !tbaa !9
  call void @do_exit(i32 noundef %28, i32 noundef 1)
  br label %29

29:                                               ; preds = %27, %23
  %30 = getelementptr inbounds %struct.termios, ptr %4, i32 0, i32 5
  %31 = getelementptr inbounds [32 x i8], ptr %30, i64 0, i64 5
  store i8 0, ptr %31, align 1, !tbaa !11
  %32 = getelementptr inbounds %struct.termios, ptr %4, i32 0, i32 5
  %33 = getelementptr inbounds [32 x i8], ptr %32, i64 0, i64 6
  store i8 0, ptr %33, align 1, !tbaa !11
  %34 = getelementptr inbounds %struct.termios, ptr %4, i32 0, i32 0
  store i32 0, ptr %34, align 4, !tbaa !12
  %35 = getelementptr inbounds %struct.termios, ptr %4, i32 0, i32 1
  store i32 0, ptr %35, align 4, !tbaa !14
  %36 = getelementptr inbounds %struct.termios, ptr %4, i32 0, i32 2
  store i32 2224, ptr %36, align 4, !tbaa !15
  %37 = getelementptr inbounds %struct.termios, ptr %4, i32 0, i32 3
  store i32 0, ptr %37, align 4, !tbaa !16
  %38 = call i32 @cfsetispeed(ptr noundef %4, i32 noundef 4098) #4
  %39 = icmp slt i32 %38, 0
  br i1 %39, label %43, label %40

40:                                               ; preds = %29
  %41 = call i32 @cfsetospeed(ptr noundef %4, i32 noundef 4098) #4
  %42 = icmp slt i32 %41, 0
  br i1 %42, label %43, label %45

43:                                               ; preds = %40, %29
  call void @perror(ptr noundef @.str.3)
  %44 = load i32, ptr %5, align 4, !tbaa !9
  call void @do_exit(i32 noundef %44, i32 noundef 1)
  br label %45

45:                                               ; preds = %43, %40
  %46 = load i32, ptr %5, align 4, !tbaa !9
  %47 = call i32 @tcsetattr(i32 noundef %46, i32 noundef 2, ptr noundef %4) #4
  %48 = icmp eq i32 %47, -1
  br i1 %48, label %49, label %51

49:                                               ; preds = %45
  call void @perror(ptr noundef @.str.4)
  %50 = load i32, ptr %5, align 4, !tbaa !9
  call void @do_exit(i32 noundef %50, i32 noundef 1)
  br label %51

51:                                               ; preds = %49, %45
  %52 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %52, ptr %2, align 4
  store i32 1, ptr %6, align 4
  br label %53

53:                                               ; preds = %51, %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 60, ptr %4) #4
  %54 = load i32, ptr %2, align 4
  ret i32 %54
}

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

declare i32 @open(ptr noundef, i32 noundef, ...) #2

declare i32 @printf(ptr noundef, ...) #2

; Function Attrs: nounwind
declare i32 @isatty(i32 noundef) #3

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #3

; Function Attrs: nounwind
declare i32 @tcgetattr(i32 noundef, ptr noundef) #3

declare void @perror(ptr noundef) #2

; Function Attrs: nounwind
declare i32 @cfsetispeed(ptr noundef, i32 noundef) #3

; Function Attrs: nounwind
declare i32 @cfsetospeed(ptr noundef, i32 noundef) #3

; Function Attrs: nounwind
declare i32 @tcsetattr(i32 noundef, 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 = { "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 "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 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !10, i64 0}
!13 = !{!"termios", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !7, i64 16, !7, i64 17, !10, i64 52, !10, i64 56}
!14 = !{!13, !10, i64 4}
!15 = !{!13, !10, i64 8}
!16 = !{!13, !10, i64 12}


; 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.termios = type { i32, i32, i32, i32, i8, [32 x i8], i32, i32 }

@.str = external hidden unnamed_addr constant [8 x i8], align 1
@stderr = external local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [17 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [24 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [12 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @open_serial(ptr noundef %0) local_unnamed_addr #0 {
  %2 = alloca %struct.termios, align 16
  call void @llvm.lifetime.start.p0(i64 60, ptr nonnull %2) #5
  %3 = tail call i32 (ptr, i32, ...) @open(ptr noundef %0, i32 noundef 258) #5
  %4 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %3)
  %5 = icmp eq i32 %3, -1
  br i1 %5, label %28, label %6

6:                                                ; preds = %1
  %7 = tail call i32 @isatty(i32 noundef %3) #5
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %8, label %11

8:                                                ; preds = %6
  %9 = load ptr, ptr @stderr, align 8, !tbaa !5
  %10 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %9, ptr noundef nonnull @.str.1, ptr noundef %0) #6
  tail call void @do_exit(i32 noundef %3, i32 noundef 1)
  br label %11

11:                                               ; preds = %8, %6
  %12 = call i32 @tcgetattr(i32 noundef %3, ptr noundef nonnull %2) #5
  %13 = icmp eq i32 %12, -1
  br i1 %13, label %14, label %15

14:                                               ; preds = %11
  call void @perror(ptr noundef nonnull @.str.2) #7
  call void @do_exit(i32 noundef %3, i32 noundef 1)
  br label %15

15:                                               ; preds = %14, %11
  %16 = getelementptr inbounds i8, ptr %2, i64 22
  store i8 0, ptr %16, align 2, !tbaa !9
  %17 = getelementptr inbounds i8, ptr %2, i64 23
  store i8 0, ptr %17, align 1, !tbaa !9
  store <4 x i32> <i32 0, i32 0, i32 2224, i32 0>, ptr %2, align 16, !tbaa !10
  %18 = call i32 @cfsetispeed(ptr noundef nonnull %2, i32 noundef 4098) #5
  %19 = icmp slt i32 %18, 0
  br i1 %19, label %23, label %20

20:                                               ; preds = %15
  %21 = call i32 @cfsetospeed(ptr noundef nonnull %2, i32 noundef 4098) #5
  %22 = icmp slt i32 %21, 0
  br i1 %22, label %23, label %24

23:                                               ; preds = %20, %15
  call void @perror(ptr noundef nonnull @.str.3) #7
  call void @do_exit(i32 noundef %3, i32 noundef 1)
  br label %24

24:                                               ; preds = %23, %20
  %25 = call i32 @tcsetattr(i32 noundef %3, i32 noundef 2, ptr noundef nonnull %2) #5
  %26 = icmp eq i32 %25, -1
  br i1 %26, label %27, label %28

27:                                               ; preds = %24
  call void @perror(ptr noundef nonnull @.str.4) #7
  call void @do_exit(i32 noundef %3, i32 noundef 1)
  br label %28

28:                                               ; preds = %24, %27, %1
  call void @llvm.lifetime.end.p0(i64 60, ptr nonnull %2) #5
  ret i32 %3
}

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

; Function Attrs: nofree
declare noundef i32 @open(ptr nocapture noundef readonly, i32 noundef, ...) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: nounwind
declare i32 @isatty(i32 noundef) local_unnamed_addr #4

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: nounwind
declare i32 @tcgetattr(i32 noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: nofree nounwind
declare void @perror(ptr nocapture noundef readonly) local_unnamed_addr #3

; Function Attrs: nounwind
declare i32 @cfsetispeed(ptr noundef, i32 noundef) local_unnamed_addr #4

; Function Attrs: nounwind
declare i32 @cfsetospeed(ptr noundef, i32 noundef) local_unnamed_addr #4

; Function Attrs: nounwind
declare i32 @tcsetattr(i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nofree "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 = { nofree nounwind "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 "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 #5 = { nounwind }
attributes #6 = { cold nounwind }
attributes #7 = { cold }

!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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass