#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 */
unsigned short __builtin_inw(int);
int /*<<< orphan*/ __builtin_outb(int, int);
void spd_set_memclk(void) {
  unsigned min_cycle_time;
  unsigned device;
  int new_cycle_time, new_latency;
  int index;
  int latency;
  min_cycle_time = 0x50;
  device = 0x50;
  new_cycle_time = 0xa0;
  new_latency = 5;
  latency = 0;
  for (index = 0; index < 3; index++, latency++) {
    unsigned long loops;
    loops = 1000000;
    do {
      unsigned short val;
      val = __builtin_inw(0x10e0);
    } while (--loops);
    if (!loops) {
      continue;
    }
    __builtin_outb(device, 0xe4);
    __builtin_outb(index, 0xe8);
    loops = 1000000;
    while (--loops)
      ;
  }
  if (new_latency > 4) {
    return;
  }
  if (new_cycle_time > min_cycle_time) {
    min_cycle_time = new_cycle_time;
  }
}


x86 assembly (clang, -O0):
<spd_set_memclk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x50,-0x4(%rbp)
movl   $0x50,-0x8(%rbp)
movl   $0xa0,-0xc(%rbp)
movl   $0x5,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jge    11f1 <spd_set_memclk+0xd1>
movq   $0xf4240,-0x20(%rbp)
mov    $0x10e0,%edi
call   1030 <__builtin_inw@plt>
mov    %ax,-0x22(%rbp)
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1164 <spd_set_memclk+0x44>
cmpq   $0x0,-0x20(%rbp)
jne    1198 <spd_set_memclk+0x78>
jmp    11da <spd_set_memclk+0xba>
mov    -0x8(%rbp),%edi
mov    $0xe4,%esi
call   1040 <__builtin_outb@plt>
mov    -0x14(%rbp),%edi
mov    $0xe8,%esi
call   1040 <__builtin_outb@plt>
movq   $0xf4240,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11d5 <spd_set_memclk+0xb5>
jmp    11ba <spd_set_memclk+0x9a>
jmp    11da <spd_set_memclk+0xba>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1152 <spd_set_memclk+0x32>
cmpl   $0x4,-0x10(%rbp)
jle    1200 <spd_set_memclk+0xe0>
jmp    1212 <spd_set_memclk+0xf2>
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jbe    1212 <spd_set_memclk+0xf2>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spd_set_memclk>:
push   %rbx
mov    $0xfffffffffff0bdc0,%rbx
nopl   0x0(%rax,%rax,1)
mov    $0x10e0,%edi
call   1030 <__builtin_inw@plt>
inc    %rbx
jne    1120 <spd_set_memclk+0x10>
mov    $0xfffffffffff0bdc0,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    $0x10e0,%edi
call   1030 <__builtin_inw@plt>
inc    %rbx
jne    1140 <spd_set_memclk+0x30>
mov    $0xfffffffffff0bdc0,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    $0x10e0,%edi
call   1030 <__builtin_inw@plt>
inc    %rbx
jne    1160 <spd_set_memclk+0x50>
pop    %rbx
ret

LLVM IR (-O0):
define void @spd_set_memclk() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i16
store i32 80, i32* %1
store i32 80, i32* %2
store i32 160, i32* %3
store i32 5, i32* %4
store i32 0, i32* %6
store i32 0, i32* %5
br label %9
9: ; preds = %34, %0
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %39
12: ; preds = %9
store i64 1000000, i64* %7
br label %13
13: ; preds = %15, %12
%14 = call zeroext i16 @__builtin_inw(i32 4320)
store i16 %14, i16* %8
br label %15
15: ; preds = %13
%16 = load i64, i64* %7
%17 = add i64 %16, -1
store i64 %17, i64* %7
%18 = icmp ne i64 %17, 0
br i1 %18, label %13, label %19
19: ; preds = %15
%20 = load i64, i64* %7
%21 = icmp ne i64 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %19
br label %34
23: ; preds = %19
%24 = load i32, i32* %2
%25 = call i32 @__builtin_outb(i32 %24, i32 228)
%26 = load i32, i32* %5
%27 = call i32 @__builtin_outb(i32 %26, i32 232)
store i64 1000000, i64* %7
br label %28
28: ; preds = %32, %23
%29 = load i64, i64* %7
%30 = add i64 %29, -1
store i64 %30, i64* %7
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
br label %28
33: ; preds = %28
br label %34
34: ; preds = %33, %22
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %5
%37 = load i32, i32* %6
%38 = add nsw i32 %37, 1
store i32 %38, i32* %6
br label %9
39: ; preds = %9
%40 = load i32, i32* %4
%41 = icmp sgt i32 %40, 4
br i1 %41, label %42, label %43
42: ; preds = %39
br label %49
43: ; preds = %39
%44 = load i32, i32* %3
%45 = load i32, i32* %1
%46 = icmp ugt i32 %44, %45
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = load i32, i32* %3
store i32 %48, i32* %1
br label %49
49: ; preds = %42, %47, %43
ret void
}

LLVM IR (-O2):
define void @spd_set_memclk() {
br label %1
1: ; preds = %0, %1
%2 = phi i64 [ %4, %1 ], [ 1000000, %0 ]
%3 = tail call zeroext i16 @__builtin_inw(i32 4320)
%4 = add nsw i64 %2, -1
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %1
6: ; preds = %1, %6
%7 = phi i64 [ %9, %6 ], [ 1000000, %1 ]
%8 = tail call zeroext i16 @__builtin_inw(i32 4320)
%9 = add nsw i64 %7, -1
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %6
11: ; preds = %6, %11
%12 = phi i64 [ %14, %11 ], [ 1000000, %6 ]
%13 = tail call zeroext i16 @__builtin_inw(i32 4320)
%14 = add nsw i64 %12, -1
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %11
16: ; preds = %11
ret void
}