#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 libc_leni(long long);
int libc_ltoi(long long n) {
  int sz = libc_leni(n) - 8;
  if (n < 0) {
    sz--;
  }
  if (sz > 0) {
    while (sz) {
      n = (libc_leni(n) > 8) ? (n / 10) : n;
      sz--;
    }
  }
  return (int)n;
}


x86 assembly (clang, -O0):
<libc_ltoi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <libc_leni@plt>
sub    $0x8,%eax
mov    %eax,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    113f <libc_ltoi+0x2f>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11a4 <libc_ltoi+0x94>
jmp    114e <libc_ltoi+0x3e>
cmpl   $0x0,-0xc(%rbp)
je     119f <libc_ltoi+0x8f>
mov    -0x8(%rbp),%rdi
call   1030 <libc_leni@plt>
cmp    $0x8,%eax
jle    1181 <libc_ltoi+0x71>
mov    -0x8(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %rax,-0x18(%rbp)
jmp    1189 <libc_ltoi+0x79>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    114e <libc_ltoi+0x3e>
jmp    11a4 <libc_ltoi+0x94>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libc_ltoi>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1030 <libc_leni@plt>
mov    %rbx,%rcx
sar    $0x3f,%rcx
lea    (%rcx,%rax,1),%ebp
add    $0xfffffff8,%ebp
test   %ebp,%ebp
jle    116b <libc_ltoi+0x5b>
movabs $0x6666666666666667,%r14
66 66 66
jmp    1145 <libc_ltoi+0x35>
nopl   0x0(%rax)
add    $0xffffffff,%ebp
je     116b <libc_ltoi+0x5b>
mov    %rbx,%rdi
call   1030 <libc_leni@plt>
cmp    $0x9,%eax
jl     1140 <libc_ltoi+0x30>
mov    %rbx,%rax
imul   %r14
mov    %rdx,%rbx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x2,%rbx
add    %rax,%rbx
jmp    1140 <libc_ltoi+0x30>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @libc_ltoi(i64 %0) {
%2 = alloca i64
%3 = alloca i32
store i64 %0, i64* %2
%4 = load i64, i64* %2
%5 = call i32 @libc_leni(i64 %4)
%6 = sub nsw i32 %5, 8
store i32 %6, i32* %3
%7 = load i64, i64* %2
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* %3
%11 = add nsw i32 %10, -1
store i32 %11, i32* %3
br label %12
12: ; preds = %9, %1
%13 = load i32, i32* %3
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %33
15: ; preds = %12
br label %16
16: ; preds = %28, %15
%17 = load i32, i32* %3
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %16
%20 = load i64, i64* %2
%21 = call i32 @libc_leni(i64 %20)
%22 = icmp sgt i32 %21, 8
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i64, i64* %2
%25 = sdiv i64 %24, 10
br label %28
26: ; preds = %19
%27 = load i64, i64* %2
br label %28
28: ; preds = %26, %23
%29 = phi i64 [ %25, %23 ], [ %27, %26 ]
store i64 %29, i64* %2
%30 = load i32, i32* %3
%31 = add nsw i32 %30, -1
store i32 %31, i32* %3
br label %16
32: ; preds = %16
br label %33
33: ; preds = %32, %12
%34 = load i64, i64* %2
%35 = trunc i64 %34 to i32
ret i32 %35
}

LLVM IR (-O2):
define i32 @libc_ltoi(i64 %0) {
%2 = tail call i32 @libc_leni(i64 %0)
%3 = icmp slt i64 %0, 0
%4 = select i1 %3, i32 -9, i32 -8
%5 = add nsw i32 %2, %4
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %1, %7
%8 = phi i64 [ %13, %7 ], [ %0, %1 ]
%9 = phi i32 [ %14, %7 ], [ %5, %1 ]
%10 = tail call i32 @libc_leni(i64 %8)
%11 = icmp sgt i32 %10, 8
%12 = sdiv i64 %8, 10
%13 = select i1 %11, i64 %12, i64 %8
%14 = add nsw i32 %9, -1
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %7
16: ; preds = %7, %1
%17 = phi i64 [ %0, %1 ], [ %13, %7 ]
%18 = trunc i64 %17 to i32
ret i32 %18
}