#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 */
scalar_t__ log2(unsigned long);
int /*<<< orphan*/ pcibios_write_config_byte(int /*<<< orphan*/, scalar_t__,
                                             unsigned int, unsigned int);
int smbus_read_byte(unsigned int, int);
void spd_set_drb(void) {
  unsigned end_of_memory;
  unsigned device;
  unsigned drb_reg;
  end_of_memory = 0;
  device = 0x50;
  drb_reg = 0x60;
  while (device <= 0x53) {
    unsigned side1_bits, side2_bits;
    int byte, byte2;
    side1_bits = side2_bits = -1;
    byte = smbus_read_byte(device, 3);
    if (byte >= 0) {
      side1_bits += byte & 0xf;
      byte = smbus_read_byte(device, 4);
      side1_bits += byte & 0xf;
      byte = smbus_read_byte(device, 17);
      side1_bits += log2(byte);
      byte = smbus_read_byte(device, 6);
      byte2 = smbus_read_byte(device, 7);
      side1_bits += log2((((unsigned long)byte2 << 8) | byte));
      side1_bits -= 25;
      byte = smbus_read_byte(device, 5);
      if (byte > 1) {
        side2_bits = side1_bits;
      }
    }
    if (side1_bits < 8) {
      end_of_memory += (1 << side1_bits);
    }
    pcibios_write_config_byte(0, ((0x00 << 3) + 0), drb_reg, end_of_memory);
    if (side2_bits < 8) {
      end_of_memory += (1 << side2_bits);
    }
    pcibios_write_config_byte(0, ((0x00 << 3) + 0), drb_reg + 1, end_of_memory);
    drb_reg += 2;
    device += 1;
  }
}


x86 assembly (clang, -O0):
<spd_set_drb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x50,-0x8(%rbp)
movl   $0x60,-0xc(%rbp)
cmpl   $0x53,-0x8(%rbp)
ja     12aa <spd_set_drb+0x17a>
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x3,%esi
call   1050 <smbus_read_byte@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     123a <spd_set_drb+0x10a>
mov    -0x18(%rbp),%eax
and    $0xf,%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x4,%esi
call   1050 <smbus_read_byte@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
and    $0xf,%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x11,%esi
call   1050 <smbus_read_byte@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rdi
call   1030 <log2@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x6,%esi
call   1050 <smbus_read_byte@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x7,%esi
call   1050 <smbus_read_byte@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rdi
shl    $0x8,%rdi
movslq -0x18(%rbp),%rax
or     %rax,%rdi
call   1030 <log2@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sub    $0x19,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1050 <smbus_read_byte@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jle    1235 <spd_set_drb+0x105>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    123a <spd_set_drb+0x10a>
cmpl   $0x8,-0x10(%rbp)
jae    1254 <spd_set_drb+0x124>
mov    -0x10(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
add    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0xc(%rbp),%edx
mov    -0x4(%rbp),%ecx
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <pcibios_write_config_byte@plt>
cmpl   $0x8,-0x14(%rbp)
jae    127f <spd_set_drb+0x14f>
mov    -0x14(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
add    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0xc(%rbp),%edx
add    $0x1,%edx
mov    -0x4(%rbp),%ecx
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <pcibios_write_config_byte@plt>
mov    -0xc(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    114d <spd_set_drb+0x1d>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spd_set_drb>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x50,%r13d
mov    $0x60,%r14d
xor    %r15d,%r15d
jmp    118a <spd_set_drb+0x5a>
nopl   0x0(%rax)
mov    %r15d,%ebp
xor    %ebx,%ebx
xor    %edi,%edi
xor    %esi,%esi
mov    %r14d,%edx
mov    %r15d,%ecx
call   1040 <pcibios_write_config_byte@plt>
add    %ebx,%ebp
lea    0x1(%r14),%edx
xor    %edi,%edi
xor    %esi,%esi
mov    %ebp,%ecx
call   1040 <pcibios_write_config_byte@plt>
add    $0x2,%r14d
add    $0x1,%r13d
mov    %ebp,%r15d
cmp    $0x68,%r14d
je     1268 <spd_set_drb+0x138>
mov    %r13d,%edi
mov    $0x3,%esi
call   1050 <smbus_read_byte@plt>
test   %eax,%eax
js     1150 <spd_set_drb+0x20>
mov    %eax,%ebp
and    $0xf,%ebp
mov    %r13d,%edi
mov    $0x4,%esi
call   1050 <smbus_read_byte@plt>
mov    %eax,%r12d
and    $0xf,%r12d
add    %ebp,%r12d
mov    %r13d,%edi
mov    $0x11,%esi
call   1050 <smbus_read_byte@plt>
movslq %eax,%rdi
call   1030 <log2@plt>
mov    %rax,%rbp
mov    %r13d,%edi
mov    $0x6,%esi
call   1050 <smbus_read_byte@plt>
mov    %eax,%ebx
mov    %r13d,%edi
mov    $0x7,%esi
call   1050 <smbus_read_byte@plt>
cltq
shl    $0x8,%rax
movslq %ebx,%rdi
or     %rax,%rdi
call   1030 <log2@plt>
add    %r12d,%ebp
lea    (%rax,%rbp,1),%r12d
add    $0xffffffe6,%r12d
mov    %r13d,%edi
mov    $0x5,%esi
call   1050 <smbus_read_byte@plt>
mov    $0x1,%ebp
mov    %r12d,%ecx
shl    %cl,%ebp
cmp    $0x8,%r12d
mov    $0x0,%ecx
cmovae %ecx,%ebp
add    %r15d,%ebp
cmp    $0x1,%eax
jle    1260 <spd_set_drb+0x130>
xor    %edi,%edi
xor    %esi,%esi
mov    %r14d,%edx
xor    %r15d,%r15d
mov    %ebp,%ecx
call   1040 <pcibios_write_config_byte@plt>
mov    $0x1,%ebx
mov    %r12d,%ecx
shl    %cl,%ebx
cmp    $0x8,%r12d
cmovae %r15d,%ebx
jmp    1164 <spd_set_drb+0x34>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%r15d
jmp    1153 <spd_set_drb+0x23>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @spd_set_drb() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i32 80, i32* %2
store i32 96, i32* %3
br label %8
8: ; preds = %79, %0
%9 = load i32, i32* %2
%10 = icmp ule i32 %9, 83
br i1 %10, label %11, label %88
11: ; preds = %8
store i32 -1, i32* %5
store i32 -1, i32* %4
%12 = load i32, i32* %2
%13 = call i32 @smbus_read_byte(i32 %12, i32 3)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp sge i32 %14, 0
br i1 %15, label %16, label %60
16: ; preds = %11
%17 = load i32, i32* %6
%18 = and i32 %17, 15
%19 = load i32, i32* %4
%20 = add i32 %19, %18
store i32 %20, i32* %4
%21 = load i32, i32* %2
%22 = call i32 @smbus_read_byte(i32 %21, i32 4)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = and i32 %23, 15
%25 = load i32, i32* %4
%26 = add i32 %25, %24
store i32 %26, i32* %4
%27 = load i32, i32* %2
%28 = call i32 @smbus_read_byte(i32 %27, i32 17)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = call i64 @log2(i64 %30)
%32 = load i32, i32* %4
%33 = zext i32 %32 to i64
%34 = add nsw i64 %33, %31
%35 = trunc i64 %34 to i32
store i32 %35, i32* %4
%36 = load i32, i32* %2
%37 = call i32 @smbus_read_byte(i32 %36, i32 6)
store i32 %37, i32* %6
%38 = load i32, i32* %2
%39 = call i32 @smbus_read_byte(i32 %38, i32 7)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = shl i64 %41, 8
%43 = load i32, i32* %6
%44 = sext i32 %43 to i64
%45 = or i64 %42, %44
%46 = call i64 @log2(i64 %45)
%47 = load i32, i32* %4
%48 = zext i32 %47 to i64
%49 = add nsw i64 %48, %46
%50 = trunc i64 %49 to i32
store i32 %50, i32* %4
%51 = load i32, i32* %4
%52 = sub i32 %51, 25
store i32 %52, i32* %4
%53 = load i32, i32* %2
%54 = call i32 @smbus_read_byte(i32 %53, i32 5)
store i32 %54, i32* %6
%55 = load i32, i32* %6
%56 = icmp sgt i32 %55, 1
br i1 %56, label %57, label %59
57: ; preds = %16
%58 = load i32, i32* %4
store i32 %58, i32* %5
br label %59
59: ; preds = %57, %16
br label %60
60: ; preds = %59, %11
%61 = load i32, i32* %4
%62 = icmp ult i32 %61, 8
br i1 %62, label %63, label %68
63: ; preds = %60
%64 = load i32, i32* %4
%65 = shl i32 1, %64
%66 = load i32, i32* %1
%67 = add i32 %66, %65
store i32 %67, i32* %1
br label %68
68: ; preds = %63, %60
%69 = load i32, i32* %3
%70 = load i32, i32* %1
%71 = call i32 @pcibios_write_config_byte(i32 0, i64 0, i32 %69, i32 %70)
%72 = load i32, i32* %5
%73 = icmp ult i32 %72, 8
br i1 %73, label %74, label %79
74: ; preds = %68
%75 = load i32, i32* %5
%76 = shl i32 1, %75
%77 = load i32, i32* %1
%78 = add i32 %77, %76
store i32 %78, i32* %1
br label %79
79: ; preds = %74, %68
%80 = load i32, i32* %3
%81 = add i32 %80, 1
%82 = load i32, i32* %1
%83 = call i32 @pcibios_write_config_byte(i32 0, i64 0, i32 %81, i32 %82)
%84 = load i32, i32* %3
%85 = add i32 %84, 2
store i32 %85, i32* %3
%86 = load i32, i32* %2
%87 = add i32 %86, 1
store i32 %87, i32* %2
br label %8
88: ; preds = %8
ret void
}

LLVM IR (-O2):
define void @spd_set_drb() {
br label %1
1: ; preds = %0, %41
%2 = phi i32 [ 0, %0 ], [ %44, %41 ]
%3 = phi i32 [ 96, %0 ], [ %47, %41 ]
%4 = phi i32 [ 80, %0 ], [ %48, %41 ]
%5 = tail call i32 @smbus_read_byte(i32 %4, i32 3)
%6 = icmp sgt i32 %5, -1
br i1 %6, label %7, label %33
7: ; preds = %1
%8 = and i32 %5, 15
%9 = tail call i32 @smbus_read_byte(i32 %4, i32 4)
%10 = and i32 %9, 15
%11 = tail call i32 @smbus_read_byte(i32 %4, i32 17)
%12 = sext i32 %11 to i64
%13 = tail call i64 @log2(i64 %12)
%14 = trunc i64 %13 to i32
%15 = tail call i32 @smbus_read_byte(i32 %4, i32 6)
%16 = tail call i32 @smbus_read_byte(i32 %4, i32 7)
%17 = sext i32 %16 to i64
%18 = shl nsw i64 %17, 8
%19 = sext i32 %15 to i64
%20 = or i64 %18, %19
%21 = tail call i64 @log2(i64 %20)
%22 = trunc i64 %21 to i32
%23 = add nuw nsw i32 %8, -26
%24 = add nsw i32 %23, %10
%25 = add i32 %24, %14
%26 = add i32 %25, %22
%27 = tail call i32 @smbus_read_byte(i32 %4, i32 5)
%28 = icmp sgt i32 %27, 1
%29 = icmp ult i32 %26, 8
%30 = shl nuw i32 1, %26
%31 = select i1 %29, i32 %30, i32 0
%32 = add i32 %31, %2
br i1 %28, label %36, label %33
33: ; preds = %1, %7
%34 = phi i32 [ %32, %7 ], [ %2, %1 ]
%35 = tail call i32 @pcibios_write_config_byte(i32 0, i64 0, i32 %3, i32 %34)
br label %41
36: ; preds = %7
%37 = tail call i32 @pcibios_write_config_byte(i32 0, i64 0, i32 %3, i32 %32)
%38 = icmp ult i32 %26, 8
%39 = shl nuw i32 1, %26
%40 = select i1 %38, i32 %39, i32 0
br label %41
41: ; preds = %36, %33
%42 = phi i32 [ %34, %33 ], [ %32, %36 ]
%43 = phi i32 [ 0, %33 ], [ %40, %36 ]
%44 = add i32 %43, %42
%45 = or i32 %3, 1
%46 = tail call i32 @pcibios_write_config_byte(i32 0, i64 0, i32 %45, i32 %44)
%47 = add nuw nsw i32 %3, 2
%48 = add nuw nsw i32 %4, 1
%49 = icmp eq i32 %48, 84
br i1 %49, label %50, label %1
50: ; preds = %41
ret void
}