#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 int gfx_read_reg32(int);
unsigned int GetVideoMemSize(void) {
  unsigned int graphicsMemBaseAddr;
  unsigned int totalMem = 0;
  int i;
  unsigned int graphicsMemMask, graphicsMemShift;
  graphicsMemBaseAddr = gfx_read_reg32(0x8414);
  if (1) {
    unsigned int mcBankCfg = gfx_read_reg32(0x8408);
    unsigned int dimmShift = 4;
    graphicsMemMask = 0x7FF;
    graphicsMemShift = 19;
    for (i = 0; i < 2; i++) {
      if (((mcBankCfg >> dimmShift) & 0x7) != 0x7) {
        switch ((mcBankCfg >> (dimmShift + 4)) & 0x7) {
        case 0:
          totalMem += 0x400000;
          break;
        case 1:
          totalMem += 0x800000;
          break;
        case 2:
          totalMem += 0x1000000;
          break;
        case 3:
          totalMem += 0x2000000;
          break;
        case 4:
          totalMem += 0x4000000;
          break;
        case 5:
          totalMem += 0x8000000;
          break;
        case 6:
          totalMem += 0x10000000;
          break;
        case 7:
          totalMem += 0x20000000;
          break;
        default:
          break;
        }
      }
      dimmShift += 16;
    }
  } else {
    unsigned int mcMemCntrl1 = gfx_read_reg32(0x8400);
    unsigned int bankSizeShift = 12;
    graphicsMemMask = 0x3FF;
    graphicsMemShift = 17;
    for (i = 0; i < 4; i++) {
      switch ((mcMemCntrl1 >> bankSizeShift) & 0x7) {
      case 1:
        totalMem += 0x200000;
        break;
      case 2:
        totalMem += 0x400000;
        break;
      case 3:
        totalMem += 0x800000;
        break;
      case 4:
        totalMem += 0x1000000;
        break;
      case 5:
        totalMem += 0x2000000;
        break;
      default:
        break;
      }
      bankSizeShift += 3;
    }
  }
  graphicsMemBaseAddr &= graphicsMemMask;
  graphicsMemBaseAddr <<= graphicsMemShift;
  return (totalMem - graphicsMemBaseAddr);
}


x86 assembly (clang, -O0):
<GetVideoMemSize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x8(%rbp)
mov    $0x8414,%edi
call   1030 <gfx_read_reg32@plt>
mov    %eax,-0x4(%rbp)
mov    $0x8408,%edi
call   1030 <gfx_read_reg32@plt>
mov    %eax,-0x18(%rbp)
movl   $0x4,-0x1c(%rbp)
movl   $0x7ff,-0x10(%rbp)
movl   $0x13,-0x14(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x2,-0xc(%rbp)
jge    1245 <GetVideoMemSize+0x135>
mov    -0x18(%rbp),%eax
mov    -0x1c(%rbp),%ecx
shr    %cl,%eax
and    $0x7,%eax
cmp    $0x7,%eax
je     122e <GetVideoMemSize+0x11e>
mov    -0x18(%rbp),%eax
mov    -0x1c(%rbp),%cl
add    $0x4,%cl
shr    %cl,%eax
and    $0x7,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x7,%eax
ja     1224 <GetVideoMemSize+0x114>
mov    -0x28(%rbp),%rax
lea    0xe65(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x8(%rbp),%eax
add    $0x400000,%eax
mov    %eax,-0x8(%rbp)
jmp    1229 <GetVideoMemSize+0x119>
mov    -0x8(%rbp),%eax
add    $0x800000,%eax
mov    %eax,-0x8(%rbp)
jmp    1229 <GetVideoMemSize+0x119>
mov    -0x8(%rbp),%eax
add    $0x1000000,%eax
mov    %eax,-0x8(%rbp)
jmp    1229 <GetVideoMemSize+0x119>
mov    -0x8(%rbp),%eax
add    $0x2000000,%eax
mov    %eax,-0x8(%rbp)
jmp    1229 <GetVideoMemSize+0x119>
mov    -0x8(%rbp),%eax
add    $0x4000000,%eax
mov    %eax,-0x8(%rbp)
jmp    1229 <GetVideoMemSize+0x119>
mov    -0x8(%rbp),%eax
add    $0x8000000,%eax
mov    %eax,-0x8(%rbp)
jmp    1229 <GetVideoMemSize+0x119>
mov    -0x8(%rbp),%eax
add    $0x10000000,%eax
mov    %eax,-0x8(%rbp)
jmp    1229 <GetVideoMemSize+0x119>
mov    -0x8(%rbp),%eax
add    $0x20000000,%eax
mov    %eax,-0x8(%rbp)
jmp    1229 <GetVideoMemSize+0x119>
jmp    1229 <GetVideoMemSize+0x119>
jmp    122e <GetVideoMemSize+0x11e>
mov    -0x1c(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1155 <GetVideoMemSize+0x45>
mov    -0x10(%rbp),%eax
and    -0x4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x4(%rbp),%eax
shl    %cl,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
sub    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetVideoMemSize>:
push   %rbx
mov    $0x8414,%edi
call   1030 <gfx_read_reg32@plt>
mov    %eax,%ebx
mov    $0x8408,%edi
call   1030 <gfx_read_reg32@plt>
mov    %eax,%ecx
mov    %eax,%edx
not    %edx
xor    %eax,%eax
test   $0x70,%dl
je     1146 <GetVideoMemSize+0x36>
mov    %ecx,%eax
shr    $0x8,%eax
and    $0x7,%eax
lea    0xedd(%rip),%rsi
mov    (%rsi,%rax,4),%eax
test   $0x700000,%edx
jne    115b <GetVideoMemSize+0x4b>
shl    $0x13,%ebx
and    $0x3ff80000,%ebx
sub    %ebx,%eax
pop    %rbx
ret
shr    $0x18,%ecx
and    $0x7,%ecx
lea    0xe98(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
add    $0x400000,%eax
jmp    114e <GetVideoMemSize+0x3e>
add    $0x800000,%eax
jmp    114e <GetVideoMemSize+0x3e>
add    $0x1000000,%eax
jmp    114e <GetVideoMemSize+0x3e>
add    $0x2000000,%eax
jmp    114e <GetVideoMemSize+0x3e>
add    $0x4000000,%eax
jmp    114e <GetVideoMemSize+0x3e>
add    $0x8000000,%eax
jmp    114e <GetVideoMemSize+0x3e>
add    $0x10000000,%eax
jmp    114e <GetVideoMemSize+0x3e>
add    $0x20000000,%eax
jmp    114e <GetVideoMemSize+0x3e>

LLVM IR (-O0):
define i32 @GetVideoMemSize() {
%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* %2
%8 = call i32 @gfx_read_reg32(i32 33812)
store i32 %8, i32* %1
%9 = call i32 @gfx_read_reg32(i32 33800)
store i32 %9, i32* %6
store i32 4, i32* %7
store i32 2047, i32* %4
store i32 19, i32* %5
store i32 0, i32* %3
br label %10
10: ; preds = %54, %0
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %57
13: ; preds = %10
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = lshr i32 %14, %15
%17 = and i32 %16, 7
%18 = icmp ne i32 %17, 7
br i1 %18, label %19, label %51
19: ; preds = %13
%20 = load i32, i32* %6
%21 = load i32, i32* %7
%22 = add i32 %21, 4
%23 = lshr i32 %20, %22
%24 = and i32 %23, 7
switch i32 %24, label %49 [
i32 0, label %25
i32 1, label %28
i32 2, label %31
i32 3, label %34
i32 4, label %37
i32 5, label %40
i32 6, label %43
i32 7, label %46
]
25: ; preds = %19
%26 = load i32, i32* %2
%27 = add i32 %26, 4194304
store i32 %27, i32* %2
br label %50
28: ; preds = %19
%29 = load i32, i32* %2
%30 = add i32 %29, 8388608
store i32 %30, i32* %2
br label %50
31: ; preds = %19
%32 = load i32, i32* %2
%33 = add i32 %32, 16777216
store i32 %33, i32* %2
br label %50
34: ; preds = %19
%35 = load i32, i32* %2
%36 = add i32 %35, 33554432
store i32 %36, i32* %2
br label %50
37: ; preds = %19
%38 = load i32, i32* %2
%39 = add i32 %38, 67108864
store i32 %39, i32* %2
br label %50
40: ; preds = %19
%41 = load i32, i32* %2
%42 = add i32 %41, 134217728
store i32 %42, i32* %2
br label %50
43: ; preds = %19
%44 = load i32, i32* %2
%45 = add i32 %44, 268435456
store i32 %45, i32* %2
br label %50
46: ; preds = %19
%47 = load i32, i32* %2
%48 = add i32 %47, 536870912
store i32 %48, i32* %2
br label %50
49: ; preds = %19
br label %50
50: ; preds = %49, %46, %43, %40, %37, %34, %31, %28, %25
br label %51
51: ; preds = %50, %13
%52 = load i32, i32* %7
%53 = add i32 %52, 16
store i32 %53, i32* %7
br label %54
54: ; preds = %51
%55 = load i32, i32* %3
%56 = add nsw i32 %55, 1
store i32 %56, i32* %3
br label %10
57: ; preds = %10
%58 = load i32, i32* %4
%59 = load i32, i32* %1
%60 = and i32 %59, %58
store i32 %60, i32* %1
%61 = load i32, i32* %5
%62 = load i32, i32* %1
%63 = shl i32 %62, %61
store i32 %63, i32* %1
%64 = load i32, i32* %2
%65 = load i32, i32* %1
%66 = sub i32 %64, %65
ret i32 %66
}

LLVM IR (-O2):
@switch.table.GetVideoMemSize = [8 x i32] [i32 4194304, i32 8388608, i32 16777216, i32 33554432, i32 67108864, i32 134217728, i32 268435456, i32 536870912]
define i32 @GetVideoMemSize() {
%1 = tail call i32 @gfx_read_reg32(i32 33812)
%2 = tail call i32 @gfx_read_reg32(i32 33800)
%3 = and i32 %2, 112
%4 = icmp eq i32 %3, 112
br i1 %4, label %12, label %5
5: ; preds = %0
%6 = lshr i32 %2, 8
%7 = and i32 %6, 7
%8 = zext i32 %7 to i64
%9 = getelementptr inbounds [8 x i32], [8 x i32]* @switch.table.GetVideoMemSize, i64 0, i64 %8
%10 = load i32, i32* %9
br label %12
11: ; preds = %16
unreachable
12: ; preds = %5, %0
%13 = phi i32 [ 0, %0 ], [ %10, %5 ]
%14 = and i32 %2, 7340032
%15 = icmp eq i32 %14, 7340032
br i1 %15, label %35, label %16
16: ; preds = %12
%17 = lshr i32 %2, 24
%18 = and i32 %17, 7
switch i32 %18, label %11 [
i32 0, label %33
i32 1, label %31
i32 2, label %29
i32 3, label %27
i32 4, label %25
i32 5, label %23
i32 6, label %21
i32 7, label %19
]
19: ; preds = %16
%20 = add nuw nsw i32 %13, 536870912
br label %35
21: ; preds = %16
%22 = add nuw nsw i32 %13, 268435456
br label %35
23: ; preds = %16
%24 = add nuw nsw i32 %13, 134217728
br label %35
25: ; preds = %16
%26 = add nuw nsw i32 %13, 67108864
br label %35
27: ; preds = %16
%28 = add nuw nsw i32 %13, 33554432
br label %35
29: ; preds = %16
%30 = add nuw nsw i32 %13, 16777216
br label %35
31: ; preds = %16
%32 = add nuw nsw i32 %13, 8388608
br label %35
33: ; preds = %16
%34 = add nuw nsw i32 %13, 4194304
br label %35
35: ; preds = %33, %31, %29, %27, %25, %23, %21, %19, %12
%36 = phi i32 [ %20, %19 ], [ %22, %21 ], [ %24, %23 ], [ %26, %25 ], [ %28, %27 ], [ %30, %29 ], [ %32, %31 ], [ %34, %33 ], [ %13, %12 ]
%37 = shl i32 %1, 19
%38 = and i32 %37, 1073217536
%39 = sub nsw i32 %36, %38
ret i32 %39
}