void test_void_pointers() {
  printf("\n===== test_void_pointers\n");
  int i = 42;
  void *p = &i;
  printf("Set void pointer to the address of an int: `void *p = &i`. Address: "
         "%p\n",
         p);
  assert(*(int *)p == 42);
  printf("Casting a void pointer to assumed type and dereferencing: "
         "`*(int*)p`. Returns %d\n",
         *(int *)p);
  int *int_p = p;
  printf("Set int pointer equal to void pointer: `int *int_p = p`\n");
  assert(*int_p == 42);
  printf("Dereferencing int pointer: `*int_p`. Returns %d\n", *int_p);
  float *float_p = p;
  printf("But would not stop you setting float pointer equal to void pointer "
         "that points to int: `float *float_p = p`\n");
  printf("Dereferencing the float pointer doesn't blow up (since float and int "
         "both 4 bytes), but value is wrong: %f\n",
         *float_p);
}


x86 assembly (clang, -O0):
<test_void_pointers>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xed1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2a,-0x4(%rbp)
lea    -0x4(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xecb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
cmpl   $0x2a,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1040 <assert@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xee9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    0xf25(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
cmpl   $0x2a,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1040 <assert@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0xf34(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    0xf4f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
movss  (%rax),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xfa2(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_void_pointers>:
push   %rax
lea    0xfff(%rip),%rdi
call   1030 <puts@plt>
movl   $0x2a,0x4(%rsp)
lea    0xeb4(%rip),%rdi
lea    0x4(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
cmpl   $0x2a,0x4(%rsp)
sete   %dil
xor    %eax,%eax
call   1050 <assert@plt>
mov    0x4(%rsp),%esi
lea    0xed3(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xfce(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
cmpl   $0x2a,0x4(%rsp)
sete   %dil
xor    %eax,%eax
call   1050 <assert@plt>
mov    0x4(%rsp),%esi
lea    0xef5(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xfd6(%rip),%rdi
call   1030 <puts@plt>
movss  0x4(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xf02(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
pop    %rax
ret

LLVM IR (-O0):
@.str = [27 x i8] c"\0A===== test_void_pointers\0A\00"
@.str.1 = [72 x i8] c"Set void pointer to the address of an int: `void *p = &i`. Address: %p\0A\00"
@.str.2 = [82 x i8] c"Casting a void pointer to assumed type and dereferencing: `*(int*)p`. Returns %d\0A\00"
@.str.3 = [57 x i8] c"Set int pointer equal to void pointer: `int *int_p = p`\0A\00"
@.str.4 = [49 x i8] c"Dereferencing int pointer: `*int_p`. Returns %d\0A\00"
@.str.5 = [109 x i8] c"But would not stop you setting float pointer equal to void pointer that points to int: `float *float_p = p`\0A\00"
@.str.6 = [108 x i8] c"Dereferencing the float pointer doesn't blow up (since float and int both 4 bytes), but value is wrong: %f\0A\00"
define void @test_void_pointers() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca float*
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i32 42, i32* %1
%6 = bitcast i32* %1 to i8*
store i8* %6, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%9 = load i8*, i8** %2
%10 = bitcast i8* %9 to i32*
%11 = load i32, i32* %10
%12 = icmp eq i32 %11, 42
%13 = zext i1 %12 to i32
%14 = call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %13)
%15 = load i8*, i8** %2
%16 = bitcast i8* %15 to i32*
%17 = load i32, i32* %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = load i8*, i8** %2
%20 = bitcast i8* %19 to i32*
store i32* %20, i32** %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32*, i32** %3
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, 42
%25 = zext i1 %24 to i32
%26 = call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %25)
%27 = load i32*, i32** %3
%28 = load i32, i32* %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = load i8*, i8** %2
%31 = bitcast i8* %30 to float*
store float* %31, float** %4
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([109 x i8], [109 x i8]* @.str.5, i64 0, i64 0))
%33 = load float*, float** %4
%34 = load float, float* %33
%35 = fpext float %34 to double
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([108 x i8], [108 x i8]* @.str.6, i64 0, i64 0), double %35)
ret void
}

LLVM IR (-O2):
@.str.1 = [72 x i8] c"Set void pointer to the address of an int: `void *p = &i`. Address: %p\0A\00"
@.str.2 = [82 x i8] c"Casting a void pointer to assumed type and dereferencing: `*(int*)p`. Returns %d\0A\00"
@.str.4 = [49 x i8] c"Dereferencing int pointer: `*int_p`. Returns %d\0A\00"
@.str.6 = [108 x i8] c"Dereferencing the float pointer doesn't blow up (since float and int both 4 bytes), but value is wrong: %f\0A\00"
@str = [26 x i8] c"\0A===== test_void_pointers\00"
@str.7 = [56 x i8] c"Set int pointer equal to void pointer: `int *int_p = p`\00"
@str.8 = [108 x i8] c"But would not stop you setting float pointer equal to void pointer that points to int: `float *float_p = p`\00"
define void @test_void_pointers() {
%1 = alloca i32
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%3 = bitcast i32* %1 to i8*
store i32 42, i32* %1
%4 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%5 = load i32, i32* %1
%6 = icmp eq i32 %5, 42
%7 = zext i1 %6 to i32
%8 = call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %7)
%9 = load i32, i32* %1
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([82 x i8], [82 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str.7, i64 0, i64 0))
%12 = load i32, i32* %1
%13 = icmp eq i32 %12, 42
%14 = zext i1 %13 to i32
%15 = call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %14)
%16 = load i32, i32* %1
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i32 %16)
%18 = bitcast i32* %1 to float*
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([108 x i8], [108 x i8]* @str.8, i64 0, i64 0))
%20 = load float, float* %18
%21 = fpext float %20 to double
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([108 x i8], [108 x i8]* @.str.6, i64 0, i64 0), double %21)
ret void
}