#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 */
typedef int /*<<< orphan*/ memsize;

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned long long);
int sysctlbyname(char *, unsigned long long *, size_t *, int /*<<< orphan*/ *,
                 int /*<<< orphan*/);
int main(void) {
  int ret;
  unsigned long long memsize;
  size_t len = sizeof(memsize);
  if (!(ret = sysctlbyname("hw.memsize", &memsize, &len, NULL, 0)))
    printf("%lld MB\n", (memsize >> 20ULL));
  else
    perror("sysctlbyname");
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x8,-0x18(%rbp)
lea    0xeb2(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   1050 <sysctlbyname@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jne    1187 <main+0x57>
mov    -0x10(%rbp),%rsi
shr    $0x14,%rsi
lea    0xe8e(%rip),%rdi
call   1030 <printf@plt>
jmp    1193 <main+0x63>
lea    0xe86(%rip),%rdi
call   1040 <perror@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movq   $0x8,(%rsp)
lea    0xebc(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    %rsp,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <sysctlbyname@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1179 <main+0x49>
mov    0x8(%rsp),%rsi
shr    $0x14,%rsi
lea    0xe9f(%rip),%rdi
call   1030 <printf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe94(%rip),%rdi
call   1040 <perror@plt>
jmp    1171 <main+0x41>

LLVM IR (-O0):
@.str = [11 x i8] c"hw.memsize\00"
@.str.1 = [9 x i8] c"%lld MB\0A\00"
@.str.2 = [13 x i8] c"sysctlbyname\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
store i32 0, i32* %1
store i64 8, i64* %4
%5 = call i32 @sysctlbyname(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64* %3, i64* %4, i32* null, i32 0)
store i32 %5, i32* %2
%6 = icmp ne i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %0
%8 = load i64, i64* %3
%9 = lshr i64 %8, 20
%10 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i64 %9)
br label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %11, %7
%14 = load i32, i32* %2
ret i32 %14
}

LLVM IR (-O2):
@.str = [11 x i8] c"hw.memsize\00"
@.str.1 = [9 x i8] c"%lld MB\0A\00"
@.str.2 = [13 x i8] c"sysctlbyname\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca i64
%3 = bitcast i64* %1 to i8*
%4 = bitcast i64* %2 to i8*
store i64 8, i64* %2
%5 = call i32 @sysctlbyname(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i64* %1, i64* %2, i32* null, i32 0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = load i64, i64* %1
%9 = lshr i64 %8, 20
%10 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i64 %9)
br label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %11, %7
ret i32 %5
}