#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*/ snprintf(char *, int, char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
long strtol(char *, int /*<<< orphan*/ *, int);
long to_octal(int n, char *rep) {
  int r[10], i = 0, j;
  char str[11];
  char *ptr = str;
  while (n > 0) {
    r[i] = n % 8;
    n = n / 8;
    i++;
  }
  i--;
  snprintf(ptr, 10 - i, "0");
  ptr++;
  for (j = 0; i >= 0; i--, j++) {
    snprintf(ptr, 10 - i, "%d", r[i]);
    ptr++;
  }
  strcpy(rep, str);
  return strtol(str, NULL, 10);
}


x86 assembly (clang, -O0):
<to_octal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x44(%rbp)
lea    -0x53(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpl   $0x0,-0x4(%rbp)
jle    1187 <to_octal+0x57>
mov    -0x4(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
movslq -0x44(%rbp),%rax
mov    %edx,-0x40(%rbp,%rax,4)
mov    -0x4(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    114e <to_octal+0x1e>
mov    -0x44(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x44(%rbp)
mov    -0x60(%rbp),%rdi
mov    $0xa,%esi
sub    -0x44(%rbp),%esi
lea    0xe5d(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x60(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x60(%rbp)
movl   $0x0,-0x48(%rbp)
cmpl   $0x0,-0x44(%rbp)
jl     120c <to_octal+0xdc>
mov    -0x60(%rbp),%rdi
mov    $0xa,%esi
sub    -0x44(%rbp),%esi
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%ecx
lea    0xe20(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x60(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x60(%rbp)
mov    -0x44(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x44(%rbp)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    11bd <to_octal+0x8d>
mov    -0x10(%rbp),%rdi
lea    -0x53(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x53(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_octal>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
test   %edi,%edi
jle    11c8 <to_octal+0x98>
xor    %ebp,%ebp
mov    %edi,%eax
nopl   (%rax)
mov    %edi,%ecx
and    $0x7,%ecx
mov    %ecx,0x10(%rsp,%rbp,4)
shr    $0x3,%eax
add    $0x1,%rbp
cmp    $0x7,%edi
mov    %eax,%edi
ja     1150 <to_octal+0x20>
mov    $0xb,%r12d
mov    $0xb,%esi
sub    %ebp,%esi
lea    0xe85(%rip),%rdx
lea    0x5(%rsp),%rdi
xor    %eax,%eax
call   1040 <snprintf@plt>
test   %ebp,%ebp
jle    11e0 <to_octal+0xb0>
lea    -0x1(%rbp),%r13
lea    0x6(%rsp),%rbx
sub    %ebp,%r12d
lea    0xe64(%rip),%r15
xchg   %ax,%ax
mov    0xc(%rsp,%rbp,4),%ecx
add    $0xffffffffffffffff,%rbp
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <snprintf@plt>
add    $0x1,%rbx
add    $0x1,%r12d
add    $0xffffffffffffffff,%r13
jb     11a0 <to_octal+0x70>
jmp    11e0 <to_octal+0xb0>
lea    0xe31(%rip),%rdx
lea    0x5(%rsp),%rdi
mov    $0xb,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
lea    0x5(%rsp),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"0\00"
@.str.1 = [3 x i8] c"%d\00"
define i64 @to_octal(i32 %0, i8* %1) {
%3 = alloca [10 x i32]
%4 = alloca [11 x i8]
%5 = bitcast [10 x i32]* %3 to i8*
%6 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 0
%7 = icmp sgt i32 %0, 0
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %40
10: ; preds = %2, %10
%11 = phi i64 [ %16, %10 ], [ 0, %2 ]
%12 = phi i32 [ %15, %10 ], [ %0, %2 ]
%13 = and i32 %12, 7
%14 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 %11
store i32 %13, i32* %14
%15 = lshr i32 %12, 3
%16 = add nuw nsw i64 %11, 1
%17 = icmp ult i32 %12, 8
br i1 %17, label %18, label %10
18: ; preds = %10
%19 = trunc i64 %16 to i32
%20 = sub nsw i32 11, %19
%21 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = icmp sgt i32 %19, 0
br i1 %22, label %23, label %40
23: ; preds = %18
%24 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 1
%25 = and i64 %16, 4294967295
%26 = and i64 %11, 4294967295
%27 = add nuw nsw i64 %26, 1
%28 = getelementptr [11 x i8], [11 x i8]* %4, i64 0, i64 %27
br label %29
29: ; preds = %23, %29
%30 = phi i64 [ %25, %23 ], [ %33, %29 ]
%31 = phi i8* [ %24, %23 ], [ %38, %29 ]
%32 = trunc i64 %30 to i32
%33 = add nsw i64 %30, -1
%34 = sub nsw i32 11, %32
%35 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 %33
%36 = load i32, i32* %35
%37 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %31, i32 %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = getelementptr inbounds i8, i8* %31, i64 1
%39 = icmp eq i8* %31, %28
br i1 %39, label %40, label %29
40: ; preds = %29, %8, %18
%41 = call i32 @strcpy(i8* %1, i8* %6)
%42 = call i64 @strtol(i8* nocapture %6, i32* null, i32 10)
ret i64 %42
}