#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 */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, char, ...);
int /*<<< orphan*/ puts(char *);
int main(int argc, char **argv) {
  union {
    short s;
    char c[sizeof(short)];
  } un;
  printf("short is %d\n", sizeof(short));
  un.s = 0x0102;
  if (un.c[0] == 1 && un.c[1] == 2) {
    puts("字节序为大端 big");
  } else if (un.c[0] == 2 && un.c[1] == 1) {
    puts("字节序为小端 small");
  } else {
    puts("字节序未知 un know");
  }
  short int x;
  char x0, x1;
  x = 0x1122;
  x0 = ((char *)&x)[0];
  x1 = ((char *)&x)[1];
  printf("x0=0x%x,x1=0x%x", x0, x1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xec3(%rip),%rdi
mov    $0x2,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movw   $0x102,-0x18(%rbp)
movsbl -0x18(%rbp),%eax
cmp    $0x1,%eax
jne    117a <main+0x5a>
movsbl -0x17(%rbp),%eax
cmp    $0x2,%eax
jne    117a <main+0x5a>
lea    0xe9d(%rip),%rdi
call   1030 <puts@plt>
jmp    11b6 <main+0x96>
movsbl -0x18(%rbp),%eax
cmp    $0x2,%eax
jne    11a5 <main+0x85>
movsbl -0x17(%rbp),%eax
cmp    $0x1,%eax
jne    11a5 <main+0x85>
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
jmp    11b1 <main+0x91>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
jmp    11b6 <main+0x96>
movw   $0x1122,-0x1a(%rbp)
mov    -0x1a(%rbp),%al
mov    %al,-0x1b(%rbp)
mov    -0x19(%rbp),%al
mov    %al,-0x1c(%rbp)
mov    -0x1b(%rbp),%al
movsbl -0x1c(%rbp),%edx
lea    0xe7f(%rip),%rdi
movsbl %al,%esi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xed8(%rip),%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xed2(%rip),%rdi
call   1030 <puts@plt>
lea    0xedf(%rip),%rdi
mov    $0x22,%esi
mov    $0x11,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
%union.anon = type { i16 }
@.str = [13 x i8] c"short is %d\0A\00"
@.str.1 = [23 x i8] c"\E5\AD\97\E8\8A\82\E5\BA\8F\E4\B8\BA\E5\A4\A7\E7\AB\AF big\00"
@.str.2 = [25 x i8] c"\E5\AD\97\E8\8A\82\E5\BA\8F\E4\B8\BA\E5\B0\8F\E7\AB\AF small\00"
@.str.3 = [24 x i8] c"\E5\AD\97\E8\8A\82\E5\BA\8F\E6\9C\AA\E7\9F\A5 un know\00"
@.str.4 = [16 x i8] c"x0=0x%x,x1=0x%x\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %union.anon
%7 = alloca i16
%8 = alloca i8
%9 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8 signext 2)
%11 = bitcast %union.anon* %6 to i16*
store i16 258, i16* %11
%12 = bitcast %union.anon* %6 to [2 x i8]*
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %12, i64 0, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %25
17: ; preds = %2
%18 = bitcast %union.anon* %6 to [2 x i8]*
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %18, i64 0, i64 1
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 2
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %42
25: ; preds = %17, %2
%26 = bitcast %union.anon* %6 to [2 x i8]*
%27 = getelementptr inbounds [2 x i8], [2 x i8]* %26, i64 0, i64 0
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 2
br i1 %30, label %31, label %39
31: ; preds = %25
%32 = bitcast %union.anon* %6 to [2 x i8]*
%33 = getelementptr inbounds [2 x i8], [2 x i8]* %32, i64 0, i64 1
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 1
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 @puts(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %41
39: ; preds = %31, %25
%40 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %37
br label %42
42: ; preds = %41, %23
store i16 4386, i16* %7
%43 = bitcast i16* %7 to i8*
%44 = getelementptr inbounds i8, i8* %43, i64 0
%45 = load i8, i8* %44
store i8 %45, i8* %8
%46 = bitcast i16* %7 to i8*
%47 = getelementptr inbounds i8, i8* %46, i64 1
%48 = load i8, i8* %47
store i8 %48, i8* %9
%49 = load i8, i8* %8
%50 = load i8, i8* %9
%51 = sext i8 %50 to i32
%52 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8 signext %49, i32 %51)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"short is %d\0A\00"
@.str.2 = [25 x i8] c"\E5\AD\97\E8\8A\82\E5\BA\8F\E4\B8\BA\E5\B0\8F\E7\AB\AF small\00"
@.str.4 = [16 x i8] c"x0=0x%x,x1=0x%x\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8 signext 2)
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%5 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8 signext 34, i32 17)
ret i32 0
}