#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 */
int /*<<< orphan*/ write_all(int, char *, int);
int write_name(int fd, char const *s) {
  static char const *hexdigits = "0123456789abcdef";
  char buf[5] = "\\x00";
  while (*s != '\0') {
    buf[2] = hexdigits[(((unsigned char)*s) >> 4) & 0xF];
    buf[3] = hexdigits[((unsigned char)*s) & 0xF];
    if (!write_all(fd, buf, sizeof(buf) - 1))
      return 0;
    ++s;
  }
  return 1;
}


x86 assembly (clang, -O0):
<write_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xeec(%rip),%eax
mov    %eax,-0x15(%rbp)
mov    0xee7(%rip),%al
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11af <write_name+0x9f>
mov    0x2ee0(%rip),%rax
mov    -0x10(%rbp),%rcx
movzbl (%rcx),%ecx
sar    $0x4,%ecx
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x13(%rbp)
mov    0x2ec3(%rip),%rax
mov    -0x10(%rbp),%rcx
movzbl (%rcx),%ecx
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x12(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write_all@plt>
cmp    $0x0,%eax
jne    119e <write_name+0x8e>
movl   $0x0,-0x4(%rbp)
jmp    11b6 <write_name+0xa6>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1131 <write_name+0x21>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_name>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movb   $0x0,0xc(%rsp)
movl   $0x3030785c,0x8(%rsp)
30
mov    (%rsi),%al
mov    $0x1,%r14d
test   %al,%al
je     1191 <write_name+0x81>
mov    %rsi,%rbx
mov    %edi,%r12d
add    $0x1,%rbx
lea    0xeba(%rip),%rbp
lea    0x8(%rsp),%r15
nopl   0x0(%rax,%rax,1)
movzbl %al,%eax
mov    %rax,%rcx
shr    $0x4,%rcx
movzbl (%rcx,%rbp,1),%ecx
mov    %cl,0xa(%rsp)
and    $0xf,%eax
movzbl (%rax,%rbp,1),%eax
mov    %al,0xb(%rsp)
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <write_all@plt>
test   %eax,%eax
je     118e <write_name+0x7e>
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1150 <write_name+0x40>
jmp    1191 <write_name+0x81>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@write_name.hexdigits = internal global i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i32 0, i32 0)
@.str = [17 x i8] c"0123456789abcdef\00"
@__const.write_name.buf = [5 x i8] c"\\x00\00"
define i32 @write_name(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [5 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = bitcast [5 x i8]* %6 to i8*
br label %8
8: ; preds = %38, %2
%9 = load i8*, i8** %5
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %41
13: ; preds = %8
%14 = load i8*, i8** @write_name.hexdigits
%15 = load i8*, i8** %5
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = ashr i32 %17, 4
%19 = and i32 %18, 15
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %14, i64 %20
%22 = load i8, i8* %21
%23 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 2
store i8 %22, i8* %23
%24 = load i8*, i8** @write_name.hexdigits
%25 = load i8*, i8** %5
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = and i32 %27, 15
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %24, i64 %29
%31 = load i8, i8* %30
%32 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 3
store i8 %31, i8* %32
%33 = load i32, i32* %4
%34 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%35 = call i32 @write_all(i32 %33, i8* %34, i32 4)
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %13
store i32 0, i32* %3
br label %42
38: ; preds = %13
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %5
br label %8
41: ; preds = %8
store i32 1, i32* %3
br label %42
42: ; preds = %41, %37
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
@__const.write_name.buf = [5 x i8] c"\\x00\00"
define i32 @write_name(i32 %0, i8* nocapture %1) {
%3 = alloca [5 x i8]
%4 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%5 = load i8, i8* %1
%6 = icmp eq i8 %5, 0
br i1 %6, label %27, label %7
7: ; preds = %2
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 2
%9 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 3
br label %13
10: ; preds = %13
%11 = load i8, i8* %26
%12 = icmp eq i8 %11, 0
br i1 %12, label %27, label %13
13: ; preds = %7, %10
%14 = phi i8 [ %5, %7 ], [ %11, %10 ]
%15 = phi i8* [ %1, %7 ], [ %26, %10 ]
%16 = lshr i8 %14, 4
%17 = zext i8 %16 to i64
%18 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %17
%19 = load i8, i8* %18
store i8 %19, i8* %8
%20 = and i8 %14, 15
%21 = zext i8 %20 to i64
%22 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %21
%23 = load i8, i8* %22
store i8 %23, i8* %9
%24 = call i32 @write_all(i32 %0, i8* %4, i32 4)
%25 = icmp eq i32 %24, 0
%26 = getelementptr inbounds i8, i8* %15, i64 1
br i1 %25, label %27, label %10
27: ; preds = %13, %10, %2
%28 = phi i32 [ 1, %2 ], [ 1, %10 ], [ 0, %13 ]
ret i32 %28
}