#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 */
typedef int uint8_t;
struct dns_hdr {
  int dummy;
};

/* Variables and functions */
const uint8_t *dns_name_ptr(const struct dns_hdr *hdr, const uint8_t *p) {
  const uint8_t *target;
  if (!hdr)
    return NULL;
  if ((p[0] & 0xc0) != 0xc0)
    return NULL;
  target = (const uint8_t *)hdr + ((p[0] & 0x3f) << 8) + (p[1] & 0xff);
  if ((target >= p) || (target[0] & 0xc0))
    return NULL;
  return target;
}


x86 assembly (clang, -O0):
<dns_name_ptr>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1124 <dns_name_ptr+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11b3 <dns_name_ptr+0xb3>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
and    $0xc0,%eax
cmp    $0xc0,%eax
je     1147 <dns_name_ptr+0x47>
movq   $0x0,-0x8(%rbp)
jmp    11b3 <dns_name_ptr+0xb3>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%ecx
and    $0x3f,%ecx
shl    $0x8,%ecx
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    -0x18(%rbp),%rcx
mov    0x4(%rcx),%ecx
and    $0xff,%ecx
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    119e <dns_name_ptr+0x9e>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
and    $0xc0,%eax
cmp    $0x0,%eax
je     11ab <dns_name_ptr+0xab>
movq   $0x0,-0x8(%rbp)
jmp    11b3 <dns_name_ptr+0xb3>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<dns_name_ptr>:
test   %rdi,%rdi
je     1132 <dns_name_ptr+0x32>
mov    (%rsi),%eax
mov    %eax,%ecx
not    %ecx
test   $0xc0,%cl
jne    1132 <dns_name_ptr+0x32>
and    $0x3f,%eax
shl    $0x8,%rax
lea    (%rdi,%rax,4),%rax
movzbl 0x4(%rsi),%ecx
lea    (%rax,%rcx,4),%rcx
xor    %eax,%eax
cmp    %rsi,%rcx
jae    1131 <dns_name_ptr+0x31>
testb  $0xc0,(%rcx)
cmove  %rcx,%rax
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.dns_hdr = type { i32 }
define i32* @dns_name_ptr(%struct.dns_hdr* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca %struct.dns_hdr*
%5 = alloca i32*
%6 = alloca i32*
store %struct.dns_hdr* %0, %struct.dns_hdr** %4
store i32* %1, i32** %5
%7 = load %struct.dns_hdr*, %struct.dns_hdr** %4
%8 = icmp ne %struct.dns_hdr* %7, null
br i1 %8, label %10, label %9
9: ; preds = %2
store i32* null, i32** %3
br label %45
10: ; preds = %2
%11 = load i32*, i32** %5
%12 = getelementptr inbounds i32, i32* %11, i64 0
%13 = load i32, i32* %12
%14 = and i32 %13, 192
%15 = icmp ne i32 %14, 192
br i1 %15, label %16, label %17
16: ; preds = %10
store i32* null, i32** %3
br label %45
17: ; preds = %10
%18 = load %struct.dns_hdr*, %struct.dns_hdr** %4
%19 = bitcast %struct.dns_hdr* %18 to i32*
%20 = load i32*, i32** %5
%21 = getelementptr inbounds i32, i32* %20, i64 0
%22 = load i32, i32* %21
%23 = and i32 %22, 63
%24 = shl i32 %23, 8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %19, i64 %25
%27 = load i32*, i32** %5
%28 = getelementptr inbounds i32, i32* %27, i64 1
%29 = load i32, i32* %28
%30 = and i32 %29, 255
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %26, i64 %31
store i32* %32, i32** %6
%33 = load i32*, i32** %6
%34 = load i32*, i32** %5
%35 = icmp uge i32* %33, %34
br i1 %35, label %42, label %36
36: ; preds = %17
%37 = load i32*, i32** %6
%38 = getelementptr inbounds i32, i32* %37, i64 0
%39 = load i32, i32* %38
%40 = and i32 %39, 192
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36, %17
store i32* null, i32** %3
br label %45
43: ; preds = %36
%44 = load i32*, i32** %6
store i32* %44, i32** %3
br label %45
45: ; preds = %43, %42, %16, %9
%46 = load i32*, i32** %3
ret i32* %46
}

LLVM IR (-O2):
%struct.dns_hdr = type { i32 }
define i32* @dns_name_ptr(%struct.dns_hdr* %0, i32* %1) {
%3 = icmp eq %struct.dns_hdr* %0, null
br i1 %3, label %24, label %4
4: ; preds = %2
%5 = load i32, i32* %1
%6 = and i32 %5, 192
%7 = icmp eq i32 %6, 192
br i1 %7, label %8, label %24
8: ; preds = %4
%9 = shl i32 %5, 8
%10 = and i32 %9, 16128
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds %struct.dns_hdr, %struct.dns_hdr* %0, i64 %11, i32 0
%13 = getelementptr inbounds i32, i32* %1, i64 1
%14 = load i32, i32* %13
%15 = and i32 %14, 255
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i32, i32* %12, i64 %16
%18 = icmp ult i32* %17, %1
br i1 %18, label %19, label %24
19: ; preds = %8
%20 = load i32, i32* %17
%21 = and i32 %20, 192
%22 = icmp eq i32 %21, 0
%23 = select i1 %22, i32* %17, i32* null
br label %24
24: ; preds = %19, %8, %4, %2
%25 = phi i32* [ null, %2 ], [ null, %4 ], [ null, %8 ], [ %23, %19 ]
ret i32* %25
}