#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 *malloc(int);
int malloc_size(int *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int *x;
  x = malloc(sizeof(int));
  *x = 42;
  int *y;
  y = malloc(1);
  *y = 42;
  int x_size = malloc_size(x);
  int y_size = malloc_size(y);
  printf("Bytes allocated for y: %d, and for x: %d\n", y_size, x_size);
  int *z;
  z = malloc(20);
  int z_size = malloc_size(z);
  printf("OK but what if we allocated more than 16 bytes..."
         "Bytes allocated for z: %d\n",
         z_size);
  printf("hi\n");
  int int_size = sizeof(int);
  printf("Bytes required to store an integer: %d\n", int_size);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x2a,(%rax)
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x2a,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <malloc_size@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <malloc_size@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x14,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <malloc_size@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x4,-0x30(%rbp)
mov    -0x30(%rbp),%esi
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
movl   $0x2a,(%rax)
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
movl   $0x2a,(%rax)
mov    %r14,%rdi
call   1030 <malloc_size@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <malloc_size@plt>
lea    0xe7d(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x14,%edi
call   1060 <malloc@plt>
mov    %rax,%rdi
call   1030 <malloc_size@plt>
lea    0xe83(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xee7(%rip),%rdi
call   1040 <puts@plt>
lea    0xeb3(%rip),%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"Bytes allocated for y: %d, and for x: %d\0A\00"
@.str.1 = [76 x i8] c"OK but what if we allocated more than 16 bytes...Bytes allocated for z: %d\0A\00"
@.str.2 = [4 x i8] c"hi\0A\00"
@.str.3 = [40 x i8] c"Bytes required to store an integer: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32* @malloc(i32 4)
store i32* %9, i32** %2
%10 = load i32*, i32** %2
store i32 42, i32* %10
%11 = call i32* @malloc(i32 1)
store i32* %11, i32** %3
%12 = load i32*, i32** %3
store i32 42, i32* %12
%13 = load i32*, i32** %2
%14 = call i32 @malloc_size(i32* %13)
store i32 %14, i32* %4
%15 = load i32*, i32** %3
%16 = call i32 @malloc_size(i32* %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = load i32, i32* %4
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %18)
%20 = call i32* @malloc(i32 20)
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = call i32 @malloc_size(i32* %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i32 4, i32* %8
%26 = load i32, i32* %8
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %26)
ret i32 0
}

LLVM IR (-O2):
@.str = [42 x i8] c"Bytes allocated for y: %d, and for x: %d\0A\00"
@.str.1 = [76 x i8] c"OK but what if we allocated more than 16 bytes...Bytes allocated for z: %d\0A\00"
@.str.3 = [40 x i8] c"Bytes required to store an integer: %d\0A\00"
@str = [3 x i8] c"hi\00"
define i32 @main() {
%1 = tail call i32* @malloc(i32 4)
store i32 42, i32* %1
%2 = tail call i32* @malloc(i32 1)
store i32 42, i32* %2
%3 = tail call i32 @malloc_size(i32* %1)
%4 = tail call i32 @malloc_size(i32* %2)
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %3)
%6 = tail call i32* @malloc(i32 20)
%7 = tail call i32 @malloc_size(i32* %6)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([76 x i8], [76 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 4)
ret i32 0
}