#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 /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ check_deref_cast(char const *, int *);
scalar_t__ check_simple_cast(char const *, int *, int *);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
scalar_t__ strchr(char const *, char);
int /*<<< orphan*/ strcmp(char const *, char const *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncmp(char const *, char *, int /*<<< orphan*/);
const char *simplify_cast(const char *cast1, const char *cast2) {
  static char buf[256];
  int bits1, bits2;
  int s1, s2;
  if (cast1[0] == 0)
    return cast2;
  if (cast2[0] == 0)
    return cast1;
  if (!strcmp(cast1, cast2))
    return cast1;
  if (check_simple_cast(cast1, &bits1, &s1) == 0 &&
      check_simple_cast(cast2, &bits2, &s2) == 0) {
    if (bits1 <= bits2)
      return cast1;
  }
  if (check_simple_cast(cast1, &bits1, &s1) == 0 &&
      check_deref_cast(cast2, &bits2) == 0) {
    if (bits1 == bits2) {
      snprintf(buf, sizeof(buf), "*(%c%d *)", s1 ? 's' : 'u', bits1);
      return buf;
    }
  }
  if (strchr(cast1, '*') && !strncmp(cast2, "(u32)", strlen("(u32)")))
    return cast1;
  snprintf(buf, sizeof(buf), "%s%s", cast1, cast2);
  return buf;
}


x86 assembly (clang, -O0):
<simplify_cast>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    119d <simplify_cast+0x2d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1336 <simplify_cast+0x1c6>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11ba <simplify_cast+0x4a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1336 <simplify_cast+0x1c6>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    11dd <simplify_cast+0x6d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1336 <simplify_cast+0x1c6>
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1080 <check_simple_cast@plt>
cmp    $0x0,%rax
jne    1231 <simplify_cast+0xc1>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1080 <check_simple_cast@plt>
cmp    $0x0,%rax
jne    1231 <simplify_cast+0xc1>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jg     122c <simplify_cast+0xbc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1336 <simplify_cast+0x1c6>
jmp    1231 <simplify_cast+0xc1>
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1080 <check_simple_cast@plt>
cmp    $0x0,%rax
jne    12b5 <simplify_cast+0x145>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <check_deref_cast@plt>
cmp    $0x0,%rax
jne    12b5 <simplify_cast+0x145>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    12b0 <simplify_cast+0x140>
mov    -0x24(%rbp),%edx
mov    $0x75,%ecx
mov    $0x73,%eax
cmp    $0x0,%edx
cmovne %eax,%ecx
mov    -0x1c(%rbp),%r8d
lea    0x2de3(%rip),%rdi
mov    $0x100,%esi
lea    0xd67(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
lea    0x2dc9(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1336 <simplify_cast+0x1c6>
jmp    12b5 <simplify_cast+0x145>
mov    -0x10(%rbp),%rdi
mov    $0x2a,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
je     1309 <simplify_cast+0x199>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xd2e(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xd1c(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1309 <simplify_cast+0x199>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1336 <simplify_cast+0x1c6>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0x2d58(%rip),%rdi
mov    $0x100,%esi
lea    0xcec(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
lea    0x2d3e(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<simplify_cast>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
cmpb   $0x0,(%rdi)
je     129c <simplify_cast+0x12c>
mov    %rdi,%rbx
cmpb   $0x0,(%r14)
je     1299 <simplify_cast+0x129>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1299 <simplify_cast+0x129>
lea    0x4(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
call   1080 <check_simple_cast@plt>
test   %rax,%rax
jne    11de <simplify_cast+0x6e>
mov    %rsp,%rsi
lea    0xc(%rsp),%rdx
mov    %r14,%rdi
call   1080 <check_simple_cast@plt>
test   %rax,%rax
jne    11de <simplify_cast+0x6e>
mov    0x4(%rsp),%eax
cmp    (%rsp),%eax
jle    1299 <simplify_cast+0x129>
lea    0x4(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
call   1080 <check_simple_cast@plt>
test   %rax,%rax
jne    123f <simplify_cast+0xcf>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1090 <check_deref_cast@plt>
test   %rax,%rax
jne    123f <simplify_cast+0xcf>
mov    0x4(%rsp),%r8d
cmp    (%rsp),%r8d
jne    123f <simplify_cast+0xcf>
xor    %eax,%eax
cmpl   $0x0,0x8(%rsp)
sete   %al
lea    (%rax,%rax,1),%ecx
add    $0x73,%ecx
lea    0x2e49(%rip),%r14
lea    0xdd2(%rip),%rdx
mov    %r14,%rdi
mov    $0x100,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    129c <simplify_cast+0x12c>
mov    %rbx,%rdi
mov    $0x2a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1271 <simplify_cast+0x101>
lea    0xdb2(%rip),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     1299 <simplify_cast+0x129>
lea    0x2df8(%rip),%r15
lea    0xd91(%rip),%rdx
mov    %r15,%rdi
mov    $0x100,%esi
mov    %rbx,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %r15,%r14
jmp    129c <simplify_cast+0x12c>
mov    %rbx,%r14
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@simplify_cast.buf = internal global [256 x i8] zeroinitializer
@.str = [10 x i8] c"*(%c%d *)\00"
@.str.1 = [6 x i8] c"(u32)\00"
@.str.2 = [5 x i8] c"%s%s\00"
define i8* @simplify_cast(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = getelementptr inbounds i8, i8* %10, i64 0
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load i8*, i8** %5
store i8* %16, i8** %3
br label %82
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i64 0
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i8*, i8** %4
store i8* %24, i8** %3
br label %82
25: ; preds = %17
%26 = load i8*, i8** %4
%27 = load i8*, i8** %5
%28 = call i32 @strcmp(i8* %26, i8* %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %25
%31 = load i8*, i8** %4
store i8* %31, i8** %3
br label %82
32: ; preds = %25
%33 = load i8*, i8** %4
%34 = call i64 @check_simple_cast(i8* %33, i32* %6, i32* %8)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %47
36: ; preds = %32
%37 = load i8*, i8** %5
%38 = call i64 @check_simple_cast(i8* %37, i32* %7, i32* %9)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %47
40: ; preds = %36
%41 = load i32, i32* %6
%42 = load i32, i32* %7
%43 = icmp sle i32 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = load i8*, i8** %4
store i8* %45, i8** %3
br label %82
46: ; preds = %40
br label %47
47: ; preds = %46, %36, %32
%48 = load i8*, i8** %4
%49 = call i64 @check_simple_cast(i8* %48, i32* %6, i32* %8)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %67
51: ; preds = %47
%52 = load i8*, i8** %5
%53 = call i64 @check_deref_cast(i8* %52, i32* %7)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %67
55: ; preds = %51
%56 = load i32, i32* %6
%57 = load i32, i32* %7
%58 = icmp eq i32 %56, %57
br i1 %58, label %59, label %66
59: ; preds = %55
%60 = load i32, i32* %8
%61 = icmp ne i32 %60, 0
%62 = zext i1 %61 to i64
%63 = select i1 %61, i32 115, i32 117
%64 = load i32, i32* %6
%65 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @simplify_cast.buf, i64 0, i64 0), i32 256, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %63, i32 %64)
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @simplify_cast.buf, i64 0, i64 0), i8** %3
br label %82
66: ; preds = %55
br label %67
67: ; preds = %66, %51, %47
%68 = load i8*, i8** %4
%69 = call i64 @strchr(i8* %68, i8 signext 42)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %78
71: ; preds = %67
%72 = load i8*, i8** %5
%73 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%74 = call i32 @strncmp(i8* %72, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %73)
%75 = icmp ne i32 %74, 0
br i1 %75, label %78, label %76
76: ; preds = %71
%77 = load i8*, i8** %4
store i8* %77, i8** %3
br label %82
78: ; preds = %71, %67
%79 = load i8*, i8** %4
%80 = load i8*, i8** %5
%81 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @simplify_cast.buf, i64 0, i64 0), i32 256, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %79, i8* %80)
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @simplify_cast.buf, i64 0, i64 0), i8** %3
br label %82
82: ; preds = %78, %76, %59, %44, %30, %23, %15
%83 = load i8*, i8** %3
ret i8* %83
}

LLVM IR (-O2):
@simplify_cast.buf = internal global [256 x i8] zeroinitializer
@.str = [10 x i8] c"*(%c%d *)\00"
@.str.1 = [6 x i8] c"(u32)\00"
@.str.2 = [5 x i8] c"%s%s\00"
define i8* @simplify_cast(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = load i8, i8* %0
%12 = icmp eq i8 %11, 0
br i1 %12, label %53, label %13
13: ; preds = %2
%14 = load i8, i8* %1
%15 = icmp eq i8 %14, 0
br i1 %15, label %53, label %16
16: ; preds = %13
%17 = tail call i32 @strcmp(i8* %0, i8* %1)
%18 = icmp eq i32 %17, 0
br i1 %18, label %53, label %19
19: ; preds = %16
%20 = call i64 @check_simple_cast(i8* %0, i32* %3, i32* %5)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = call i64 @check_simple_cast(i8* %1, i32* %4, i32* %6)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* %3
%27 = load i32, i32* %4
%28 = icmp sgt i32 %26, %27
br i1 %28, label %29, label %53
29: ; preds = %25, %22, %19
%30 = call i64 @check_simple_cast(i8* %0, i32* %3, i32* %5)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %44
32: ; preds = %29
%33 = call i64 @check_deref_cast(i8* %1, i32* %4)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %32
%36 = load i32, i32* %3
%37 = load i32, i32* %4
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %44
39: ; preds = %35
%40 = load i32, i32* %5
%41 = icmp eq i32 %40, 0
%42 = select i1 %41, i32 117, i32 115
%43 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @simplify_cast.buf, i64 0, i64 0), i32 256, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %42, i32 %36)
br label %53
44: ; preds = %35, %32, %29
%45 = call i64 @strchr(i8* %0, i8 signext 42)
%46 = icmp eq i64 %45, 0
br i1 %46, label %51, label %47
47: ; preds = %44
%48 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%49 = call i32 @strncmp(i8* %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %48)
%50 = icmp eq i32 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %47, %44
%52 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @simplify_cast.buf, i64 0, i64 0), i32 256, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1)
br label %53
53: ; preds = %47, %25, %16, %13, %2, %51, %39
%54 = phi i8* [ getelementptr inbounds ([256 x i8], [256 x i8]* @simplify_cast.buf, i64 0, i64 0), %39 ], [ getelementptr inbounds ([256 x i8], [256 x i8]* @simplify_cast.buf, i64 0, i64 0), %51 ], [ %1, %2 ], [ %0, %13 ], [ %0, %16 ], [ %0, %25 ], [ %0, %47 ]
ret i8* %54
}