#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 */
long INT_MAX;
long INT_MIN;
long atol(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int strlen(char *);
int reverse(int x) {
  static char buf[32];
  int i, j;
  sprintf(buf, "%d", x);
  i = (x < 0 ? 1 : 0);
  j = strlen(buf) - 1;
  while (i < j) {
    int t = buf[i];
    buf[i] = buf[j];
    buf[j] = t;
    i++, j--;
  }
  long t = atol(buf);
  if (t < 0 && t < INT_MIN || t > 0 && t > INT_MAX)
    t = 0;
  return ((int)t);
}


x86 assembly (clang, -O0):
<reverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edx
lea    0x2f0b(%rip),%rdi
lea    0xeb4(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x4(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovl  %ecx,%eax
mov    %eax,-0x8(%rbp)
lea    0x2ee5(%rip),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11da <reverse+0xaa>
movslq -0x8(%rbp),%rcx
lea    0x2ec3(%rip),%rax
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x10(%rbp)
movslq -0xc(%rbp),%rcx
lea    0x2eb1(%rip),%rax
mov    (%rax,%rcx,1),%dl
movslq -0x8(%rbp),%rcx
lea    0x2ea3(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
mov    %al,%dl
movslq -0xc(%rbp),%rcx
lea    0x2e90(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1176 <reverse+0x46>
lea    0x2e6f(%rip),%rdi
call   1040 <atol@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1209 <reverse+0xd9>
mov    -0x18(%rbp),%rax
mov    0x2de0(%rip),%rcx
cmp    (%rcx),%rax
jl     1228 <reverse+0xf8>
cmpq   $0x0,-0x18(%rbp)
jle    1230 <reverse+0x100>
mov    -0x18(%rbp),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
jle    1230 <reverse+0x100>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x2f13(%rip),%r14
lea    0xebc(%rip),%rsi
mov    %r14,%rdi
mov    %ebx,%edx
call   1050 <sprintf@plt>
shr    $0x1f,%ebx
mov    %r14,%rdi
call   1030 <strlen@plt>
add    $0xffffffff,%eax
cmp    %eax,%ebx
jge    118f <reverse+0x5f>
cltq
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%rbx,%r14,1),%ecx
movzbl (%rax,%r14,1),%edx
mov    %dl,(%rbx,%r14,1)
mov    %cl,(%rax,%r14,1)
add    $0x1,%rbx
add    $0xffffffffffffffff,%rax
cmp    %rax,%rbx
jl     1170 <reverse+0x40>
lea    0x2eba(%rip),%rdi
call   1040 <atol@plt>
test   %rax,%rax
jns    11ac <reverse+0x7c>
mov    0x2e39(%rip),%rcx
cmp    (%rcx),%rax
jl     11bd <reverse+0x8d>
test   %rax,%rax
jle    11bf <reverse+0x8f>
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
jle    11bf <reverse+0x8f>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@reverse.buf = internal global [32 x i8] zeroinitializer
@.str = [3 x i8] c"%d\00"
@INT_MIN = global i64 0
@INT_MAX = global i64 0
define i32 @reverse(i32 %0) {
%2 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @reverse.buf, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = lshr i32 %0, 31
%4 = tail call i32 @strlen(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @reverse.buf, i64 0, i64 0))
%5 = add i32 %4, -1
%6 = icmp slt i32 %3, %5
br i1 %6, label %7, label %21
7: ; preds = %1
%8 = sext i32 %5 to i64
%9 = lshr i32 %0, 31
%10 = zext i32 %9 to i64
br label %11
11: ; preds = %7, %11
%12 = phi i64 [ %10, %7 ], [ %18, %11 ]
%13 = phi i64 [ %8, %7 ], [ %19, %11 ]
%14 = getelementptr inbounds [32 x i8], [32 x i8]* @reverse.buf, i64 0, i64 %12
%15 = load i8, i8* %14
%16 = getelementptr inbounds [32 x i8], [32 x i8]* @reverse.buf, i64 0, i64 %13
%17 = load i8, i8* %16
store i8 %17, i8* %14
store i8 %15, i8* %16
%18 = add nuw nsw i64 %12, 1
%19 = add nsw i64 %13, -1
%20 = icmp slt i64 %18, %19
br i1 %20, label %11, label %21
21: ; preds = %11, %1
%22 = tail call i64 @atol(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @reverse.buf, i64 0, i64 0))
%23 = icmp slt i64 %22, 0
%24 = load i64, i64* @INT_MIN
%25 = icmp slt i64 %22, %24
%26 = select i1 %23, i1 %25, i1 false
br i1 %26, label %32, label %27
27: ; preds = %21
%28 = icmp sgt i64 %22, 0
%29 = load i64, i64* @INT_MAX
%30 = icmp sgt i64 %22, %29
%31 = select i1 %28, i1 %30, i1 false
br i1 %31, label %32, label %33
32: ; preds = %27, %21
br label %33
33: ; preds = %32, %27
%34 = phi i64 [ 0, %32 ], [ %22, %27 ]
%35 = trunc i64 %34 to i32
ret i32 %35
}