#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 */
scalar_t__ calloc(int, int);
char *realloc(char *, int);
int /*<<< orphan*/ strchr(char *, char);
int strlen(char *);
char *nodupstring(char *s) {
  int len, i, p;
  char *new;
  len = strlen(s);
  new = (char *)calloc(len + 1, 1);
  p = 0;
  for (i = 0; i < len; i++) {
    if (!strchr(new, s[i])) {
      new[p] = s[i];
      p++;
    }
  }
  new = realloc(new, p + 1);
  return new;
}


x86 assembly (clang, -O0):
<nodupstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11d6 <nodupstring+0x96>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
call   1040 <strchr@plt>
cmp    $0x0,%eax
jne    11c3 <nodupstring+0x83>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c8 <nodupstring+0x88>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    117a <nodupstring+0x3a>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x1,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nodupstring>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r15
test   %ebx,%ebx
jle    11ac <nodupstring+0x6c>
mov    %ebx,%r12d
xor    %ebx,%ebx
xor    %r13d,%r13d
jmp    1189 <nodupstring+0x49>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    %rbx,%r12
je     11af <nodupstring+0x6f>
movsbl (%r14,%rbx,1),%esi
mov    %r15,%rdi
call   1040 <strchr@plt>
test   %eax,%eax
jne    1180 <nodupstring+0x40>
movzbl (%r14,%rbx,1),%eax
movslq %r13d,%r13
mov    %al,(%r15,%r13,1)
add    $0x1,%r13d
jmp    1180 <nodupstring+0x40>
xor    %r13d,%r13d
add    $0x1,%r13d
mov    %r15,%rdi
mov    %r13d,%esi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1060 <realloc@plt>

LLVM IR (-O0):
define i8* @nodupstring(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = add nsw i32 %9, 1
%11 = call i64 @calloc(i32 %10, i32 1)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %6
store i32 0, i32* %5
store i32 0, i32* %4
br label %13
13: ; preds = %39, %1
%14 = load i32, i32* %4
%15 = load i32, i32* %3
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %42
17: ; preds = %13
%18 = load i8*, i8** %6
%19 = load i8*, i8** %2
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = call i32 @strchr(i8* %18, i8 signext %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %38, label %26
26: ; preds = %17
%27 = load i8*, i8** %2
%28 = load i32, i32* %4
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = load i8*, i8** %6
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 %31, i8* %35
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
br label %38
38: ; preds = %26, %17
br label %39
39: ; preds = %38
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
br label %13
42: ; preds = %13
%43 = load i8*, i8** %6
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
%46 = call i8* @realloc(i8* %43, i32 %45)
store i8* %46, i8** %6
%47 = load i8*, i8** %6
ret i8* %47
}

LLVM IR (-O2):
define i8* @nodupstring(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i64 @calloc(i32 %3, i32 1)
%5 = inttoptr i64 %4 to i8*
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %25
7: ; preds = %1
%8 = zext i32 %2 to i64
br label %9
9: ; preds = %7, %21
%10 = phi i64 [ 0, %7 ], [ %23, %21 ]
%11 = phi i32 [ 0, %7 ], [ %22, %21 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %10
%13 = load i8, i8* %12
%14 = tail call i32 @strchr(i8* %5, i8 signext %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %9
%17 = load i8, i8* %12
%18 = sext i32 %11 to i64
%19 = getelementptr inbounds i8, i8* %5, i64 %18
store i8 %17, i8* %19
%20 = add nsw i32 %11, 1
br label %21
21: ; preds = %9, %16
%22 = phi i32 [ %11, %9 ], [ %20, %16 ]
%23 = add nuw nsw i64 %10, 1
%24 = icmp eq i64 %23, %8
br i1 %24, label %25, label %9
25: ; preds = %21, %1
%26 = phi i32 [ 0, %1 ], [ %22, %21 ]
%27 = add nsw i32 %26, 1
%28 = tail call i8* @realloc(i8* %5, i32 %27)
ret i8* %28
}