#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 */

/* Variables and functions */
int /*<<< orphan*/ strcpy(char *, char *);
int encode_nsap_new(char *buf, const unsigned char *addr) {
  int i;
  int digit;
  for (i = 20; i;) {
    i--;
    digit = addr[i] & 0x0F;
    *(buf++) = digit + (digit >= 10 ? '7' : '0');
    *(buf++) = '.';
    digit = ((unsigned char)(addr[i])) >> 4;
    *(buf++) = digit + (digit >= 10 ? '7' : '0');
    *(buf++) = '.';
  }
  strcpy(buf, "NSAP.INT.");
  return 0;
}


x86 assembly (clang, -O0):
<encode_nsap_new>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x14,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11dd <encode_nsap_new+0xcd>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
and    $0xf,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x18(%rbp),%esi
mov    $0x30,%ecx
mov    $0x37,%edx
cmp    $0xa,%esi
cmovge %edx,%ecx
add    %ecx,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x2e,(%rax)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
sar    $0x4,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x18(%rbp),%esi
mov    $0x30,%ecx
mov    $0x37,%edx
cmp    $0xa,%esi
cmovge %edx,%ecx
add    %ecx,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movb   $0x2e,(%rax)
jmp    1127 <encode_nsap_new+0x17>
mov    -0x8(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_nsap_new>:
push   %rax
mov    $0x14,%eax
mov    $0x37,%r8d
nopl   0x0(%rax)
movzbl -0x1(%rsi,%rax,1),%edx
and    $0xf,%dl
cmp    $0xa,%dl
mov    $0x30,%ecx
cmovae %r8d,%ecx
add    %dl,%cl
mov    %cl,(%rdi)
movb   $0x2e,0x1(%rdi)
movzbl -0x1(%rsi,%rax,1),%ecx
mov    %ecx,%edx
shr    $0x4,%dl
cmp    $0xa0,%cl
mov    $0x30,%ecx
cmovae %r8d,%ecx
add    %dl,%cl
mov    %cl,0x2(%rdi)
movb   $0x2e,0x3(%rdi)
add    $0x4,%rdi
add    $0xffffffffffffffff,%rax
jne    1120 <encode_nsap_new+0x10>
lea    0xe94(%rip),%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"NSAP.INT.\00"
define i32 @encode_nsap_new(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 20, i32* %5
br label %7
7: ; preds = %10, %2
%8 = load i32, i32* %5
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %49
10: ; preds = %7
%11 = load i32, i32* %5
%12 = add nsw i32 %11, -1
store i32 %12, i32* %5
%13 = load i8*, i8** %4
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = and i32 %18, 15
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = load i32, i32* %6
%22 = icmp sge i32 %21, 10
%23 = zext i1 %22 to i64
%24 = select i1 %22, i32 55, i32 48
%25 = add nsw i32 %20, %24
%26 = trunc i32 %25 to i8
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %3
store i8 %26, i8* %27
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %3
store i8 46, i8* %29
%31 = load i8*, i8** %4
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = ashr i32 %36, 4
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = load i32, i32* %6
%40 = icmp sge i32 %39, 10
%41 = zext i1 %40 to i64
%42 = select i1 %40, i32 55, i32 48
%43 = add nsw i32 %38, %42
%44 = trunc i32 %43 to i8
%45 = load i8*, i8** %3
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %3
store i8 %44, i8* %45
%47 = load i8*, i8** %3
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %3
store i8 46, i8* %47
br label %7
49: ; preds = %7
%50 = load i8*, i8** %3
%51 = call i32 @strcpy(i8* %50, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"NSAP.INT.\00"
define i32 @encode_nsap_new(i8* %0, i8* nocapture %1) {
br label %3
3: ; preds = %2, %3
%4 = phi i64 [ 20, %2 ], [ %6, %3 ]
%5 = phi i8* [ %0, %2 ], [ %21, %3 ]
%6 = add nsw i64 %4, -1
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = load i8, i8* %7
%9 = and i8 %8, 15
%10 = icmp ugt i8 %9, 9
%11 = select i1 %10, i8 55, i8 48
%12 = add nuw nsw i8 %11, %9
%13 = getelementptr inbounds i8, i8* %5, i64 1
store i8 %12, i8* %5
%14 = getelementptr inbounds i8, i8* %5, i64 2
store i8 46, i8* %13
%15 = load i8, i8* %7
%16 = lshr i8 %15, 4
%17 = icmp ugt i8 %15, -97
%18 = select i1 %17, i8 55, i8 48
%19 = add nuw nsw i8 %18, %16
%20 = getelementptr inbounds i8, i8* %5, i64 3
store i8 %19, i8* %14
%21 = getelementptr inbounds i8, i8* %5, i64 4
store i8 46, i8* %20
%22 = icmp eq i64 %6, 0
br i1 %22, label %23, label %3
23: ; preds = %3
%24 = tail call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
ret i32 0
}