#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 int64_t;

/* Variables and functions */
int abs(int);
int reverse(int x) {
  int flag = (x > 0) ? 1 : -1;
  int value = abs(x);
  int64_t result = 0;
  while (value > 0) {
    result = result * 10 + value % 10;
    value /= 10;
  }
  if (result > ((int)(~0U >> 1)))
    result = 0;
  return (int)result * flag;
}


x86 assembly (clang, -O0):
<reverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edx
mov    $0xffffffff,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovg  %ecx,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <abs@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    117a <reverse+0x6a>
imul   $0xa,-0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    -0x14(%rbp),%eax
add    %edx,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0xc(%rbp)
jmp    1143 <reverse+0x33>
cmpl   $0x7fffffff,-0x10(%rbp)
jle    118e <reverse+0x7e>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
imul   -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse>:
test   %edi,%edi
je     1145 <reverse+0x45>
mov    %edi,%eax
neg    %eax
cmovs  %edi,%eax
xor    %edx,%edx
mov    $0xcccccccd,%r8d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    (%rdx,%rdx,4),%r9d
mov    %eax,%esi
imul   %r8,%rsi
shr    $0x23,%rsi
lea    (%rsi,%rsi,1),%ecx
lea    (%rcx,%rcx,4),%ecx
mov    %eax,%edx
sub    %ecx,%edx
lea    (%rdx,%r9,2),%edx
cmp    $0x9,%eax
mov    %esi,%eax
ja     1120 <reverse+0x20>
jmp    1147 <reverse+0x47>
xor    %edx,%edx
mov    %edx,%eax
neg    %eax
test   %edi,%edi
cmovg  %edx,%eax
ret

LLVM IR (-O0):
define i32 @reverse(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = icmp sgt i32 %6, 0
%8 = zext i1 %7 to i64
%9 = select i1 %7, i32 1, i32 -1
store i32 %9, i32* %3
%10 = load i32, i32* %2
%11 = call i32 @abs(i32 %10)
store i32 %11, i32* %4
store i32 0, i32* %5
br label %12
12: ; preds = %15, %1
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = load i32, i32* %5
%17 = mul nsw i32 %16, 10
%18 = load i32, i32* %4
%19 = srem i32 %18, 10
%20 = add nsw i32 %17, %19
store i32 %20, i32* %5
%21 = load i32, i32* %4
%22 = sdiv i32 %21, 10
store i32 %22, i32* %4
br label %12
23: ; preds = %12
%24 = load i32, i32* %5
%25 = icmp sgt i32 %24, 2147483647
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 0, i32* %5
br label %27
27: ; preds = %26, %23
%28 = load i32, i32* %5
%29 = load i32, i32* %3
%30 = mul nsw i32 %28, %29
ret i32 %30
}

LLVM IR (-O2):
define i32 @reverse(i32 %0) {
%2 = icmp eq i32 %0, 0
br i1 %2, label %13, label %3
3: ; preds = %1
%4 = tail call i32 @llvm.abs.i32(i32 %0, i1 true)
br label %5
5: ; preds = %3, %5
%6 = phi i32 [ %10, %5 ], [ 0, %3 ]
%7 = phi i32 [ %11, %5 ], [ %4, %3 ]
%8 = mul nsw i32 %6, 10
%9 = urem i32 %7, 10
%10 = add nsw i32 %8, %9
%11 = udiv i32 %7, 10
%12 = icmp ult i32 %7, 10
br i1 %12, label %13, label %5
13: ; preds = %5, %1
%14 = phi i32 [ 0, %1 ], [ %10, %5 ]
%15 = icmp slt i32 %0, 1
%16 = sub nsw i32 0, %14
%17 = select i1 %15, i32 %16, i32 %14
ret i32 %17
}