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

/* Variables and functions */
int /*<<< orphan*/ WrMessage(char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
void WrMemoryAlignm(void) {
  struct _CharDoubleChar {
    char one_char;
    double one_double;
    char two_char;
  } CharDoubleChar;
  struct _DoubleCharChar {
    double one_double;
    char one_char;
    char two_char;
  } DoubleCharChar;
  char *pchar[3];
  long diff1, diff2;
  WrMessage("@MEMORY-ALIGNMENT");
  printf("Expected size (no alignment): %d\n",
         (int)(2 * sizeof(char) + sizeof(double)));
  printf("Structure 1 has size %d\n", (int)sizeof(CharDoubleChar));
  printf("Structure 2 has size %d\n", (int)sizeof(DoubleCharChar));
  printf("\n");
  pchar[0] = malloc(sizeof(char));
  pchar[1] = malloc(sizeof(char));
  pchar[2] = malloc(sizeof(char));
  diff1 = (long)pchar[1] - (long)pchar[0];
  diff2 = (long)pchar[2] - (long)pchar[1];
  WrMessage("@MEMORY-ALIGNMENT-POINTERS");
  if (diff1 == diff2) {
    printf("malloc() seems to return addresses that are multiples of %ld\n\n",
           diff1);
  } else {
    WrMessage("@MEMORY-ALIGNMENT-FAILED-MULT");
  }
  free(pchar[0]);
  free(pchar[1]);
  free(pchar[2]);
  return;
}


x86 assembly (clang, -O0):
<WrMemoryAlignm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    0xeb1(%rip),%rdi
call   1060 <WrMessage@plt>
lea    0xeb7(%rip),%rdi
mov    $0xa,%esi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xec6(%rip),%rdi
mov    $0x18,%esi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xecc(%rip),%rdi
mov    $0x10,%esi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xf2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x50(%rbp)
lea    0xe7c(%rip),%rdi
call   1060 <WrMessage@plt>
mov    -0x48(%rbp),%rax
cmp    -0x50(%rbp),%rax
jne    1214 <WrMemoryAlignm+0xd4>
mov    -0x48(%rbp),%rsi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1220 <WrMemoryAlignm+0xe0>
lea    0xea5(%rip),%rdi
call   1060 <WrMessage@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WrMemoryAlignm>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
lea    0xea0(%rip),%rdi
call   1070 <WrMessage@plt>
lea    0xea6(%rip),%rdi
mov    $0xa,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xeb5(%rip),%rdi
mov    $0x18,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xebb(%rip),%rdi
mov    $0x10,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rbx
sub    %r12,%rbx
mov    %r12,%r13
sub    %r14,%r13
lea    0xe84(%rip),%rdi
call   1070 <WrMessage@plt>
cmp    %rbx,%r13
jne    11ff <WrMemoryAlignm+0xaf>
lea    0xe8e(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    120b <WrMemoryAlignm+0xbb>
lea    0xeba(%rip),%rdi
call   1070 <WrMessage@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
%struct._CharDoubleChar = type { i8, double, i8 }
%struct._DoubleCharChar = type { double, i8, i8 }
@.str = [18 x i8] c"@MEMORY-ALIGNMENT\00"
@.str.1 = [34 x i8] c"Expected size (no alignment): %d\0A\00"
@.str.2 = [25 x i8] c"Structure 1 has size %d\0A\00"
@.str.3 = [25 x i8] c"Structure 2 has size %d\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [27 x i8] c"@MEMORY-ALIGNMENT-POINTERS\00"
@.str.6 = [63 x i8] c"malloc() seems to return addresses that are multiples of %ld\0A\0A\00"
@.str.7 = [30 x i8] c"@MEMORY-ALIGNMENT-FAILED-MULT\00"
define void @WrMemoryAlignm() {
%1 = alloca %struct._CharDoubleChar
%2 = alloca %struct._DoubleCharChar
%3 = alloca [3 x i8*]
%4 = alloca i64
%5 = alloca i64
%6 = call i32 @WrMessage(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 10)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 24)
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 16)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%11 = call i8* @malloc(i32 1)
%12 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
store i8* %11, i8** %12
%13 = call i8* @malloc(i32 1)
%14 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 1
store i8* %13, i8** %14
%15 = call i8* @malloc(i32 1)
%16 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 2
store i8* %15, i8** %16
%17 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 1
%18 = load i8*, i8** %17
%19 = ptrtoint i8* %18 to i64
%20 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
%21 = load i8*, i8** %20
%22 = ptrtoint i8* %21 to i64
%23 = sub nsw i64 %19, %22
store i64 %23, i64* %4
%24 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 2
%25 = load i8*, i8** %24
%26 = ptrtoint i8* %25 to i64
%27 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 1
%28 = load i8*, i8** %27
%29 = ptrtoint i8* %28 to i64
%30 = sub nsw i64 %26, %29
store i64 %30, i64* %5
%31 = call i32 @WrMessage(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%32 = load i64, i64* %4
%33 = load i64, i64* %5
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %0
%36 = load i64, i64* %4
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.6, i64 0, i64 0), i64 %36)
br label %40
38: ; preds = %0
%39 = call i32 @WrMessage(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
%41 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
%42 = load i8*, i8** %41
%43 = call i32 @free(i8* %42)
%44 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 1
%45 = load i8*, i8** %44
%46 = call i32 @free(i8* %45)
%47 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 2
%48 = load i8*, i8** %47
%49 = call i32 @free(i8* %48)
ret void
}

LLVM IR (-O2):
%struct._CharDoubleChar = type { i8, double, i8 }
%struct._DoubleCharChar = type { double, i8, i8 }
@.str = [18 x i8] c"@MEMORY-ALIGNMENT\00"
@.str.1 = [34 x i8] c"Expected size (no alignment): %d\0A\00"
@.str.2 = [25 x i8] c"Structure 1 has size %d\0A\00"
@.str.3 = [25 x i8] c"Structure 2 has size %d\0A\00"
@.str.5 = [27 x i8] c"@MEMORY-ALIGNMENT-POINTERS\00"
@.str.6 = [63 x i8] c"malloc() seems to return addresses that are multiples of %ld\0A\0A\00"
@.str.7 = [30 x i8] c"@MEMORY-ALIGNMENT-FAILED-MULT\00"
define void @WrMemoryAlignm() {
%1 = tail call i32 @WrMessage(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 10)
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 24)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 16)
%5 = tail call i32 @putchar(i32 10)
%6 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%7 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%8 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%9 = ptrtoint i8* %7 to i64
%10 = ptrtoint i8* %6 to i64
%11 = sub nsw i64 %9, %10
%12 = ptrtoint i8* %8 to i64
%13 = sub nsw i64 %12, %9
%14 = tail call i32 @WrMessage(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%15 = icmp eq i64 %11, %13
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([63 x i8], [63 x i8]* @.str.6, i64 0, i64 0), i64 %11)
br label %20
18: ; preds = %0
%19 = tail call i32 @WrMessage(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0))
br label %20
20: ; preds = %18, %16
%21 = tail call i32 @free(i8* %6)
%22 = tail call i32 @free(i8* %7)
%23 = tail call i32 @free(i8* %8)
ret void
}