#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 get_digit(char);
char set_digit(int);
void round_hex_string(char *hex_str, int index) {
  int idx = index - 1;
  if (get_digit(hex_str[idx]) > 7) {
    idx--;
    if (get_digit(hex_str[idx]) == 15) {
      ;
    } else {
      hex_str[idx] = set_digit(get_digit(hex_str[idx]) + 1);
    }
  }
  hex_str[index - 1] = '\0';
  return;
}


x86 assembly (clang, -O0):
<round_hex_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <get_digit@plt>
cmp    $0x7,%eax
jle    11a7 <round_hex_string+0x87>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <get_digit@plt>
cmp    $0xf,%eax
jne    117a <round_hex_string+0x5a>
jmp    11a2 <round_hex_string+0x82>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <get_digit@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1030 <set_digit@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11a7 <round_hex_string+0x87>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<round_hex_string>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
movslq %esi,%rbx
movsbl -0x1(%rbx,%rdi,1),%edi
call   1040 <get_digit@plt>
cmp    $0x8,%eax
jl     1161 <round_hex_string+0x41>
movsbl -0x2(%rbx,%r14,1),%edi
call   1040 <get_digit@plt>
cmp    $0xf,%eax
je     1161 <round_hex_string+0x41>
movsbl -0x2(%r14,%rbx,1),%edi
call   1040 <get_digit@plt>
lea    0x1(%rax),%edi
call   1030 <set_digit@plt>
mov    %al,-0x2(%r14,%rbx,1)
movb   $0x0,-0x1(%r14,%rbx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @round_hex_string(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = sub nsw i32 %6, 1
store i32 %7, i32* %5
%8 = load i8*, i8** %3
%9 = load i32, i32* %5
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %8, i64 %10
%12 = load i8, i8* %11
%13 = call i32 @get_digit(i8 signext %12)
%14 = icmp sgt i32 %13, 7
br i1 %14, label %15, label %40
15: ; preds = %2
%16 = load i32, i32* %5
%17 = add nsw i32 %16, -1
store i32 %17, i32* %5
%18 = load i8*, i8** %3
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = call i32 @get_digit(i8 signext %22)
%24 = icmp eq i32 %23, 15
br i1 %24, label %25, label %26
25: ; preds = %15
br label %39
26: ; preds = %15
%27 = load i8*, i8** %3
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = call i32 @get_digit(i8 signext %31)
%33 = add nsw i32 %32, 1
%34 = call signext i8 @set_digit(i32 %33)
%35 = load i8*, i8** %3
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 %34, i8* %38
br label %39
39: ; preds = %26, %25
br label %40
40: ; preds = %39, %2
%41 = load i8*, i8** %3
%42 = load i32, i32* %4
%43 = sub nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %41, i64 %44
store i8 0, i8* %45
ret void
}

LLVM IR (-O2):
define void @round_hex_string(i8* nocapture %0, i32 %1) {
%3 = add nsw i32 %1, -1
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = load i8, i8* %5
%7 = tail call i32 @get_digit(i8 signext %6)
%8 = icmp sgt i32 %7, 7
br i1 %8, label %9, label %21
9: ; preds = %2
%10 = add nsw i32 %1, -2
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = tail call i32 @get_digit(i8 signext %13)
%15 = icmp eq i32 %14, 15
br i1 %15, label %21, label %16
16: ; preds = %9
%17 = load i8, i8* %12
%18 = tail call i32 @get_digit(i8 signext %17)
%19 = add nsw i32 %18, 1
%20 = tail call signext i8 @set_digit(i32 %19)
store i8 %20, i8* %12
br label %21
21: ; preds = %16, %9, %2
store i8 0, i8* %5
ret void
}