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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
scalar_t__ inet_pton(int /*<<< orphan*/, char *, void *);
int /*<<< orphan*/ memmove(char *, char *, int);
int strlen(char *);
char *chop_lvl2_domain(char *domname) {
  int i;
  int len, n;
  unsigned char unusedbuf[sizeof(struct in6_addr)];
  if (inet_pton(AF_INET, domname, (void *)unusedbuf) > 0 ||
      inet_pton(AF_INET6, domname, (void *)unusedbuf) > 0)
    return domname;
  n = 0;
  len = strlen(domname);
  for (i = len; i >= 0; i--) {
    if (domname[i] == '.') {
      n++;
      if (n == 2)
        break;
    }
  }
  if (i > 0)
    memmove(domname, domname + i + 1, len - i - 1);
  return domname;
}


x86 assembly (clang, -O0):
<chop_lvl2_domain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <inet_pton@plt>
cmp    $0x0,%rax
jg     117c <chop_lvl2_domain+0x4c>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <inet_pton@plt>
cmp    $0x0,%rax
jle    1189 <chop_lvl2_domain+0x59>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1224 <chop_lvl2_domain+0xf4>
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11f1 <chop_lvl2_domain+0xc1>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
jne    11de <chop_lvl2_domain+0xae>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jne    11d9 <chop_lvl2_domain+0xa9>
jmp    11f1 <chop_lvl2_domain+0xc1>
jmp    11de <chop_lvl2_domain+0xae>
jmp    11e3 <chop_lvl2_domain+0xb3>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11a2 <chop_lvl2_domain+0x72>
cmpl   $0x0,-0x14(%rbp)
jle    121c <chop_lvl2_domain+0xec>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%edx
sub    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1050 <memmove@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chop_lvl2_domain>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rdx
mov    %rbx,%rsi
call   1040 <inet_pton@plt>
test   %rax,%rax
jg     116e <chop_lvl2_domain+0x3e>
mov    0x2e86(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rdx
mov    %rbx,%rsi
call   1040 <inet_pton@plt>
test   %rax,%rax
jle    1177 <chop_lvl2_domain+0x47>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
js     116e <chop_lvl2_domain+0x3e>
mov    %eax,%eax
xor    %ecx,%ecx
mov    $0xffffffff,%edx
jmp    119f <chop_lvl2_domain+0x6f>
xchg   %ax,%ax
lea    -0x1(%rax),%rsi
add    $0x1,%edx
test   %rax,%rax
mov    %rsi,%rax
jle    116e <chop_lvl2_domain+0x3e>
cmpb   $0x2e,(%rbx,%rax,1)
jne    1190 <chop_lvl2_domain+0x60>
cmp    $0x1,%ecx
mov    $0x1,%ecx
jne    1190 <chop_lvl2_domain+0x60>
test   %eax,%eax
je     116e <chop_lvl2_domain+0x3e>
mov    %eax,%eax
lea    (%rbx,%rax,1),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
call   1050 <memmove@plt>
jmp    116e <chop_lvl2_domain+0x3e>

LLVM IR (-O0):
@AF_INET = global i32 0
@AF_INET6 = global i32 0
define i8* @chop_lvl2_domain(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4 x i8]
store i8* %0, i8** %3
%8 = load i32, i32* @AF_INET
%9 = load i8*, i8** %3
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%11 = call i64 @inet_pton(i32 %8, i8* %9, i8* %10)
%12 = icmp sgt i64 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %1
%14 = load i32, i32* @AF_INET6
%15 = load i8*, i8** %3
%16 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%17 = call i64 @inet_pton(i32 %14, i8* %15, i8* %16)
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13, %1
%20 = load i8*, i8** %3
store i8* %20, i8** %2
br label %64
21: ; preds = %13
store i32 0, i32* %6
%22 = load i8*, i8** %3
%23 = call i32 @strlen(i8* %22)
store i32 %23, i32* %5
%24 = load i32, i32* %5
store i32 %24, i32* %4
br label %25
25: ; preds = %44, %21
%26 = load i32, i32* %4
%27 = icmp sge i32 %26, 0
br i1 %27, label %28, label %47
28: ; preds = %25
%29 = load i8*, i8** %3
%30 = load i32, i32* %4
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 46
br i1 %35, label %36, label %43
36: ; preds = %28
%37 = load i32, i32* %6
%38 = add nsw i32 %37, 1
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp eq i32 %39, 2
br i1 %40, label %41, label %42
41: ; preds = %36
br label %47
42: ; preds = %36
br label %43
43: ; preds = %42, %28
br label %44
44: ; preds = %43
%45 = load i32, i32* %4
%46 = add nsw i32 %45, -1
store i32 %46, i32* %4
br label %25
47: ; preds = %41, %25
%48 = load i32, i32* %4
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %62
50: ; preds = %47
%51 = load i8*, i8** %3
%52 = load i8*, i8** %3
%53 = load i32, i32* %4
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = getelementptr inbounds i8, i8* %55, i64 1
%57 = load i32, i32* %5
%58 = load i32, i32* %4
%59 = sub nsw i32 %57, %58
%60 = sub nsw i32 %59, 1
%61 = call i32 @memmove(i8* %51, i8* %56, i32 %60)
br label %62
62: ; preds = %50, %47
%63 = load i8*, i8** %3
store i8* %63, i8** %2
br label %64
64: ; preds = %62, %19
%65 = load i8*, i8** %2
ret i8* %65
}

LLVM IR (-O2):
@AF_INET = global i32 0
@AF_INET6 = global i32 0
define i8* @chop_lvl2_domain(i8* returned %0) {
%2 = alloca [4 x i8]
%3 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @AF_INET
%5 = call i64 @inet_pton(i32 %4, i8* %0, i8* %3)
%6 = icmp sgt i64 %5, 0
br i1 %6, label %38, label %7
7: ; preds = %1
%8 = load i32, i32* @AF_INET6
%9 = call i64 @inet_pton(i32 %8, i8* %0, i8* %3)
%10 = icmp sgt i64 %9, 0
br i1 %10, label %38, label %11
11: ; preds = %7
%12 = call i32 @strlen(i8* %0)
%13 = icmp sgt i32 %12, -1
br i1 %13, label %14, label %38
14: ; preds = %11
%15 = zext i32 %12 to i64
br label %16
16: ; preds = %14, %24
%17 = phi i64 [ %15, %14 ], [ %26, %24 ]
%18 = phi i32 [ 0, %14 ], [ %25, %24 ]
%19 = getelementptr inbounds i8, i8* %0, i64 %17
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 46
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = icmp eq i32 %18, 1
br i1 %23, label %28, label %24
24: ; preds = %16, %22
%25 = phi i32 [ 1, %22 ], [ %18, %16 ]
%26 = add nsw i64 %17, -1
%27 = icmp sgt i64 %17, 0
br i1 %27, label %16, label %38
28: ; preds = %22
%29 = trunc i64 %17 to i32
%30 = icmp eq i32 %29, 0
br i1 %30, label %38, label %31
31: ; preds = %28
%32 = and i64 %17, 4294967295
%33 = getelementptr inbounds i8, i8* %0, i64 %32
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = xor i32 %29, -1
%36 = add i32 %12, %35
%37 = call i32 @memmove(i8* %0, i8* %34, i32 %36)
br label %38
38: ; preds = %24, %11, %28, %31, %1, %7
ret i8* %0
}