#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 /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ isgraph(char const);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
const char *printable_dnsname(const char *name) {
  static char buf[512];
  int i;
  for (i = 0; i < sizeof(buf) - 1; name++) {
    if (!*name)
      break;
    if (isgraph(*name)) {
      buf[i] = *name;
      i++;
    } else {
      if (i + 3 > 512 - 1)
        break;
      sprintf(buf + i, "%%%02x", (unsigned char)*name);
      i += 3;
    }
  }
  buf[i] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<printable_dnsname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movslq -0xc(%rbp),%rax
cmp    $0x1ff,%rax
jae    11e3 <printable_dnsname+0xc3>
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1155 <printable_dnsname+0x35>
jmp    11e3 <printable_dnsname+0xc3>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isgraph@plt>
cmp    $0x0,%rax
je     118d <printable_dnsname+0x6d>
mov    -0x8(%rbp),%rax
mov    (%rax),%dl
movslq -0xc(%rbp),%rcx
lea    0x2ec4(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11cd <printable_dnsname+0xad>
mov    -0xc(%rbp),%eax
add    $0x3,%eax
cmp    $0x1ff,%eax
jle    11a3 <printable_dnsname+0x83>
jmp    11e3 <printable_dnsname+0xc3>
movslq -0xc(%rbp),%rax
lea    0x2e92(%rip),%rdi
add    %rax,%rdi
mov    -0x8(%rbp),%rax
lea    0xe44(%rip),%rsi
movzbl (%rax),%edx
call   1030 <sprintf@plt>
mov    -0xc(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0xc(%rbp)
jmp    11d2 <printable_dnsname+0xb2>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1133 <printable_dnsname+0x13>
movslq -0xc(%rbp),%rcx
lea    0x2e52(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e47(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printable_dnsname>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
xor    %r12d,%r12d
lea    0x2f0b(%rip),%r14
lea    0xec4(%rip),%r15
xor    %ebp,%ebp
jmp    1159 <printable_dnsname+0x39>
movzbl (%rbx),%eax
mov    %al,(%r12,%r14,1)
add    $0x1,%ebp
add    $0x1,%rbx
mov    %ebp,%r12d
cmp    $0x1ff,%ebp
jae    118b <printable_dnsname+0x6b>
movzbl (%rbx),%eax
test   %al,%al
je     118b <printable_dnsname+0x6b>
movsbl %al,%edi
call   1040 <isgraph@plt>
test   %rax,%rax
jne    1140 <printable_dnsname+0x20>
cmp    $0x1fc,%ebp
ja     118b <printable_dnsname+0x6b>
add    $0x3,%ebp
add    %r14,%r12
movzbl (%rbx),%edx
mov    %r12,%rdi
mov    %r15,%rsi
call   1030 <sprintf@plt>
jmp    114a <printable_dnsname+0x2a>
mov    %ebp,%eax
movb   $0x0,(%rax,%r14,1)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@printable_dnsname.buf = internal global [512 x i8] zeroinitializer
@.str = [7 x i8] c"%%%02x\00"
define i8* @printable_dnsname(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
br label %4
4: ; preds = %41, %1
%5 = load i32, i32* %3
%6 = sext i32 %5 to i64
%7 = icmp ult i64 %6, 511
br i1 %7, label %8, label %44
8: ; preds = %4
%9 = load i8*, i8** %2
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %8
br label %44
13: ; preds = %8
%14 = load i8*, i8** %2
%15 = load i8, i8* %14
%16 = call i64 @isgraph(i8 signext %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %13
%19 = load i8*, i8** %2
%20 = load i8, i8* %19
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [512 x i8], [512 x i8]* @printable_dnsname.buf, i64 0, i64 %22
store i8 %20, i8* %23
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
br label %40
26: ; preds = %13
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 3
%29 = icmp sgt i32 %28, 511
br i1 %29, label %30, label %31
30: ; preds = %26
br label %44
31: ; preds = %26
%32 = load i32, i32* %3
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* getelementptr inbounds ([512 x i8], [512 x i8]* @printable_dnsname.buf, i64 0, i64 0), i64 %33
%35 = load i8*, i8** %2
%36 = load i8, i8* %35
%37 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %36)
%38 = load i32, i32* %3
%39 = add nsw i32 %38, 3
store i32 %39, i32* %3
br label %40
40: ; preds = %31, %18
br label %41
41: ; preds = %40
%42 = load i8*, i8** %2
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %2
br label %4
44: ; preds = %30, %12, %4
%45 = load i32, i32* %3
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [512 x i8], [512 x i8]* @printable_dnsname.buf, i64 0, i64 %46
store i8 0, i8* %47
ret i8* getelementptr inbounds ([512 x i8], [512 x i8]* @printable_dnsname.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@printable_dnsname.buf = internal global [512 x i8] zeroinitializer
@.str = [7 x i8] c"%%%02x\00"
define i8* @printable_dnsname(i8* nocapture %0) {
br label %2
2: ; preds = %1, %22
%3 = phi i64 [ 0, %1 ], [ %25, %22 ]
%4 = phi i32 [ 0, %1 ], [ %23, %22 ]
%5 = phi i8* [ %0, %1 ], [ %24, %22 ]
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 0
br i1 %7, label %27, label %8
8: ; preds = %2
%9 = tail call i64 @isgraph(i8 signext %6)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = load i8, i8* %5
%13 = getelementptr inbounds [512 x i8], [512 x i8]* @printable_dnsname.buf, i64 0, i64 %3
store i8 %12, i8* %13
%14 = add nuw nsw i32 %4, 1
br label %22
15: ; preds = %8
%16 = icmp ugt i32 %4, 508
br i1 %16, label %27, label %17
17: ; preds = %15
%18 = add nuw nsw i32 %4, 3
%19 = getelementptr inbounds [512 x i8], [512 x i8]* @printable_dnsname.buf, i64 0, i64 %3
%20 = load i8, i8* %5
%21 = tail call i32 @sprintf(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8 zeroext %20)
br label %22
22: ; preds = %11, %17
%23 = phi i32 [ %14, %11 ], [ %18, %17 ]
%24 = getelementptr inbounds i8, i8* %5, i64 1
%25 = zext i32 %23 to i64
%26 = icmp ult i32 %23, 511
br i1 %26, label %2, label %27
27: ; preds = %15, %2, %22
%28 = phi i32 [ %4, %15 ], [ %4, %2 ], [ %23, %22 ]
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds [512 x i8], [512 x i8]* @printable_dnsname.buf, i64 0, i64 %29
store i8 0, i8* %30
ret i8* getelementptr inbounds ([512 x i8], [512 x i8]* @printable_dnsname.buf, i64 0, i64 0)
}