#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 char, unsigned int);
int /*<<< orphan*/ print_debug(char *);
int /*<<< orphan*/ print_debug_hex32(unsigned int);
int smbus_read_byte(unsigned char, int);
void spd_set_drb(void) {
  unsigned end_of_memory;
  unsigned char device;
  unsigned char drb_reg;
  end_of_memory = 0;
  device = 0x50;
  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);
    }
    drb_reg = ((device - 0x50) << 1) + 0x60;
    print_debug("end_of_memory: ");
    print_debug_hex32(end_of_memory);
    print_debug("\n");
    pcibios_write_config_byte(0, ((0x00 << 3) + 0), drb_reg, end_of_memory);
    if (side2_bits < 8) {
      end_of_memory += (1 << side2_bits);
    }
    print_debug("end_of_memory: ");
    print_debug_hex32(end_of_memory);
    print_debug("\n");
    pcibios_write_config_byte(0, ((0x00 << 3) + 0), drb_reg + 1, end_of_memory);
    device += 1;
  }
}


x86 assembly (clang, -O0):
<spd_set_drb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x50,-0x5(%rbp)
movzbl -0x5(%rbp),%eax
cmp    $0x53,%eax
jg     1318 <spd_set_drb+0x1c8>
movl   $0xffffffff,-0x10(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    $0x3,%esi
movzbl -0x5(%rbp),%edi
call   1060 <smbus_read_byte@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1259 <spd_set_drb+0x109>
mov    -0x14(%rbp),%eax
and    $0xf,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x4,%esi
movzbl -0x5(%rbp),%edi
call   1060 <smbus_read_byte@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
and    $0xf,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    $0x11,%esi
movzbl -0x5(%rbp),%edi
call   1060 <smbus_read_byte@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rdi
call   1030 <log2@plt>
mov    %rax,%rcx
mov    -0xc(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0xc(%rbp)
mov    $0x6,%esi
movzbl -0x5(%rbp),%edi
call   1060 <smbus_read_byte@plt>
mov    %eax,-0x14(%rbp)
mov    $0x7,%esi
movzbl -0x5(%rbp),%edi
call   1060 <smbus_read_byte@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rdi
shl    $0x8,%rdi
movslq -0x14(%rbp),%rax
or     %rax,%rdi
call   1030 <log2@plt>
mov    %rax,%rcx
mov    -0xc(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x19,%eax
mov    %eax,-0xc(%rbp)
mov    $0x5,%esi
movzbl -0x5(%rbp),%edi
call   1060 <smbus_read_byte@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jle    1254 <spd_set_drb+0x104>
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1259 <spd_set_drb+0x109>
cmpl   $0x8,-0xc(%rbp)
jae    1273 <spd_set_drb+0x123>
mov    -0xc(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
add    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
movzbl -0x5(%rbp),%eax
sub    $0x50,%eax
shl    $0x1,%eax
add    $0x60,%eax
mov    %al,-0x6(%rbp)
lea    0xd76(%rip),%rdi
call   1070 <print_debug@plt>
mov    -0x4(%rbp),%edi
call   1050 <print_debug_hex32@plt>
lea    0xd72(%rip),%rdi
call   1070 <print_debug@plt>
mov    -0x6(%rbp),%al
mov    -0x4(%rbp),%ecx
xor    %edi,%edi
xor    %edx,%edx
mov    %edx,%esi
movzbl %al,%edx
call   1040 <pcibios_write_config_byte@plt>
cmpl   $0x8,-0x10(%rbp)
jae    12d1 <spd_set_drb+0x181>
mov    -0x10(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
add    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
lea    0xd28(%rip),%rdi
call   1070 <print_debug@plt>
mov    -0x4(%rbp),%edi
call   1050 <print_debug_hex32@plt>
lea    0xd24(%rip),%rdi
call   1070 <print_debug@plt>
movzbl -0x6(%rbp),%eax
add    $0x1,%eax
mov    -0x4(%rbp),%ecx
xor    %edi,%edi
xor    %edx,%edx
mov    %edx,%esi
movzbl %al,%edx
call   1040 <pcibios_write_config_byte@plt>
movzbl -0x5(%rbp),%eax
add    $0x1,%eax
mov    %al,-0x5(%rbp)
jmp    1163 <spd_set_drb+0x13>
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,%r14b
xor    %r13d,%r13d
mov    $0x61,%r15b
jmp    11b1 <spd_set_drb+0x61>
cs nopw 0x0(%rax,%rax,1)
add    %eax,%r13d
mov    %rbp,%rdi
call   1070 <print_debug@plt>
mov    %r13d,%edi
call   1050 <print_debug_hex32@plt>
lea    0xe86(%rip),%rdi
call   1070 <print_debug@plt>
movzbl %r15b,%edx
xor    %edi,%edi
xor    %esi,%esi
mov    %r13d,%ecx
call   1040 <pcibios_write_config_byte@plt>
add    $0x1,%r14b
add    $0x2,%r15b
cmp    $0x69,%r15b
je     12d7 <spd_set_drb+0x187>
movzbl %r14b,%ebp
mov    %ebp,%edi
mov    $0x3,%esi
call   1060 <smbus_read_byte@plt>
test   %eax,%eax
js     1280 <spd_set_drb+0x130>
mov    %eax,%ebx
mov    %r14b,0x7(%rsp)
and    $0xf,%ebx
mov    %ebp,%edi
mov    $0x4,%esi
call   1060 <smbus_read_byte@plt>
mov    %eax,%r12d
and    $0xf,%r12d
add    %ebx,%r12d
mov    %ebp,%edi
mov    $0x11,%esi
call   1060 <smbus_read_byte@plt>
movslq %eax,%rdi
call   1030 <log2@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    $0x6,%esi
call   1060 <smbus_read_byte@plt>
mov    %eax,%r14d
mov    %ebp,%edi
mov    $0x7,%esi
call   1060 <smbus_read_byte@plt>
cltq
shl    $0x8,%rax
movslq %r14d,%rdi
or     %rax,%rdi
call   1030 <log2@plt>
add    %r12d,%ebx
lea    (%rax,%rbx,1),%r12d
add    $0xffffffe6,%r12d
mov    %ebp,%edi
mov    $0x5,%esi
call   1060 <smbus_read_byte@plt>
mov    $0x1,%edx
mov    %r12d,%ecx
shl    %cl,%edx
cmp    $0x8,%r12d
jb     1255 <spd_set_drb+0x105>
xor    %edx,%edx
cmp    $0x2,%eax
lea    0xda1(%rip),%rbp
jge    1267 <spd_set_drb+0x117>
mov    $0xffffffff,%r12d
add    %edx,%r13d
movzbl 0x7(%rsp),%r14d
jmp    128d <spd_set_drb+0x13d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0xffffffff,%r12d
lea    0xd73(%rip),%rbp
lea    -0x1(%r15),%ebx
mov    %rbp,%rdi
call   1070 <print_debug@plt>
mov    %r13d,%edi
call   1050 <print_debug_hex32@plt>
lea    0xd68(%rip),%rdi
call   1070 <print_debug@plt>
movzbl %bl,%edx
xor    %edi,%edi
xor    %esi,%esi
mov    %r13d,%ecx
call   1040 <pcibios_write_config_byte@plt>
mov    $0x1,%eax
mov    %r12d,%ecx
shl    %cl,%eax
cmp    $0x8,%r12d
jb     1170 <spd_set_drb+0x20>
xor    %eax,%eax
jmp    1170 <spd_set_drb+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"end_of_memory: \00"
@.str.1 = [2 x i8] c"\0A\00"
define void @spd_set_drb() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i8 80, i8* %2
br label %8
8: ; preds = %90, %0
%9 = load i8, i8* %2
%10 = zext i8 %9 to i32
%11 = icmp sle i32 %10, 83
br i1 %11, label %12, label %105
12: ; preds = %8
store i32 -1, i32* %5
store i32 -1, i32* %4
%13 = load i8, i8* %2
%14 = call i32 @smbus_read_byte(i8 zeroext %13, i32 3)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp sge i32 %15, 0
br i1 %16, label %17, label %61
17: ; preds = %12
%18 = load i32, i32* %6
%19 = and i32 %18, 15
%20 = load i32, i32* %4
%21 = add i32 %20, %19
store i32 %21, i32* %4
%22 = load i8, i8* %2
%23 = call i32 @smbus_read_byte(i8 zeroext %22, i32 4)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = and i32 %24, 15
%26 = load i32, i32* %4
%27 = add i32 %26, %25
store i32 %27, i32* %4
%28 = load i8, i8* %2
%29 = call i32 @smbus_read_byte(i8 zeroext %28, i32 17)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = call i64 @log2(i64 %31)
%33 = load i32, i32* %4
%34 = zext i32 %33 to i64
%35 = add nsw i64 %34, %32
%36 = trunc i64 %35 to i32
store i32 %36, i32* %4
%37 = load i8, i8* %2
%38 = call i32 @smbus_read_byte(i8 zeroext %37, i32 6)
store i32 %38, i32* %6
%39 = load i8, i8* %2
%40 = call i32 @smbus_read_byte(i8 zeroext %39, i32 7)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = shl i64 %42, 8
%44 = load i32, i32* %6
%45 = sext i32 %44 to i64
%46 = or i64 %43, %45
%47 = call i64 @log2(i64 %46)
%48 = load i32, i32* %4
%49 = zext i32 %48 to i64
%50 = add nsw i64 %49, %47
%51 = trunc i64 %50 to i32
store i32 %51, i32* %4
%52 = load i32, i32* %4
%53 = sub i32 %52, 25
store i32 %53, i32* %4
%54 = load i8, i8* %2
%55 = call i32 @smbus_read_byte(i8 zeroext %54, i32 5)
store i32 %55, i32* %6
%56 = load i32, i32* %6
%57 = icmp sgt i32 %56, 1
br i1 %57, label %58, label %60
58: ; preds = %17
%59 = load i32, i32* %4
store i32 %59, i32* %5
br label %60
60: ; preds = %58, %17
br label %61
61: ; preds = %60, %12
%62 = load i32, i32* %4
%63 = icmp ult i32 %62, 8
br i1 %63, label %64, label %69
64: ; preds = %61
%65 = load i32, i32* %4
%66 = shl i32 1, %65
%67 = load i32, i32* %1
%68 = add i32 %67, %66
store i32 %68, i32* %1
br label %69
69: ; preds = %64, %61
%70 = load i8, i8* %2
%71 = zext i8 %70 to i32
%72 = sub nsw i32 %71, 80
%73 = shl i32 %72, 1
%74 = add nsw i32 %73, 96
%75 = trunc i32 %74 to i8
store i8 %75, i8* %3
%76 = call i32 @print_debug(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%77 = load i32, i32* %1
%78 = call i32 @print_debug_hex32(i32 %77)
%79 = call i32 @print_debug(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%80 = load i8, i8* %3
%81 = load i32, i32* %1
%82 = call i32 @pcibios_write_config_byte(i32 0, i64 0, i8 zeroext %80, i32 %81)
%83 = load i32, i32* %5
%84 = icmp ult i32 %83, 8
br i1 %84, label %85, label %90
85: ; preds = %69
%86 = load i32, i32* %5
%87 = shl i32 1, %86
%88 = load i32, i32* %1
%89 = add i32 %88, %87
store i32 %89, i32* %1
br label %90
90: ; preds = %85, %69
%91 = call i32 @print_debug(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%92 = load i32, i32* %1
%93 = call i32 @print_debug_hex32(i32 %92)
%94 = call i32 @print_debug(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%95 = load i8, i8* %3
%96 = zext i8 %95 to i32
%97 = add nsw i32 %96, 1
%98 = trunc i32 %97 to i8
%99 = load i32, i32* %1
%100 = call i32 @pcibios_write_config_byte(i32 0, i64 0, i8 zeroext %98, i32 %99)
%101 = load i8, i8* %2
%102 = zext i8 %101 to i32
%103 = add nsw i32 %102, 1
%104 = trunc i32 %103 to i8
store i8 %104, i8* %2
br label %8
105: ; preds = %8
ret void
}

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