#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 /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sscanf(char *, char *, unsigned char *);
int main(int argc, char **argv) {
  unsigned char buffer[4];
  unsigned int i, div, r, pe, c, re, rts, p;
  if (argc != 5) {
    printf("usage: showfreq xx xx xx xx\n");
    return 0;
  }
  for (i = 0; i < 4; i++)
    sscanf(argv[i + 1], "%hhx", buffer + i);
  div = buffer[1] | ((buffer[0] & 0x7F) << 8) | (((buffer[2] >> 5) & 3) << 15);
  r = buffer[2] & 15;
  pe = (buffer[2] >> 4) & 1;
  c = buffer[3] >> 6;
  re = !!(buffer[3] & 32);
  rts = !!(buffer[3] & 16);
  p = buffer[3] & 15;
  printf("div: %d\n", div);
  printf("r=%d pe=%d c=%d re=%d rts=%d p=%d\n", r, pe, c, re, rts, p);
  div *= 62500;
  div -= 36125000;
  div /= 1;
  printf("freq: %d Hz (%d.%03dMHz)\n", div, div / (1000 * 1000),
         (div / 1000) % 1000);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x5,-0x8(%rbp)
je     115a <main+0x3a>
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b3 <main+0x193>
movl   $0x0,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jae    119f <main+0x7f>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
mov    (%rax,%rcx,8),%rdi
lea    -0x14(%rbp),%rdx
mov    -0x18(%rbp),%eax
add    %rax,%rdx
lea    0xe91(%rip),%rsi
call   1040 <sscanf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1161 <main+0x41>
movzbl -0x13(%rbp),%eax
movzbl -0x14(%rbp),%ecx
and    $0x7f,%ecx
shl    $0x8,%ecx
or     %ecx,%eax
movzbl -0x12(%rbp),%ecx
sar    $0x5,%ecx
and    $0x3,%ecx
shl    $0xf,%ecx
or     %ecx,%eax
mov    %eax,-0x1c(%rbp)
movzbl -0x12(%rbp),%eax
and    $0xf,%eax
mov    %eax,-0x20(%rbp)
movzbl -0x12(%rbp),%eax
sar    $0x4,%eax
and    $0x1,%eax
mov    %eax,-0x24(%rbp)
movzbl -0x11(%rbp),%eax
sar    $0x6,%eax
mov    %eax,-0x28(%rbp)
movzbl -0x11(%rbp),%eax
and    $0x20,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
movzbl -0x11(%rbp),%eax
and    $0x10,%eax
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x30(%rbp)
movzbl -0x11(%rbp),%eax
and    $0xf,%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    -0x2c(%rbp),%r8d
mov    -0x30(%rbp),%r9d
mov    -0x34(%rbp),%eax
lea    0xde1(%rip),%rdi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1030 <printf@plt>
imul   $0xf424,-0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x2273948,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
shr    $0x0,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%eax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,%ecx
mov    -0x38(%rbp),%edx
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x5,%edi
jne    1266 <main+0x136>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xeab(%rip),%rbp
lea    0x10(%rsp),%rdx
mov    %rbp,%rsi
call   1050 <sscanf@plt>
mov    0x10(%rbx),%rdi
lea    0x11(%rsp),%rdx
mov    %rbp,%rsi
call   1050 <sscanf@plt>
mov    0x18(%rbx),%rdi
lea    0x12(%rsp),%rdx
mov    %rbp,%rsi
call   1050 <sscanf@plt>
mov    0x20(%rbx),%rdi
lea    0x13(%rsp),%rdx
mov    %rbp,%rsi
call   1050 <sscanf@plt>
movzbl 0x11(%rsp),%eax
movzbl 0x10(%rsp),%ecx
and    $0x7f,%ecx
shl    $0x8,%ecx
or     %eax,%ecx
movzbl 0x12(%rsp),%r13d
mov    %r13d,%ebx
and    $0x60,%ebx
shl    $0xa,%ebx
or     %ecx,%ebx
mov    %r13d,%eax
and    $0xf,%eax
mov    %eax,0x14(%rsp)
shr    $0x4,%r13d
and    $0x1,%r13d
movzbl 0x13(%rsp),%r15d
mov    %r15d,%r14d
shr    $0x6,%r14d
mov    %r15d,%r12d
shr    $0x5,%r12d
and    $0x1,%r12d
mov    %r15d,%ebp
shr    $0x4,%ebp
and    $0x1,%ebp
and    $0xf,%r15d
lea    0xe0f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15d,(%rsp)
lea    0xe04(%rip),%rdi
mov    0x14(%rsp),%esi
mov    %r13d,%edx
mov    %r14d,%ecx
mov    %r12d,%r8d
mov    %ebp,%r9d
xor    %eax,%eax
call   1040 <printf@plt>
imul   $0xf424,%ebx,%esi
add    $0xfdd8c6b8,%esi
imul   $0x431bde83,%rsi,%rdx
shr    $0x32,%rdx
imul   $0x10624dd3,%rsi,%rcx
shr    $0x26,%rcx
imul   $0x10624dd3,%rcx,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%eax
sub    %eax,%ecx
lea    0xdd4(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1272 <main+0x142>
lea    0xdde(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"usage: showfreq xx xx xx xx\0A\00"
@.str.1 = [5 x i8] c"%hhx\00"
@.str.2 = [9 x i8] c"div: %d\0A\00"
@.str.3 = [35 x i8] c"r=%d pe=%d c=%d re=%d rts=%d p=%d\0A\00"
@.str.4 = [26 x i8] c"freq: %d Hz (%d.%03dMHz)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [4 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 5
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %110
19: ; preds = %2
store i32 0, i32* %7
br label %20
20: ; preds = %35, %19
%21 = load i32, i32* %7
%22 = icmp ult i32 %21, 4
br i1 %22, label %23, label %38
23: ; preds = %20
%24 = load i8**, i8*** %5
%25 = load i32, i32* %7
%26 = add i32 %25, 1
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8*, i8** %24, i64 %27
%29 = load i8*, i8** %28
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%31 = load i32, i32* %7
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = call i32 @sscanf(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %23
%36 = load i32, i32* %7
%37 = add i32 %36, 1
store i32 %37, i32* %7
br label %20
38: ; preds = %20
%39 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 1
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = and i32 %44, 127
%46 = shl i32 %45, 8
%47 = or i32 %41, %46
%48 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 2
%49 = load i8, i8* %48
%50 = zext i8 %49 to i32
%51 = ashr i32 %50, 5
%52 = and i32 %51, 3
%53 = shl i32 %52, 15
%54 = or i32 %47, %53
store i32 %54, i32* %8
%55 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 2
%56 = load i8, i8* %55
%57 = zext i8 %56 to i32
%58 = and i32 %57, 15
store i32 %58, i32* %9
%59 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 2
%60 = load i8, i8* %59
%61 = zext i8 %60 to i32
%62 = ashr i32 %61, 4
%63 = and i32 %62, 1
store i32 %63, i32* %10
%64 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
%65 = load i8, i8* %64
%66 = zext i8 %65 to i32
%67 = ashr i32 %66, 6
store i32 %67, i32* %11
%68 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
%69 = load i8, i8* %68
%70 = zext i8 %69 to i32
%71 = and i32 %70, 32
%72 = icmp ne i32 %71, 0
%73 = xor i1 %72, true
%74 = xor i1 %73, true
%75 = zext i1 %74 to i32
store i32 %75, i32* %12
%76 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
%77 = load i8, i8* %76
%78 = zext i8 %77 to i32
%79 = and i32 %78, 16
%80 = icmp ne i32 %79, 0
%81 = xor i1 %80, true
%82 = xor i1 %81, true
%83 = zext i1 %82 to i32
store i32 %83, i32* %13
%84 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
%85 = load i8, i8* %84
%86 = zext i8 %85 to i32
%87 = and i32 %86, 15
store i32 %87, i32* %14
%88 = load i32, i32* %8
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %88)
%90 = load i32, i32* %9
%91 = load i32, i32* %10
%92 = load i32, i32* %11
%93 = load i32, i32* %12
%94 = load i32, i32* %13
%95 = load i32, i32* %14
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %90, i32 %91, i32 %92, i32 %93, i32 %94, i32 %95)
%97 = load i32, i32* %8
%98 = mul i32 %97, 62500
store i32 %98, i32* %8
%99 = load i32, i32* %8
%100 = sub i32 %99, 36125000
store i32 %100, i32* %8
%101 = load i32, i32* %8
%102 = udiv i32 %101, 1
store i32 %102, i32* %8
%103 = load i32, i32* %8
%104 = load i32, i32* %8
%105 = udiv i32 %104, 1000000
%106 = load i32, i32* %8
%107 = udiv i32 %106, 1000
%108 = urem i32 %107, 1000
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %103, i32 %105, i32 %108)
store i32 0, i32* %3
br label %110
110: ; preds = %38, %17
%111 = load i32, i32* %3
ret i32 %111
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"%hhx\00"
@.str.2 = [9 x i8] c"div: %d\0A\00"
@.str.3 = [35 x i8] c"r=%d pe=%d c=%d re=%d rts=%d p=%d\0A\00"
@.str.4 = [26 x i8] c"freq: %d Hz (%d.%03dMHz)\0A\00"
@str = [28 x i8] c"usage: showfreq xx xx xx xx\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 5
br i1 %5, label %6, label %53
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%13 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = getelementptr inbounds i8*, i8** %1, i64 3
%15 = load i8*, i8** %14
%16 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%17 = call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds i8*, i8** %1, i64 4
%19 = load i8*, i8** %18
%20 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
%21 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i8, i8* %12
%23 = zext i8 %22 to i32
%24 = load i8, i8* %4
%25 = and i8 %24, 127
%26 = zext i8 %25 to i32
%27 = shl nuw nsw i32 %26, 8
%28 = or i32 %27, %23
%29 = load i8, i8* %16
%30 = zext i8 %29 to i32
%31 = shl nuw nsw i32 %30, 10
%32 = and i32 %31, 98304
%33 = or i32 %28, %32
%34 = and i32 %30, 15
%35 = lshr i32 %30, 4
%36 = and i32 %35, 1
%37 = load i8, i8* %20
%38 = zext i8 %37 to i32
%39 = lshr i32 %38, 6
%40 = lshr i32 %38, 5
%41 = and i32 %40, 1
%42 = lshr i32 %38, 4
%43 = and i32 %42, 1
%44 = and i32 %38, 15
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %34, i32 %36, i32 %39, i32 %41, i32 %43, i32 %44)
%47 = mul i32 %33, 62500
%48 = add i32 %47, -36125000
%49 = udiv i32 %48, 1000000
%50 = udiv i32 %48, 1000
%51 = urem i32 %50, 1000
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %48, i32 %49, i32 %51)
br label %55
53: ; preds = %2
%54 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %55
55: ; preds = %6, %53
ret i32 0
}