#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 char *nis_name;
typedef int /*<<< orphan*/ const_nis_name;

/* Variables and functions */
int /*<<< orphan*/ ERANGE;
scalar_t__ __glibc_unlikely(int);
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
char *memcpy(char *, char *, size_t);
char *strchr(int /*<<< orphan*/, char);
char *strcpy(char *, char *);
size_t strlen(char *);
nis_name nis_domain_of_r(const_nis_name name, char *buffer, size_t buflen) {
  char *cptr;
  size_t cptr_len;
  if (buffer == NULL) {
  erange:
    __set_errno(ERANGE);
    return NULL;
  }
  buffer[0] = '\0';
  cptr = strchr(name, '.');
  if (cptr == NULL)
    return buffer;
  ++cptr;
  cptr_len = strlen(cptr);
  if (cptr_len == 0) {
    if (buflen < 2)
      goto erange;
    return strcpy(buffer, ".");
  }
  if (__glibc_unlikely(cptr_len >= buflen)) {
    __set_errno(ERANGE);
    return NULL;
  }
  return memcpy(buffer, cptr, cptr_len + 1);
}


x86 assembly (clang, -O0):
<nis_domain_of_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119e <nis_domain_of_r+0x3e>
jmp    1183 <nis_domain_of_r+0x23>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    126e <nis_domain_of_r+0x10e>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0xc(%rbp),%edi
mov    $0x2e,%esi
call   1060 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ce <nis_domain_of_r+0x6e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    126e <nis_domain_of_r+0x10e>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    121b <nis_domain_of_r+0xbb>
cmpq   $0x2,-0x20(%rbp)
jae    1202 <nis_domain_of_r+0xa2>
jmp    1183 <nis_domain_of_r+0x23>
mov    -0x18(%rbp),%rdi
lea    0xdf3(%rip),%rsi
call   1040 <strcpy@plt>
mov    %rax,-0x8(%rbp)
jmp    126e <nis_domain_of_r+0x10e>
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <__glibc_unlikely@plt>
cmp    $0x0,%rax
je     1255 <nis_domain_of_r+0xf5>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    126e <nis_domain_of_r+0x10e>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
add    $0x1,%rdx
call   1070 <memcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nis_domain_of_r>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     11b9 <nis_domain_of_r+0x69>
mov    %rdx,%r15
mov    %rsi,%rbx
movb   $0x0,(%rsi)
mov    $0x2e,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11c9 <nis_domain_of_r+0x79>
mov    %rax,%r14
add    $0x1,%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
je     11b3 <nis_domain_of_r+0x63>
mov    %rax,%r12
xor    %edi,%edi
cmp    %r15,%rax
setae  %dil
call   1070 <__glibc_unlikely@plt>
test   %rax,%rax
jne    11b9 <nis_domain_of_r+0x69>
add    $0x1,%r12
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1060 <memcpy@plt>
jmp    11c9 <nis_domain_of_r+0x79>
cmp    $0x2,%r15
jae    11d8 <nis_domain_of_r+0x88>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
movw   $0x2e,(%rbx)
jmp    11c9 <nis_domain_of_r+0x79>

LLVM IR (-O0):
@ERANGE = global i32 0
@.str = [2 x i8] c".\00"
define i8* @nis_domain_of_r(i32 %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %3
br label %13
13: ; preds = %35, %12
%14 = load i32, i32* @ERANGE
%15 = call i32 @__set_errno(i32 %14)
store i8* null, i8** %4
br label %55
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = getelementptr inbounds i8, i8* %17, i64 0
store i8 0, i8* %18
%19 = load i32, i32* %5
%20 = call i8* @strchr(i32 %19, i8 signext 46)
store i8* %20, i8** %8
%21 = load i8*, i8** %8
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = load i8*, i8** %6
store i8* %24, i8** %4
br label %55
25: ; preds = %16
%26 = load i8*, i8** %8
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = call i64 @strlen(i8* %28)
store i64 %29, i64* %9
%30 = load i64, i64* %9
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %25
%33 = load i64, i64* %7
%34 = icmp ult i64 %33, 2
br i1 %34, label %35, label %36
35: ; preds = %32
br label %13
36: ; preds = %32
%37 = load i8*, i8** %6
%38 = call i8* @strcpy(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %38, i8** %4
br label %55
39: ; preds = %25
%40 = load i64, i64* %9
%41 = load i64, i64* %7
%42 = icmp uge i64 %40, %41
%43 = zext i1 %42 to i32
%44 = call i64 @__glibc_unlikely(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = load i32, i32* @ERANGE
%48 = call i32 @__set_errno(i32 %47)
store i8* null, i8** %4
br label %55
49: ; preds = %39
%50 = load i8*, i8** %6
%51 = load i8*, i8** %8
%52 = load i64, i64* %9
%53 = add i64 %52, 1
%54 = call i8* @memcpy(i8* %50, i8* %51, i64 %53)
store i8* %54, i8** %4
br label %55
55: ; preds = %49, %46, %36, %23, %13
%56 = load i8*, i8** %4
ret i8* %56
}

LLVM IR (-O2):
@ERANGE = global i32 0
define i8* @nis_domain_of_r(i32 %0, i8* writeonly %1, i64 %2) {
%4 = icmp eq i8* %1, null
br i1 %4, label %5, label %8
5: ; preds = %15, %3
%6 = load i32, i32* @ERANGE
%7 = tail call i32 @__set_errno(i32 %6)
br label %29
8: ; preds = %3
store i8 0, i8* %1
%9 = tail call i8* @strchr(i32 %0, i8 signext 46)
%10 = icmp eq i8* %9, null
br i1 %10, label %29, label %11
11: ; preds = %8
%12 = getelementptr inbounds i8, i8* %9, i64 1
%13 = tail call i64 @strlen(i8* %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = icmp ult i64 %2, 2
br i1 %16, label %5, label %17
17: ; preds = %15
%18 = bitcast i8* %1 to i16*
store i16 46, i16* %18
br label %29
19: ; preds = %11
%20 = icmp uge i64 %13, %2
%21 = zext i1 %20 to i32
%22 = tail call i64 @__glibc_unlikely(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = load i32, i32* @ERANGE
%26 = tail call i32 @__set_errno(i32 %25)
br label %29
27: ; preds = %19
%28 = add i64 %13, 1
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %1, i8* align 1 %12, i64 %28, i1 false)
br label %29
29: ; preds = %8, %27, %24, %17, %5
%30 = phi i8* [ null, %5 ], [ %1, %17 ], [ null, %24 ], [ %1, %27 ], [ %1, %8 ]
ret i8* %30
}