#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*/ reverse_string(char *, char *);
char *_octal2str(long val, char *out) {
  char *p = out;
  if (val == 0) {
    *p++ = '0';
    *p = 0;
  } else {
    while (val > 0) {
      *p++ = (val & 0x7) + '0';
      val >>= 3;
    }
    *p = 0;
    reverse_string(out, p);
  }
  return p;
}


x86 assembly (clang, -O0):
<_octal2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1151 <_octal2str+0x41>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x30,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    11a5 <_octal2str+0x95>
jmp    1156 <_octal2str+0x46>
cmpq   $0x0,-0x8(%rbp)
jle    1191 <_octal2str+0x81>
mov    -0x8(%rbp),%rax
and    $0x7,%rax
add    $0x30,%rax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
sar    $0x3,%rax
mov    %rax,-0x8(%rbp)
jmp    1156 <_octal2str+0x46>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <reverse_string@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_octal2str>:
push   %rbx
test   %rdi,%rdi
je     115b <_octal2str+0x4b>
mov    %rsi,%rbx
jle    114b <_octal2str+0x3b>
mov    %rsi,%rbx
mov    %rdi,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %edi,%ecx
and    $0x7,%cl
or     $0x30,%cl
mov    %cl,(%rbx)
add    $0x1,%rbx
shr    $0x3,%rax
cmp    $0x7,%rdi
mov    %rax,%rdi
ja     1130 <_octal2str+0x20>
movb   $0x0,(%rbx)
mov    %rsi,%rdi
mov    %rbx,%rsi
call   1030 <reverse_string@plt>
jmp    1167 <_octal2str+0x57>
movw   $0x30,(%rsi)
add    $0x1,%rsi
mov    %rsi,%rbx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
define i8* @_octal2str(i64 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
store i64 %0, i64* %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
store i8* %6, i8** %5
%7 = load i64, i64* %3
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i8*, i8** %5
%11 = getelementptr inbounds i8, i8* %10, i32 1
store i8* %11, i8** %5
store i8 48, i8* %10
%12 = load i8*, i8** %5
store i8 0, i8* %12
br label %31
13: ; preds = %2
br label %14
14: ; preds = %17, %13
%15 = load i64, i64* %3
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %14
%18 = load i64, i64* %3
%19 = and i64 %18, 7
%20 = add nsw i64 %19, 48
%21 = trunc i64 %20 to i8
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %5
store i8 %21, i8* %22
%24 = load i64, i64* %3
%25 = ashr i64 %24, 3
store i64 %25, i64* %3
br label %14
26: ; preds = %14
%27 = load i8*, i8** %5
store i8 0, i8* %27
%28 = load i8*, i8** %4
%29 = load i8*, i8** %5
%30 = call i32 @reverse_string(i8* %28, i8* %29)
br label %31
31: ; preds = %26, %9
%32 = load i8*, i8** %5
ret i8* %32
}

LLVM IR (-O2):
define i8* @_octal2str(i64 %0, i8* %1) {
%3 = icmp eq i64 %0, 0
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = icmp sgt i64 %0, 0
br i1 %5, label %8, label %17
6: ; preds = %2
%7 = getelementptr inbounds i8, i8* %1, i64 1
store i8 48, i8* %1
store i8 0, i8* %7
br label %20
8: ; preds = %4, %8
%9 = phi i8* [ %14, %8 ], [ %1, %4 ]
%10 = phi i64 [ %15, %8 ], [ %0, %4 ]
%11 = trunc i64 %10 to i8
%12 = and i8 %11, 7
%13 = or i8 %12, 48
%14 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %13, i8* %9
%15 = lshr i64 %10, 3
%16 = icmp ult i64 %10, 8
br i1 %16, label %17, label %8
17: ; preds = %8, %4
%18 = phi i8* [ %1, %4 ], [ %14, %8 ]
store i8 0, i8* %18
%19 = tail call i32 @reverse_string(i8* %1, i8* %18)
br label %20
20: ; preds = %17, %6
%21 = phi i8* [ %7, %6 ], [ %18, %17 ]
ret i8* %21
}