#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 */
scalar_t__ big_endian();
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  union {
    short s;
    char c[sizeof(short)];
  } un;
  un.s = 0x0102;
  if (sizeof(short) == 2) {
    if (un.c[0] == 1 && un.c[1] == 2) {
      printf("big-endian\n");
    } else if (un.c[0] == 2 && un.c[1] == 1) {
      printf("little-endian\n");
    } else {
      printf("unknown\n");
    }
    int left = un.s >> 8;
    int right = un.s & 0xFF;
    printf("left  : %d \n", left);
    printf("right : %d \n", right);
  } else {
    printf("sizeof(short) = %d\n", sizeof(short));
  }
  if (big_endian()) {
    printf("big-endian");
  } else {
    printf("little-endian");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movw   $0x102,-0x8(%rbp)
movsbl -0x8(%rbp),%eax
cmp    $0x1,%eax
jne    1162 <main+0x42>
movsbl -0x7(%rbp),%eax
cmp    $0x2,%eax
jne    1162 <main+0x42>
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11a2 <main+0x82>
movsbl -0x8(%rbp),%eax
cmp    $0x2,%eax
jne    118f <main+0x6f>
movsbl -0x7(%rbp),%eax
cmp    $0x1,%eax
jne    118f <main+0x6f>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    119d <main+0x7d>
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11a2 <main+0x82>
movswl -0x8(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0xc(%rbp)
movswl -0x8(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <big_endian@plt>
cmp    $0x0,%rax
je     11fe <main+0xde>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    120c <main+0xec>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xeed(%rip),%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
lea    0xeb9(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeb3(%rip),%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <big_endian@plt>
test   %rax,%rax
lea    0xea3(%rip),%rdi
cmove  %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%union.anon = type { i16 }
@.str = [12 x i8] c"big-endian\0A\00"
@.str.1 = [15 x i8] c"little-endian\0A\00"
@.str.2 = [9 x i8] c"unknown\0A\00"
@.str.3 = [13 x i8] c"left : %d \0A\00"
@.str.4 = [13 x i8] c"right : %d \0A\00"
@.str.5 = [11 x i8] c"big-endian\00"
@.str.6 = [14 x i8] c"little-endian\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %union.anon
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = bitcast %union.anon* %2 to i16*
store i16 258, i16* %5
%6 = bitcast %union.anon* %2 to [2 x i8]*
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %19
11: ; preds = %0
%12 = bitcast %union.anon* %2 to [2 x i8]*
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %12, i64 0, i64 1
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 2
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %36
19: ; preds = %11, %0
%20 = bitcast %union.anon* %2 to [2 x i8]*
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %20, i64 0, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 2
br i1 %24, label %25, label %33
25: ; preds = %19
%26 = bitcast %union.anon* %2 to [2 x i8]*
%27 = getelementptr inbounds [2 x i8], [2 x i8]* %26, i64 0, i64 1
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %35
33: ; preds = %25, %19
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %31
br label %36
36: ; preds = %35, %17
%37 = bitcast %union.anon* %2 to i16*
%38 = load i16, i16* %37
%39 = sext i16 %38 to i32
%40 = ashr i32 %39, 8
store i32 %40, i32* %3
%41 = bitcast %union.anon* %2 to i16*
%42 = load i16, i16* %41
%43 = sext i16 %42 to i32
%44 = and i32 %43, 255
store i32 %44, i32* %4
%45 = load i32, i32* %3
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %4
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %47)
%49 = call i64 (...) @big_endian()
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %36
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %55
53: ; preds = %36
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %53, %51
ret i32 0
}

LLVM IR (-O2):
@.str.3 = [13 x i8] c"left : %d \0A\00"
@.str.4 = [13 x i8] c"right : %d \0A\00"
@.str.5 = [11 x i8] c"big-endian\00"
@str = [14 x i8] c"little-endian\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 1)
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 2)
%4 = tail call i64 (...) @big_endian()
%5 = icmp eq i64 %4, 0
%6 = select i1 %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %6)
ret i32 0
}