#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_mem_size(int);
int /*<<< orphan*/ printf(char *, ...);
int calculate_flash_size(int f) {
  int mem, x;
  mem = get_mem_size(f);
  if (mem <= 0) {
    printf("ERROR: Failed to get memory size.\n");
  }
  printf("Device reports %i kb memory.\n", mem);
  x = 1;
  while (x < mem)
    x <<= 1;
  printf("Assuming flash size is %i kb or %i pages.\n", x, x / 32);
  return x;
}


x86 assembly (clang, -O0):
<calculate_flash_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <get_mem_size@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     114e <calculate_flash_size+0x2e>
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xecb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1180 <calculate_flash_size+0x60>
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1166 <calculate_flash_size+0x46>
mov    -0xc(%rbp),%esi
mov    -0xc(%rbp),%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_flash_size>:
push   %rbp
push   %rbx
push   %rax
call   1050 <get_mem_size@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     114a <calculate_flash_size+0x1a>
lea    0xf04(%rip),%rdi
call   1030 <puts@plt>
lea    0xeaf(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
nop
mov    %eax,%ebx
lea    (%rbx,%rbx,1),%eax
cmp    %ebp,%ebx
jl     1160 <calculate_flash_size+0x30>
lea    0x1f(%rbx),%edx
test   %ebx,%ebx
cmovns %ebx,%edx
sar    $0x5,%edx
lea    0xea3(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"ERROR: Failed to get memory size.\0A\00"
@.str.1 = [30 x i8] c"Device reports %i kb memory.\0A\00"
@.str.2 = [43 x i8] c"Assuming flash size is %i kb or %i pages.\0A\00"
define i32 @calculate_flash_size(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i32 @get_mem_size(i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp sle i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = load i32, i32* %3
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %12)
store i32 1, i32* %4
br label %14
14: ; preds = %18, %11
%15 = load i32, i32* %4
%16 = load i32, i32* %3
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* %4
%20 = shl i32 %19, 1
store i32 %20, i32* %4
br label %14
21: ; preds = %14
%22 = load i32, i32* %4
%23 = load i32, i32* %4
%24 = sdiv i32 %23, 32
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %24)
%26 = load i32, i32* %4
ret i32 %26
}

LLVM IR (-O2):
@.str.1 = [30 x i8] c"Device reports %i kb memory.\0A\00"
@.str.2 = [43 x i8] c"Assuming flash size is %i kb or %i pages.\0A\00"
@str = [34 x i8] c"ERROR: Failed to get memory size.\00"
define i32 @calculate_flash_size(i32 %0) {
%2 = tail call i32 @get_mem_size(i32 %0)
%3 = icmp slt i32 %2, 1
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %2)
br label %8
8: ; preds = %8, %6
%9 = phi i32 [ 1, %6 ], [ %11, %8 ]
%10 = icmp slt i32 %9, %2
%11 = shl i32 %9, 1
br i1 %10, label %8, label %12
12: ; preds = %8
%13 = sdiv i32 %9, 32
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %9, i32 %13)
ret i32 %9
}