#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 char *const_nis_name;

/* Variables and functions */
int /*<<< orphan*/ ERANGE;
scalar_t__ __glibc_unlikely(int);
scalar_t__ __mempcpy(char *, char *, size_t);
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
nis_name nis_leaf_of_r(const_nis_name name, char *buffer, size_t buflen) {
  size_t i = 0;
  buffer[0] = '\0';
  while (name[i] != '.' && name[i] != '\0')
    i++;
  if (__glibc_unlikely(i >= buflen)) {
    __set_errno(ERANGE);
    return NULL;
  }
  *((char *)__mempcpy(buffer, name, i)) = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<nis_leaf_of_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x2e,%ecx
mov    %al,-0x29(%rbp)
je     1182 <nis_leaf_of_r+0x52>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1192 <nis_leaf_of_r+0x62>
jmp    11a3 <nis_leaf_of_r+0x73>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1153 <nis_leaf_of_r+0x23>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <__glibc_unlikely@plt>
cmp    $0x0,%rax
je     11dd <nis_leaf_of_r+0xad>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <nis_leaf_of_r+0xc9>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <__mempcpy@plt>
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nis_leaf_of_r>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
movb   $0x0,(%rsi)
xor    %ebx,%ebx
movzbl (%r15,%rbx,1),%eax
test   %al,%al
je     1153 <nis_leaf_of_r+0x23>
cmp    $0x2e,%al
je     1153 <nis_leaf_of_r+0x23>
add    $0x1,%rbx
jmp    1140 <nis_leaf_of_r+0x10>
xor    %edi,%edi
cmp    %rdx,%rbx
setae  %dil
call   1050 <__glibc_unlikely@plt>
test   %rax,%rax
je     1179 <nis_leaf_of_r+0x49>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
xor    %r14d,%r14d
jmp    118a <nis_leaf_of_r+0x5a>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <__mempcpy@plt>
movb   $0x0,(%rax)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@ERANGE = global i32 0
define i8* @nis_leaf_of_r(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
%9 = load i8*, i8** %6
%10 = getelementptr inbounds i8, i8* %9, i64 0
store i8 0, i8* %10
br label %11
11: ; preds = %27, %3
%12 = load i8*, i8** %5
%13 = load i64, i64* %8
%14 = getelementptr inbounds i8, i8* %12, i64 %13
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 46
br i1 %17, label %18, label %25
18: ; preds = %11
%19 = load i8*, i8** %5
%20 = load i64, i64* %8
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 0
br label %25
25: ; preds = %18, %11
%26 = phi i1 [ false, %11 ], [ %24, %18 ]
br i1 %26, label %27, label %30
27: ; preds = %25
%28 = load i64, i64* %8
%29 = add i64 %28, 1
store i64 %29, i64* %8
br label %11
30: ; preds = %25
%31 = load i64, i64* %8
%32 = load i64, i64* %7
%33 = icmp uge i64 %31, %32
%34 = zext i1 %33 to i32
%35 = call i64 @__glibc_unlikely(i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %30
%38 = load i32, i32* @ERANGE
%39 = call i32 @__set_errno(i32 %38)
store i8* null, i8** %4
br label %47
40: ; preds = %30
%41 = load i8*, i8** %6
%42 = load i8*, i8** %5
%43 = load i64, i64* %8
%44 = call i64 @__mempcpy(i8* %41, i8* %42, i64 %43)
%45 = inttoptr i64 %44 to i8*
store i8 0, i8* %45
%46 = load i8*, i8** %6
store i8* %46, i8** %4
br label %47
47: ; preds = %40, %37
%48 = load i8*, i8** %4
ret i8* %48
}

LLVM IR (-O2):
@ERANGE = global i32 0
define i8* @nis_leaf_of_r(i8* %0, i8* %1, i64 %2) {
store i8 0, i8* %1
br label %4
4: ; preds = %8, %3
%5 = phi i64 [ 0, %3 ], [ %9, %8 ]
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = load i8, i8* %6
switch i8 %7, label %8 [
i8 46, label %10
i8 0, label %10
]
8: ; preds = %4
%9 = add i64 %5, 1
br label %4
10: ; preds = %4, %4
%11 = icmp uge i64 %5, %2
%12 = zext i1 %11 to i32
%13 = tail call i64 @__glibc_unlikely(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = load i32, i32* @ERANGE
%17 = tail call i32 @__set_errno(i32 %16)
br label %21
18: ; preds = %10
%19 = tail call i64 @__mempcpy(i8* %1, i8* %0, i64 %5)
%20 = inttoptr i64 %19 to i8*
store i8 0, i8* %20
br label %21
21: ; preds = %18, %15
%22 = phi i8* [ null, %15 ], [ %1, %18 ]
ret i8* %22
}