#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 ptrdiff_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int);
int main(void) {
  typedef struct {
    int a;
    char b;
    int c;
  } triplet;
  printf("sizeof(triplet) = %zu\n", sizeof(triplet));
  printf("Sum of each element size = %zu\n", 2 * sizeof(int) + sizeof(char));
  triplet x;
  ptrdiff_t aLoc = (ptrdiff_t) & (x.a);
  ptrdiff_t bLoc = (ptrdiff_t) & (x.b);
  ptrdiff_t cLoc = (ptrdiff_t) & (x.c);
  printf("Bytes between start of 'a' and start of 'b': %td\n", bLoc - aLoc);
  printf("Bytes between start of 'b' and start of 'c': %td\n", cLoc - bLoc);
#pragma pack(push)
#pragma pack(1)
  typedef struct {
    int a;
    char b;
    int c;
  } packed_triplet;
#pragma pack(pop)
  printf("Packed struct: %zu\n", sizeof(packed_triplet));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeda(%rip),%rdi
mov    $0xc,%esi
call   1030 <printf@plt>
lea    0xee0(%rip),%rdi
mov    $0x9,%esi
call   1030 <printf@plt>
lea    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
lea    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x18(%rbp)
lea    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%esi
sub    -0x14(%rbp),%esi
lea    0xecc(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%esi
sub    -0x18(%rbp),%esi
lea    0xeec(%rip),%rdi
call   1030 <printf@plt>
lea    0xf12(%rip),%rdi
mov    $0x9,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xee8(%rip),%rdi
mov    $0xc,%esi
call   1030 <printf@plt>
lea    0xeee(%rip),%rdi
mov    $0x9,%esi
call   1030 <printf@plt>
lea    0xefd(%rip),%rdi
mov    $0x4,%esi
call   1030 <printf@plt>
lea    0xf1e(%rip),%rdi
mov    $0x4,%esi
call   1030 <printf@plt>
lea    0xf3f(%rip),%rdi
mov    $0x9,%esi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.triplet = type { i32, i8, i32 }
@.str = [23 x i8] c"sizeof(triplet) = %zu\0A\00"
@.str.1 = [32 x i8] c"Sum of each element size = %zu\0A\00"
@.str.2 = [50 x i8] c"Bytes between start of 'a' and start of 'b': %td\0A\00"
@.str.3 = [50 x i8] c"Bytes between start of 'b' and start of 'c': %td\0A\00"
@.str.4 = [20 x i8] c"Packed struct: %zu\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.triplet
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 12)
%7 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 9)
%8 = getelementptr inbounds %struct.triplet, %struct.triplet* %2, i32 0, i32 0
%9 = ptrtoint i32* %8 to i32
store i32 %9, i32* %3
%10 = getelementptr inbounds %struct.triplet, %struct.triplet* %2, i32 0, i32 1
%11 = ptrtoint i8* %10 to i32
store i32 %11, i32* %4
%12 = getelementptr inbounds %struct.triplet, %struct.triplet* %2, i32 0, i32 2
%13 = ptrtoint i32* %12 to i32
store i32 %13, i32* %5
%14 = load i32, i32* %4
%15 = load i32, i32* %3
%16 = sub nsw i32 %14, %15
%17 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %5
%19 = load i32, i32* %4
%20 = sub nsw i32 %18, %19
%21 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 9)
ret i32 0
}

LLVM IR (-O2):
%struct.triplet = type { i32, i8, i32 }
@.str = [23 x i8] c"sizeof(triplet) = %zu\0A\00"
@.str.1 = [32 x i8] c"Sum of each element size = %zu\0A\00"
@.str.2 = [50 x i8] c"Bytes between start of 'a' and start of 'b': %td\0A\00"
@.str.3 = [50 x i8] c"Bytes between start of 'b' and start of 'c': %td\0A\00"
@.str.4 = [20 x i8] c"Packed struct: %zu\0A\00"
define i32 @main() {
%1 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 12)
%2 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 9)
%3 = tail call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i32 4)
%4 = tail call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i32 4)
%5 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 9)
ret i32 0
}