#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 get_spd_byte(int, unsigned char);
unsigned int get_ddr_size(int slotnum, int ddrtype) {
  unsigned char offset_rank, offset_density, offset_0x08;
  unsigned char density, capacity, Width, BWidth, rank;
  int value = 0;
  int size = 0;
  if (ddrtype == 0x8) {
    offset_rank = 0x05;
    rank = get_spd_byte(slotnum, offset_rank) & 0xf;
    rank++;
    offset_density = 0x1F;
    if (density == 0x20)
      value = 128;
    else if (density == 0x40)
      value = 256;
    else if (density == 0x80)
      value = 512;
    else if (density == 0x01)
      value = 1024;
    else if (density == 0x02)
      value = 2048;
    else if (density == 0x04)
      value = 4096;
    else
      value = 0;
    size = rank * value;
  }
  if (ddrtype == 0xb) {
    offset_density = 0x04;
    capacity = get_spd_byte(slotnum, offset_density);
    capacity &= 0x0F;
    if (capacity == 1)
      value = 512;
    else if (capacity == 2)
      value = 1024;
    else if (capacity == 3)
      value = 2048;
    else if (capacity == 4)
      value = 4096;
    else if (capacity == 5)
      value = 8192;
    else if (capacity == 6)
      value = 16384;
    else
      value = 256;
    offset_rank = 0x07;
    rank = get_spd_byte(slotnum, offset_rank);
    Width = rank;
    Width &= 0x07;
    rank &= 0x38;
    rank = rank >> 3;
    rank++;
    switch (Width) {
    case 0:
      Width = 4;
      break;
    case 1:
      Width = 8;
      break;
    case 2:
      Width = 16;
      break;
    case 3:
      Width = 32;
      break;
    }
    offset_0x08 = 0x08;
    BWidth = get_spd_byte(slotnum, offset_0x08);
    BWidth &= 0x07;
    switch (BWidth) {
    case 0:
      BWidth = 8;
      break;
    case 1:
      BWidth = 16;
      break;
    case 2:
      BWidth = 32;
      break;
    case 3:
      BWidth = 64;
      break;
    }
    size = (value / 8) * BWidth / Width * rank;
  }
  return size;
}


x86 assembly (clang, -O0):
<get_ddr_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x8,-0x8(%rbp)
jne    121b <get_ddr_size+0x10b>
movb   $0x5,-0x9(%rbp)
mov    -0x4(%rbp),%edi
movzbl -0x9(%rbp),%esi
call   1030 <get_spd_byte@plt>
and    $0xf,%eax
mov    %al,-0x10(%rbp)
mov    -0x10(%rbp),%al
add    $0x1,%al
mov    %al,-0x10(%rbp)
movb   $0x1f,-0xa(%rbp)
movzbl -0xc(%rbp),%eax
cmp    $0x20,%eax
jne    1171 <get_ddr_size+0x61>
movl   $0x80,-0x14(%rbp)
jmp    1210 <get_ddr_size+0x100>
movzbl -0xc(%rbp),%eax
cmp    $0x40,%eax
jne    118a <get_ddr_size+0x7a>
movl   $0x100,-0x14(%rbp)
jmp    120b <get_ddr_size+0xfb>
movzbl -0xc(%rbp),%eax
cmp    $0x80,%eax
jne    11a5 <get_ddr_size+0x95>
movl   $0x200,-0x14(%rbp)
jmp    1206 <get_ddr_size+0xf6>
movzbl -0xc(%rbp),%eax
cmp    $0x1,%eax
jne    11be <get_ddr_size+0xae>
movl   $0x400,-0x14(%rbp)
jmp    1201 <get_ddr_size+0xf1>
movzbl -0xc(%rbp),%eax
cmp    $0x2,%eax
jne    11d7 <get_ddr_size+0xc7>
movl   $0x800,-0x14(%rbp)
jmp    11fc <get_ddr_size+0xec>
movzbl -0xc(%rbp),%eax
cmp    $0x4,%eax
jne    11f0 <get_ddr_size+0xe0>
movl   $0x1000,-0x14(%rbp)
jmp    11f7 <get_ddr_size+0xe7>
movl   $0x0,-0x14(%rbp)
jmp    11fc <get_ddr_size+0xec>
jmp    1201 <get_ddr_size+0xf1>
jmp    1206 <get_ddr_size+0xf6>
jmp    120b <get_ddr_size+0xfb>
jmp    1210 <get_ddr_size+0x100>
movzbl -0x10(%rbp),%eax
imul   -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0xb,-0x8(%rbp)
jne    13fb <get_ddr_size+0x2eb>
movb   $0x4,-0xa(%rbp)
mov    -0x4(%rbp),%edi
movzbl -0xa(%rbp),%esi
call   1030 <get_spd_byte@plt>
mov    %al,-0xd(%rbp)
movzbl -0xd(%rbp),%eax
and    $0xf,%eax
mov    %al,-0xd(%rbp)
movzbl -0xd(%rbp),%eax
cmp    $0x1,%eax
jne    125b <get_ddr_size+0x14b>
movl   $0x200,-0x14(%rbp)
jmp    12f8 <get_ddr_size+0x1e8>
movzbl -0xd(%rbp),%eax
cmp    $0x2,%eax
jne    1274 <get_ddr_size+0x164>
movl   $0x400,-0x14(%rbp)
jmp    12f3 <get_ddr_size+0x1e3>
movzbl -0xd(%rbp),%eax
cmp    $0x3,%eax
jne    128d <get_ddr_size+0x17d>
movl   $0x800,-0x14(%rbp)
jmp    12ee <get_ddr_size+0x1de>
movzbl -0xd(%rbp),%eax
cmp    $0x4,%eax
jne    12a6 <get_ddr_size+0x196>
movl   $0x1000,-0x14(%rbp)
jmp    12e9 <get_ddr_size+0x1d9>
movzbl -0xd(%rbp),%eax
cmp    $0x5,%eax
jne    12bf <get_ddr_size+0x1af>
movl   $0x2000,-0x14(%rbp)
jmp    12e4 <get_ddr_size+0x1d4>
movzbl -0xd(%rbp),%eax
cmp    $0x6,%eax
jne    12d8 <get_ddr_size+0x1c8>
movl   $0x4000,-0x14(%rbp)
jmp    12df <get_ddr_size+0x1cf>
movl   $0x100,-0x14(%rbp)
jmp    12e4 <get_ddr_size+0x1d4>
jmp    12e9 <get_ddr_size+0x1d9>
jmp    12ee <get_ddr_size+0x1de>
jmp    12f3 <get_ddr_size+0x1e3>
jmp    12f8 <get_ddr_size+0x1e8>
movb   $0x7,-0x9(%rbp)
mov    -0x4(%rbp),%edi
movzbl -0x9(%rbp),%esi
call   1030 <get_spd_byte@plt>
mov    %al,-0x10(%rbp)
mov    -0x10(%rbp),%al
mov    %al,-0xe(%rbp)
mov    -0xe(%rbp),%al
and    $0x7,%al
mov    %al,-0xe(%rbp)
mov    -0x10(%rbp),%al
and    $0x38,%al
mov    %al,-0x10(%rbp)
movzbl -0x10(%rbp),%eax
shr    $0x3,%eax
mov    %al,-0x10(%rbp)
mov    -0x10(%rbp),%al
add    $0x1,%al
mov    %al,-0x10(%rbp)
movzbl -0xe(%rbp),%eax
mov    %rax,-0x20(%rbp)
sub    $0x3,%rax
ja     1378 <get_ddr_size+0x268>
mov    -0x20(%rbp),%rax
lea    0xcb0(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movb   $0x4,-0xe(%rbp)
jmp    1378 <get_ddr_size+0x268>
movb   $0x8,-0xe(%rbp)
jmp    1378 <get_ddr_size+0x268>
movb   $0x10,-0xe(%rbp)
jmp    1378 <get_ddr_size+0x268>
movb   $0x20,-0xe(%rbp)
movb   $0x8,-0xb(%rbp)
mov    -0x4(%rbp),%edi
movzbl -0xb(%rbp),%esi
call   1030 <get_spd_byte@plt>
mov    %al,-0xf(%rbp)
mov    -0xf(%rbp),%al
and    $0x7,%al
mov    %al,-0xf(%rbp)
movzbl -0xf(%rbp),%eax
mov    %rax,-0x28(%rbp)
sub    $0x3,%rax
ja     13d8 <get_ddr_size+0x2c8>
mov    -0x28(%rbp),%rax
lea    0xc60(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movb   $0x8,-0xf(%rbp)
jmp    13d8 <get_ddr_size+0x2c8>
movb   $0x10,-0xf(%rbp)
jmp    13d8 <get_ddr_size+0x2c8>
movb   $0x20,-0xf(%rbp)
jmp    13d8 <get_ddr_size+0x2c8>
movb   $0x40,-0xf(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
movzbl -0xf(%rbp),%ecx
imul   %ecx,%eax
movzbl -0xe(%rbp),%ecx
cltd
idiv   %ecx
movzbl -0x10(%rbp),%ecx
imul   %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ddr_size>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
cmp    $0xb,%esi
je     113c <get_ddr_size+0x2c>
xor    %ebx,%ebx
cmp    $0x8,%esi
jne    11cf <get_ddr_size+0xbf>
mov    %r14d,%edi
mov    $0x5,%esi
call   1030 <get_spd_byte@plt>
jmp    11cf <get_ddr_size+0xbf>
mov    %r14d,%edi
mov    $0x4,%esi
call   1030 <get_spd_byte@plt>
and    $0xf,%eax
add    $0xffffffff,%eax
mov    $0x20,%r15d
cmp    $0x5,%eax
ja     1167 <get_ddr_size+0x57>
cltq
lea    0xe9d(%rip),%rcx
mov    (%rcx,%rax,4),%r15d
mov    %r14d,%edi
mov    $0x7,%esi
call   1030 <get_spd_byte@plt>
mov    %eax,%ebp
and    $0x7,%al
shr    $0x3,%ebp
and    $0x7,%ebp
add    $0x1,%ebp
lea    0x0(,%rax,8),%ecx
mov    $0x20100804,%edx
shr    %cl,%edx
cmp    $0x4,%al
movzbl %al,%ebx
cmovb  %edx,%ebx
mov    %r14d,%edi
mov    $0x8,%esi
call   1030 <get_spd_byte@plt>
and    $0x7,%al
lea    0x0(,%rax,8),%ecx
mov    $0x40201008,%edx
shr    %cl,%edx
cmp    $0x4,%al
movzbl %al,%eax
cmovb  %edx,%eax
movzbl %al,%eax
imul   %r15d,%eax
movzbl %bl,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,%ebx
imul   %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @get_ddr_size(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8
%12 = alloca i8
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %13
store i32 0, i32* %14
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 8
br i1 %16, label %17, label %65
17: ; preds = %2
store i8 5, i8* %5
%18 = load i32, i32* %3
%19 = load i8, i8* %5
%20 = call i32 @get_spd_byte(i32 %18, i8 zeroext %19)
%21 = and i32 %20, 15
%22 = trunc i32 %21 to i8
store i8 %22, i8* %12
%23 = load i8, i8* %12
%24 = add i8 %23, 1
store i8 %24, i8* %12
store i8 31, i8* %6
%25 = load i8, i8* %8
%26 = zext i8 %25 to i32
%27 = icmp eq i32 %26, 32
br i1 %27, label %28, label %29
28: ; preds = %17
store i32 128, i32* %13
br label %60
29: ; preds = %17
%30 = load i8, i8* %8
%31 = zext i8 %30 to i32
%32 = icmp eq i32 %31, 64
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 256, i32* %13
br label %59
34: ; preds = %29
%35 = load i8, i8* %8
%36 = zext i8 %35 to i32
%37 = icmp eq i32 %36, 128
br i1 %37, label %38, label %39
38: ; preds = %34
store i32 512, i32* %13
br label %58
39: ; preds = %34
%40 = load i8, i8* %8
%41 = zext i8 %40 to i32
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %44
43: ; preds = %39
store i32 1024, i32* %13
br label %57
44: ; preds = %39
%45 = load i8, i8* %8
%46 = zext i8 %45 to i32
%47 = icmp eq i32 %46, 2
br i1 %47, label %48, label %49
48: ; preds = %44
store i32 2048, i32* %13
br label %56
49: ; preds = %44
%50 = load i8, i8* %8
%51 = zext i8 %50 to i32
%52 = icmp eq i32 %51, 4
br i1 %52, label %53, label %54
53: ; preds = %49
store i32 4096, i32* %13
br label %55
54: ; preds = %49
store i32 0, i32* %13
br label %55
55: ; preds = %54, %53
br label %56
56: ; preds = %55, %48
br label %57
57: ; preds = %56, %43
br label %58
58: ; preds = %57, %38
br label %59
59: ; preds = %58, %33
br label %60
60: ; preds = %59, %28
%61 = load i8, i8* %12
%62 = zext i8 %61 to i32
%63 = load i32, i32* %13
%64 = mul nsw i32 %62, %63
store i32 %64, i32* %14
br label %65
65: ; preds = %60, %2
%66 = load i32, i32* %4
%67 = icmp eq i32 %66, 11
br i1 %67, label %68, label %165
68: ; preds = %65
store i8 4, i8* %6
%69 = load i32, i32* %3
%70 = load i8, i8* %6
%71 = call i32 @get_spd_byte(i32 %69, i8 zeroext %70)
%72 = trunc i32 %71 to i8
store i8 %72, i8* %9
%73 = load i8, i8* %9
%74 = zext i8 %73 to i32
%75 = and i32 %74, 15
%76 = trunc i32 %75 to i8
store i8 %76, i8* %9
%77 = load i8, i8* %9
%78 = zext i8 %77 to i32
%79 = icmp eq i32 %78, 1
br i1 %79, label %80, label %81
80: ; preds = %68
store i32 512, i32* %13
br label %112
81: ; preds = %68
%82 = load i8, i8* %9
%83 = zext i8 %82 to i32
%84 = icmp eq i32 %83, 2
br i1 %84, label %85, label %86
85: ; preds = %81
store i32 1024, i32* %13
br label %111
86: ; preds = %81
%87 = load i8, i8* %9
%88 = zext i8 %87 to i32
%89 = icmp eq i32 %88, 3
br i1 %89, label %90, label %91
90: ; preds = %86
store i32 2048, i32* %13
br label %110
91: ; preds = %86
%92 = load i8, i8* %9
%93 = zext i8 %92 to i32
%94 = icmp eq i32 %93, 4
br i1 %94, label %95, label %96
95: ; preds = %91
store i32 4096, i32* %13
br label %109
96: ; preds = %91
%97 = load i8, i8* %9
%98 = zext i8 %97 to i32
%99 = icmp eq i32 %98, 5
br i1 %99, label %100, label %101
100: ; preds = %96
store i32 8192, i32* %13
br label %108
101: ; preds = %96
%102 = load i8, i8* %9
%103 = zext i8 %102 to i32
%104 = icmp eq i32 %103, 6
br i1 %104, label %105, label %106
105: ; preds = %101
store i32 16384, i32* %13
br label %107
106: ; preds = %101
store i32 256, i32* %13
br label %107
107: ; preds = %106, %105
br label %108
108: ; preds = %107, %100
br label %109
109: ; preds = %108, %95
br label %110
110: ; preds = %109, %90
br label %111
111: ; preds = %110, %85
br label %112
112: ; preds = %111, %80
store i8 7, i8* %5
%113 = load i32, i32* %3
%114 = load i8, i8* %5
%115 = call i32 @get_spd_byte(i32 %113, i8 zeroext %114)
%116 = trunc i32 %115 to i8
store i8 %116, i8* %12
%117 = load i8, i8* %12
store i8 %117, i8* %10
%118 = load i8, i8* %10
%119 = zext i8 %118 to i32
%120 = and i32 %119, 7
%121 = trunc i32 %120 to i8
store i8 %121, i8* %10
%122 = load i8, i8* %12
%123 = zext i8 %122 to i32
%124 = and i32 %123, 56
%125 = trunc i32 %124 to i8
store i8 %125, i8* %12
%126 = load i8, i8* %12
%127 = zext i8 %126 to i32
%128 = ashr i32 %127, 3
%129 = trunc i32 %128 to i8
store i8 %129, i8* %12
%130 = load i8, i8* %12
%131 = add i8 %130, 1
store i8 %131, i8* %12
%132 = load i8, i8* %10
%133 = zext i8 %132 to i32
switch i32 %133, label %138 [
i32 0, label %134
i32 1, label %135
i32 2, label %136
i32 3, label %137
]
134: ; preds = %112
store i8 4, i8* %10
br label %138
135: ; preds = %112
store i8 8, i8* %10
br label %138
136: ; preds = %112
store i8 16, i8* %10
br label %138
137: ; preds = %112
store i8 32, i8* %10
br label %138
138: ; preds = %112, %137, %136, %135, %134
store i8 8, i8* %7
%139 = load i32, i32* %3
%140 = load i8, i8* %7
%141 = call i32 @get_spd_byte(i32 %139, i8 zeroext %140)
%142 = trunc i32 %141 to i8
store i8 %142, i8* %11
%143 = load i8, i8* %11
%144 = zext i8 %143 to i32
%145 = and i32 %144, 7
%146 = trunc i32 %145 to i8
store i8 %146, i8* %11
%147 = load i8, i8* %11
%148 = zext i8 %147 to i32
switch i32 %148, label %153 [
i32 0, label %149
i32 1, label %150
i32 2, label %151
i32 3, label %152
]
149: ; preds = %138
store i8 8, i8* %11
br label %153
150: ; preds = %138
store i8 16, i8* %11
br label %153
151: ; preds = %138
store i8 32, i8* %11
br label %153
152: ; preds = %138
store i8 64, i8* %11
br label %153
153: ; preds = %138, %152, %151, %150, %149
%154 = load i32, i32* %13
%155 = sdiv i32 %154, 8
%156 = load i8, i8* %11
%157 = zext i8 %156 to i32
%158 = mul nsw i32 %155, %157
%159 = load i8, i8* %10
%160 = zext i8 %159 to i32
%161 = sdiv i32 %158, %160
%162 = load i8, i8* %12
%163 = zext i8 %162 to i32
%164 = mul nsw i32 %161, %163
store i32 %164, i32* %14
br label %165
165: ; preds = %153, %65
%166 = load i32, i32* %14
ret i32 %166
}

LLVM IR (-O2):
@switch.table.get_ddr_size = [6 x i32] [i32 64, i32 128, i32 256, i32 512, i32 1024, i32 2048]
define i32 @get_ddr_size(i32 %0, i32 %1) {
switch i32 %1, label %42 [
i32 8, label %3
i32 11, label %5
]
3: ; preds = %2
%4 = tail call i32 @get_spd_byte(i32 %0, i8 zeroext 5)
br label %42
5: ; preds = %2
%6 = tail call i32 @get_spd_byte(i32 %0, i8 zeroext 4)
%7 = and i32 %6, 15
%8 = add nsw i32 %7, -1
%9 = icmp ult i32 %8, 6
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = sext i32 %8 to i64
%12 = getelementptr inbounds [6 x i32], [6 x i32]* @switch.table.get_ddr_size, i64 0, i64 %11
%13 = load i32, i32* %12
br label %14
14: ; preds = %5, %10
%15 = phi i32 [ %13, %10 ], [ 32, %5 ]
%16 = tail call i32 @get_spd_byte(i32 %0, i8 zeroext 7)
%17 = trunc i32 %16 to i8
%18 = and i8 %17, 7
%19 = lshr i32 %16, 3
%20 = and i32 %19, 7
%21 = add nuw nsw i32 %20, 1
%22 = icmp ult i8 %18, 4
%23 = shl nuw nsw i8 %18, 3
%24 = zext i8 %23 to i32
%25 = lshr i32 537921540, %24
%26 = trunc i32 %25 to i8
%27 = select i1 %22, i8 %26, i8 %18
%28 = tail call i32 @get_spd_byte(i32 %0, i8 zeroext 8)
%29 = trunc i32 %28 to i8
%30 = and i8 %29, 7
%31 = icmp ult i8 %30, 4
%32 = shl nuw nsw i8 %30, 3
%33 = zext i8 %32 to i32
%34 = lshr i32 1075843080, %33
%35 = trunc i32 %34 to i8
%36 = select i1 %31, i8 %35, i8 %30
%37 = zext i8 %36 to i32
%38 = mul nuw nsw i32 %15, %37
%39 = zext i8 %27 to i32
%40 = udiv i32 %38, %39
%41 = mul nuw nsw i32 %40, %21
br label %42
42: ; preds = %3, %2, %14
%43 = phi i32 [ %41, %14 ], [ 0, %3 ], [ 0, %2 ]
ret i32 %43
}