// #target: macos-x64

module foo;

extern fn void printf(char*, ...);

bitstruct Foo : uint
{
    int x : 1..3;
    uint y : 11..13;
    int z : 15..15;
}

bitstruct Foo2 : char[4]
{
    int x : 1..3;
    uint y : 11..13;
    int z : 15..15;
}

fn void test()
{
    Foo b = {};
    int x = (int)b;
    char[4] y = bitcast(b, char[4]);
    Foo *c = &b;
    c.x;
    int* x2 = (int*)c;
    char[4]* y2 = (char[4]*)c;
}

fn void test2()
{
    Foo2 b = { 3, 2, -1 };
    int x = bitcast(b, int);
    char[4] y = (char[4])b;
    Foo2 *c = &b;
    printf("%d\n", c.x);
    int* x2 = (int*)c;
    char[4]* y2 = (char[4]*)c;
}

fn void main()
{
    test();
    test2();
}

/* #expect: foo.ll

define void @foo.test() #0 {
entry:
  %b = alloca i32, align 4
  %x = alloca i32, align 4
  %y = alloca [4 x i8], align 1
  %expr = alloca i32, align 4
  %c = alloca ptr, align 8
  %x2 = alloca ptr, align 8
  %y2 = alloca ptr, align 8
  store i32 0, ptr %b, align 4
  %0 = load i32, ptr %b, align 4
  store i32 %0, ptr %x, align 4
  %1 = load i32, ptr %b, align 4
  store i32 %1, ptr %expr, align 4
  call void @llvm.memcpy.p0.p0.i32(ptr align 1 %y, ptr align 1 %expr, i32 4, i1 false)
  store ptr %b, ptr %c, align 8
  %2 = load ptr, ptr %c, align 8
  %3 = load i32, ptr %2, align 4
  %shl = shl i32 %3, 28
  %ashr = ashr i32 %shl, 29
  %4 = load ptr, ptr %c, align 8
  store ptr %4, ptr %x2, align 8
  %5 = load ptr, ptr %c, align 8
  store ptr %5, ptr %y2, align 8
  ret void
}

; Function Attrs:
define void @foo.test2() #0 {
entry:
  %b = alloca [4 x i8], align 1
  %x = alloca i32, align 4
  %expr = alloca [4 x i8], align 1
  %x1 = alloca i32, align 4
  %y = alloca [4 x i8], align 1
  %c = alloca ptr, align 8
  %x2 = alloca ptr, align 8
  %y2 = alloca ptr, align 8
  store [4 x i8] c"\06\90\00\00", ptr %b, align 1
  call void @llvm.memcpy.p0.p0.i32(ptr align 1 %expr, ptr align 1 %b, i32 4, i1 false)
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %x1, ptr align 1 %expr, i64 4, i1 false)
  %0 = load i32, ptr %x1, align 4
  store i32 %0, ptr %x, align 4
  call void @llvm.memcpy.p0.p0.i32(ptr align 1 %y, ptr align 1 %b, i32 4, i1 false)
  store ptr %b, ptr %c, align 8
  %1 = load ptr, ptr %c, align 8
  %2 = load i8, ptr %1, align 1
  %zext = zext i8 %2 to i32
  %lshrl = lshr i32 %zext, 1
  %shl = shl i32 %lshrl, 29
  %ashr = ashr i32 %shl, 29
  call void (ptr, ...) @printf(ptr @.str, i32 %ashr)
  %3 = load ptr, ptr %c, align 8
  store ptr %3, ptr %x2, align 8
  %4 = load ptr, ptr %c, align 8
  store ptr %4, ptr %y2, align 8
  ret void
}