#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int HSMMCSDIntClock(unsigned int, int);
scalar_t__ HSMMCSDIsIntClockStable(unsigned int, int);
int HSMMCSDBusFreqSet(unsigned int baseAddr, unsigned int freq_in,
                      unsigned int freq_out, unsigned int bypass) {
  volatile unsigned int clkd = 0;
  volatile unsigned int regVal = 0;
  if (HSMMCSDIntClock(baseAddr, ((0x1u) << (0x00000000u))) == -1) {
    return -1;
  }
  if (bypass == 0) {
    clkd = freq_in / freq_out;
    clkd = (clkd < 2) ? 2 : clkd;
    clkd = (clkd > 1023) ? 1023 : clkd;
    while ((freq_in / clkd) > freq_out) {
      if (clkd == 1023) {
        return -1;
      }
      clkd++;
    }
    regVal =
        (*((volatile unsigned int *)(baseAddr + (0x22C)))) & ~(0x0000FFC0u);
    (*((volatile unsigned int *)(baseAddr + (0x22C)))) =
        regVal | (clkd << (0x00000006u));
    if (HSMMCSDIsIntClockStable(baseAddr, 0xFFFF) == 0) {
      return -1;
    }
    (*((volatile unsigned int *)(baseAddr + (0x22C)))) |= (0x00000004u);
  }
  return 0;
}


x86 assembly (clang, -O0):
<HSMMCSDBusFreqSet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1040 <HSMMCSDIntClock@plt>
cmp    $0xffffffff,%eax
jne    1164 <HSMMCSDBusFreqSet+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <HSMMCSDBusFreqSet+0x146>
cmpl   $0x0,-0x14(%rbp)
jne    125f <HSMMCSDBusFreqSet+0x13f>
mov    -0xc(%rbp),%eax
xor    %edx,%edx
divl   -0x10(%rbp)
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    $0x2,%eax
jae    1192 <HSMMCSDBusFreqSet+0x72>
mov    $0x2,%eax
mov    %eax,-0x20(%rbp)
jmp    1198 <HSMMCSDBusFreqSet+0x78>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    $0x3ff,%eax
jbe    11b9 <HSMMCSDBusFreqSet+0x99>
mov    $0x3ff,%eax
mov    %eax,-0x24(%rbp)
jmp    11bf <HSMMCSDBusFreqSet+0x9f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x18(%rbp),%ecx
xor    %edx,%edx
div    %ecx
cmp    -0x10(%rbp),%eax
jbe    1200 <HSMMCSDBusFreqSet+0xe0>
mov    -0x18(%rbp),%eax
cmp    $0x3ff,%eax
jne    11f2 <HSMMCSDBusFreqSet+0xd2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <HSMMCSDBusFreqSet+0x146>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11c5 <HSMMCSDBusFreqSet+0xa5>
mov    -0x8(%rbp),%eax
add    $0x22c,%eax
mov    %eax,%eax
mov    (%rax),%eax
and    $0xffff003f,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
shl    $0x6,%eax
or     %eax,%ecx
mov    -0x8(%rbp),%eax
add    $0x22c,%eax
mov    %eax,%eax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
mov    $0xffff,%esi
call   1030 <HSMMCSDIsIntClockStable@plt>
cmp    $0x0,%rax
jne    124e <HSMMCSDBusFreqSet+0x12e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1266 <HSMMCSDBusFreqSet+0x146>
mov    -0x8(%rbp),%eax
add    $0x22c,%eax
mov    %eax,%eax
mov    (%rax),%ecx
or     $0x4,%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HSMMCSDBusFreqSet>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %esi,%ebx
mov    %edi,%r14d
movl   $0x0,(%rsp)
movl   $0x0,0x4(%rsp)
mov    $0x1,%esi
call   1040 <HSMMCSDIntClock@plt>
cmp    $0xffffffff,%eax
je     11ac <HSMMCSDBusFreqSet+0x8c>
xor    %eax,%eax
test   %ebp,%ebp
jne    11b1 <HSMMCSDBusFreqSet+0x91>
mov    %ebx,%eax
xor    %edx,%edx
div    %r15d
mov    %eax,(%rsp)
mov    (%rsp),%ecx
mov    $0x2,%eax
cmp    $0x2,%ecx
jb     116f <HSMMCSDBusFreqSet+0x4f>
mov    (%rsp),%eax
mov    %eax,(%rsp)
mov    (%rsp),%ecx
mov    $0x3ff,%eax
cmp    $0x3ff,%ecx
ja     1185 <HSMMCSDBusFreqSet+0x65>
mov    (%rsp),%eax
mov    %eax,(%rsp)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%eax
xor    %edx,%edx
divl   (%rsp)
cmp    %r15d,%eax
jbe    11bc <HSMMCSDBusFreqSet+0x9c>
mov    (%rsp),%eax
cmp    $0x3ff,%eax
je     11ac <HSMMCSDBusFreqSet+0x8c>
addl   $0x1,(%rsp)
jmp    1190 <HSMMCSDBusFreqSet+0x70>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x22c(%r14),%ebx
mov    $0xffff003f,%eax
and    (%rbx),%eax
mov    %eax,0x4(%rsp)
mov    0x4(%rsp),%eax
mov    (%rsp),%ecx
shl    $0x6,%ecx
or     %eax,%ecx
mov    %ecx,(%rbx)
mov    %r14d,%edi
mov    $0xffff,%esi
call   1030 <HSMMCSDIsIntClockStable@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     11b1 <HSMMCSDBusFreqSet+0x91>
orl    $0x4,(%rbx)
xor    %eax,%eax
jmp    11b1 <HSMMCSDBusFreqSet+0x91>

LLVM IR (-O0):
define i32 @HSMMCSDBusFreqSet(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store volatile i32 0, i32* %10
store volatile i32 0, i32* %11
%12 = load i32, i32* %6
%13 = call i32 @HSMMCSDIntClock(i32 %12, i32 1)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 -1, i32* %5
br label %77
16: ; preds = %4
%17 = load i32, i32* %9
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %76
19: ; preds = %16
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = udiv i32 %20, %21
store volatile i32 %22, i32* %10
%23 = load volatile i32, i32* %10
%24 = icmp ult i32 %23, 2
br i1 %24, label %25, label %26
25: ; preds = %19
br label %28
26: ; preds = %19
%27 = load volatile i32, i32* %10
br label %28
28: ; preds = %26, %25
%29 = phi i32 [ 2, %25 ], [ %27, %26 ]
store volatile i32 %29, i32* %10
%30 = load volatile i32, i32* %10
%31 = icmp ugt i32 %30, 1023
br i1 %31, label %32, label %33
32: ; preds = %28
br label %35
33: ; preds = %28
%34 = load volatile i32, i32* %10
br label %35
35: ; preds = %33, %32
%36 = phi i32 [ 1023, %32 ], [ %34, %33 ]
store volatile i32 %36, i32* %10
br label %37
37: ; preds = %47, %35
%38 = load i32, i32* %7
%39 = load volatile i32, i32* %10
%40 = udiv i32 %38, %39
%41 = load i32, i32* %8
%42 = icmp ugt i32 %40, %41
br i1 %42, label %43, label %50
43: ; preds = %37
%44 = load volatile i32, i32* %10
%45 = icmp eq i32 %44, 1023
br i1 %45, label %46, label %47
46: ; preds = %43
store i32 -1, i32* %5
br label %77
47: ; preds = %43
%48 = load volatile i32, i32* %10
%49 = add i32 %48, 1
store volatile i32 %49, i32* %10
br label %37
50: ; preds = %37
%51 = load i32, i32* %6
%52 = add i32 %51, 556
%53 = zext i32 %52 to i64
%54 = inttoptr i64 %53 to i32*
%55 = load volatile i32, i32* %54
%56 = and i32 %55, -65473
store volatile i32 %56, i32* %11
%57 = load volatile i32, i32* %11
%58 = load volatile i32, i32* %10
%59 = shl i32 %58, 6
%60 = or i32 %57, %59
%61 = load i32, i32* %6
%62 = add i32 %61, 556
%63 = zext i32 %62 to i64
%64 = inttoptr i64 %63 to i32*
store volatile i32 %60, i32* %64
%65 = load i32, i32* %6
%66 = call i64 @HSMMCSDIsIntClockStable(i32 %65, i32 65535)
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %50
store i32 -1, i32* %5
br label %77
69: ; preds = %50
%70 = load i32, i32* %6
%71 = add i32 %70, 556
%72 = zext i32 %71 to i64
%73 = inttoptr i64 %72 to i32*
%74 = load volatile i32, i32* %73
%75 = or i32 %74, 4
store volatile i32 %75, i32* %73
br label %76
76: ; preds = %69, %16
store i32 0, i32* %5
br label %77
77: ; preds = %76, %68, %46, %15
%78 = load i32, i32* %5
ret i32 %78
}

LLVM IR (-O2):
define i32 @HSMMCSDBusFreqSet(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %5 to i8*
store volatile i32 0, i32* %5
%8 = bitcast i32* %6 to i8*
store volatile i32 0, i32* %6
%9 = tail call i32 @HSMMCSDIntClock(i32 %0, i32 1)
%10 = icmp eq i32 %9, -1
br i1 %10, label %54, label %11
11: ; preds = %4
%12 = icmp eq i32 %3, 0
br i1 %12, label %13, label %54
13: ; preds = %11
%14 = udiv i32 %1, %2
store volatile i32 %14, i32* %5
%15 = load volatile i32, i32* %5
%16 = icmp ult i32 %15, 2
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = load volatile i32, i32* %5
br label %19
19: ; preds = %13, %17
%20 = phi i32 [ %18, %17 ], [ 2, %13 ]
store volatile i32 %20, i32* %5
%21 = load volatile i32, i32* %5
%22 = icmp ugt i32 %21, 1023
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = load volatile i32, i32* %5
br label %25
25: ; preds = %19, %23
%26 = phi i32 [ %24, %23 ], [ 1023, %19 ]
store volatile i32 %26, i32* %5
%27 = load volatile i32, i32* %5
%28 = udiv i32 %1, %27
%29 = icmp ugt i32 %28, %2
br i1 %29, label %30, label %39
30: ; preds = %25, %33
%31 = load volatile i32, i32* %5
%32 = icmp eq i32 %31, 1023
br i1 %32, label %54, label %33
33: ; preds = %30
%34 = load volatile i32, i32* %5
%35 = add i32 %34, 1
store volatile i32 %35, i32* %5
%36 = load volatile i32, i32* %5
%37 = udiv i32 %1, %36
%38 = icmp ugt i32 %37, %2
br i1 %38, label %30, label %39
39: ; preds = %33, %25
%40 = add i32 %0, 556
%41 = zext i32 %40 to i64
%42 = inttoptr i64 %41 to i32*
%43 = load volatile i32, i32* %42
%44 = and i32 %43, -65473
store volatile i32 %44, i32* %6
%45 = load volatile i32, i32* %6
%46 = load volatile i32, i32* %5
%47 = shl i32 %46, 6
%48 = or i32 %47, %45
store volatile i32 %48, i32* %42
%49 = tail call i64 @HSMMCSDIsIntClockStable(i32 %0, i32 65535)
%50 = icmp eq i64 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %39
%52 = load volatile i32, i32* %42
%53 = or i32 %52, 4
store volatile i32 %53, i32* %42
br label %54
54: ; preds = %30, %11, %51, %39, %4
%55 = phi i32 [ -1, %4 ], [ -1, %39 ], [ 0, %51 ], [ 0, %11 ], [ -1, %30 ]
ret i32 %55
}