void union_element(int *p_array, int size, int p, int q) {
  if (!p_array) {
    printf("function:%s line: %d p_array is NULL\n", __FUNCTION__, 114);
    return;
  }
  if (p > (size - 1) || q > (size - 1)) {
    printf("index 'p' 'q' are bigger than the max size of array\n");
    printf("p: %3d q: %3d", p, q);
    return;
  }
  int p_union = p_array[p];
  int q_union = p_array[q];
  int tmp = 0;
  p_array[p] = q_union;
}


x86 assembly (clang, -O0):
<union_element>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    114f <union_element+0x3f>
lea    0xec9(%rip),%rdi
lea    0xee8(%rip),%rsi
mov    $0x72,%edx
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11c9 <union_element+0xb9>
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jg     1171 <union_element+0x61>
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    1198 <union_element+0x88>
lea    0xebc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xedd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11c9 <union_element+0xb9>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<union_element>:
push   %rbp
push   %rbx
push   %rax
test   %rdi,%rdi
je     1147 <union_element+0x27>
mov    %ecx,%ebx
mov    %edx,%ebp
cmp    %edx,%esi
jle    1167 <union_element+0x47>
cmp    %ebx,%esi
jle    1167 <union_element+0x47>
movslq %ebp,%rax
movslq %ebx,%rcx
mov    (%rdi,%rcx,4),%ecx
mov    %ecx,(%rdi,%rax,4)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xeb2(%rip),%rdi
lea    0xed1(%rip),%rsi
mov    $0x72,%edx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <printf@plt>
lea    0xed4(%rip),%rdi
call   1030 <puts@plt>
lea    0xeba(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [38 x i8] c"function:%s line: %d p_array is NULL\0A\00"
@__FUNCTION__.union_element = [14 x i8] c"union_element\00"
@.str.1 = [53 x i8] c"index 'p' 'q' are bigger than the max size of array\0A\00"
@.str.2 = [14 x i8] c"p: %3d q: %3d\00"
define void @union_element(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i32*, i32** %5
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %4
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__FUNCTION__.union_element, i64 0, i64 0), i32 114)
br label %47
16: ; preds = %4
%17 = load i32, i32* %7
%18 = load i32, i32* %6
%19 = sub nsw i32 %18, 1
%20 = icmp sgt i32 %17, %19
br i1 %20, label %26, label %21
21: ; preds = %16
%22 = load i32, i32* %8
%23 = load i32, i32* %6
%24 = sub nsw i32 %23, 1
%25 = icmp sgt i32 %22, %24
br i1 %25, label %26, label %31
26: ; preds = %21, %16
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* %7
%29 = load i32, i32* %8
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %28, i32 %29)
br label %47
31: ; preds = %21
%32 = load i32*, i32** %5
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
%36 = load i32, i32* %35
store i32 %36, i32* %9
%37 = load i32*, i32** %5
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %37, i64 %39
%41 = load i32, i32* %40
store i32 %41, i32* %10
store i32 0, i32* %11
%42 = load i32, i32* %10
%43 = load i32*, i32** %5
%44 = load i32, i32* %7
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
store i32 %42, i32* %46
br label %47
47: ; preds = %31, %26, %14
ret void
}

LLVM IR (-O2):
@.str = [38 x i8] c"function:%s line: %d p_array is NULL\0A\00"
@__FUNCTION__.union_element = [14 x i8] c"union_element\00"
@.str.2 = [14 x i8] c"p: %3d q: %3d\00"
@str = [52 x i8] c"index 'p' 'q' are bigger than the max size of array\00"
define void @union_element(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp eq i32* %0, null
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__FUNCTION__.union_element, i64 0, i64 0), i32 114)
br label %21
8: ; preds = %4
%9 = icmp sgt i32 %1, %2
%10 = icmp sgt i32 %1, %3
%11 = and i1 %9, %10
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %2, i32 %3)
br label %21
15: ; preds = %8
%16 = sext i32 %2 to i64
%17 = getelementptr inbounds i32, i32* %0, i64 %16
%18 = sext i32 %3 to i64
%19 = getelementptr inbounds i32, i32* %0, i64 %18
%20 = load i32, i32* %19
store i32 %20, i32* %17
br label %21
21: ; preds = %15, %12, %6
ret void
}