// #target: macos-x64

module foo;

bitstruct BitField : char
{
  int a : 0..2;
  int b : 4..5;
  int c : 6..7;
}

bitstruct BitFieldI : ushort
{
  int a : 0..4;
  int b : 5..10;
  int c : 11..15;
}


struct Packet
{
  bitstruct : int
  {
    int a : 0..2;
    int b : 3..5;
    int c : 6..10;
  }
  int packet_id;
}

bitstruct BitField3 : char[3]
{
  int a : 0..2;
  int b : 3..8;
  int c : 9..18;
  int d : 19..23;
}

bitstruct BitField3u : char[3]
{
  uint a : 0..2;
  uint b : 3..8;
  uint c : 9..18;
  uint d : 19..23;
}

bitstruct BitField4 : char[3]
{
  int a : 0..2;
  int b : 3..7;
  int c : 8..15;
  int d : 16..19;
}

bitstruct BitFieldCross : char[5]
{
    uint a : 5..22;
}

BitField c = { 2, -1, 1 };

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

fn void main()
{
    BitField b = { 3, -1, 1 };
    BitFieldI c1 = { 5, 0, 0 };
    BitFieldI c2 = { 0, 3, 0 };
    BitFieldI c3 = { 0, 0, 9 };
    BitFieldI c4 = { -5, 7, 9 };
    BitFieldI c5 = { -5, 0, 0 };
    BitFieldI c6 = { 5, 0, 0 };
    BitFieldI c7 = { 0, 0, 5 };

    BitField3 e1 = { 3, 1, 3, 4 };
    BitField3 e2 = { 1, 1, 3, 4 };
    BitField3 e3 = { 2, 1, 3, 4 };
    BitField d = { };

    printf("%d\n", e1.a);
    printf("%d\n", e2.a);
    printf("%d\n", e3.a);

    BitField3u z1 = { 3, 1, 3, 4 };
    BitField3u z2 = { 4, 1, 3, 4 };
    BitField3u z3 = { 7, 1, 3, 4 };
    printf("%u\n", z1.a);
    printf("%u\n", z2.a);
    printf("%u\n", z3.a);
    BitFieldCross xx = { 0x1238 };
    printf("%x\n", xx.a);
}

/* #expect: foo.ll

define void @foo.main() #0 {
entry:
  %b = alloca i8, align 1
  %c1 = alloca i16, align 2
  %c2 = alloca i16, align 2
  %c3 = alloca i16, align 2
  %c4 = alloca i16, align 2
  %c5 = alloca i16, align 2
  %c6 = alloca i16, align 2
  %c7 = alloca i16, align 2
  %e1 = alloca [3 x i8], align 1
  %e2 = alloca [3 x i8], align 1
  %e3 = alloca [3 x i8], align 1
  %d = alloca i8, align 1
  %z1 = alloca [3 x i8], align 1
  %z2 = alloca [3 x i8], align 1
  %z3 = alloca [3 x i8], align 1
  %xx = alloca [5 x i8], align 1
  store i8 115, ptr %b, align 1
  store i16 5, ptr %c1, align 2
  store i16 96, ptr %c2, align 2
  store i16 18432, ptr %c3, align 2
  store i16 18683, ptr %c4, align 2
  store i16 27, ptr %c5, align 2
  store i16 5, ptr %c6, align 2
  store i16 10240, ptr %c7, align 2
  store [3 x i8] c"\0B\06 ", ptr %e1, align 1
  store [3 x i8] c"\09\06 ", ptr %e2, align 1
  store [3 x i8] c"\0A\06 ", ptr %e3, align 1
  store i8 0, ptr %d, align 1
  %0 = load i8, ptr %e1, align 1
  %zext = zext i8 %0 to i32
  %shl = shl i32 %zext, 29
  %ashr = ashr i32 %shl, 29
  call void (ptr, ...) @printf(ptr @.str, i32 %ashr)
  %1 = load i8, ptr %e2, align 1
  %zext1 = zext i8 %1 to i32
  %shl2 = shl i32 %zext1, 29
  %ashr3 = ashr i32 %shl2, 29
  call void (ptr, ...) @printf(ptr @.str.1, i32 %ashr3)
  %2 = load i8, ptr %e3, align 1
  %zext4 = zext i8 %2 to i32
  %shl5 = shl i32 %zext4, 29
  %ashr6 = ashr i32 %shl5, 29
  call void (ptr, ...) @printf(ptr @.str.2, i32 %ashr6)
  store [3 x i8] c"\0B\06 ", ptr %z1, align 1
  store [3 x i8] c"\0C\06 ", ptr %z2, align 1
  store [3 x i8] c"\0F\06 ", ptr %z3, align 1
  %3 = load i8, ptr %z1, align 1
  %zext7 = zext i8 %3 to i32
  %4 = and i32 7, %zext7
  call void (ptr, ...) @printf(ptr @.str.3, i32 %4)
  %5 = load i8, ptr %z2, align 1
  %zext8 = zext i8 %5 to i32
  %6 = and i32 7, %zext8
  call void (ptr, ...) @printf(ptr @.str.4, i32 %6)
  %7 = load i8, ptr %z3, align 1
  %zext9 = zext i8 %7 to i32
  %8 = and i32 7, %zext9
  call void (ptr, ...) @printf(ptr @.str.5, i32 %8)
  store [5 x i8] c"\00G\02\00\00", ptr %xx, align 1
  %9 = load i8, ptr %xx, align 1
  %zext10 = zext i8 %9 to i32
  %lshrl = lshr i32 %zext10, 5
  %ptradd = getelementptr inbounds i8, ptr %xx, i64 1
  %10 = load i8, ptr %ptradd, align 1
  %zext11 = zext i8 %10 to i32
  %shl12 = shl i32 %zext11, 3
  %11 = or i32 %shl12, %lshrl
  %ptradd13 = getelementptr inbounds i8, ptr %xx, i64 2
  %12 = load i8, ptr %ptradd13, align 1
  %zext14 = zext i8 %12 to i32
  %shl15 = shl i32 %zext14, 11
  %13 = or i32 %shl15, %11
  %14 = and i32 262143, %13
  call void (ptr, ...) @printf(ptr @.str.6, i32 %14)
  ret void
}