#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 */
struct hashtable {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int ht_add(struct hashtable *, void *, void *);
int ht_free(struct hashtable *, unsigned int);
int ht_search(struct hashtable *, void *, unsigned int *);
int ht_replace(struct hashtable *t, void *key, void *data) {
  int ret;
  unsigned int index;
  ret = ht_add(t, key, data);
  if (ret == 0 || ret != 3)
    return ret;
  ret = ht_search(t, key, &index);
  assert(ret == 1);
  ret = ht_free(t, index);
  assert(ret == 0);
  return ht_add(t, key, data);
}


x86 assembly (clang, -O0):
<ht_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <ht_add@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     117c <ht_replace+0x3c>
cmpl   $0x3,-0x24(%rbp)
je     1187 <ht_replace+0x47>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e0 <ht_replace+0xa0>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1050 <ht_search@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1030 <ht_free@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <ht_add@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ht_replace>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1040 <ht_add@plt>
cmp    $0x3,%eax
jne    11a1 <ht_replace+0x61>
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <ht_search@plt>
xor    %edi,%edi
cmp    $0x1,%eax
sete   %dil
call   1060 <assert@plt>
mov    0xc(%rsp),%esi
mov    %rbx,%rdi
call   1030 <ht_free@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1060 <assert@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <ht_add@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.hashtable = type { i32 }
define i32 @ht_replace(%struct.hashtable* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.hashtable*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store %struct.hashtable* %0, %struct.hashtable** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load %struct.hashtable*, %struct.hashtable** %5
%11 = load i8*, i8** %6
%12 = load i8*, i8** %7
%13 = call i32 @ht_add(%struct.hashtable* %10, i8* %11, i8* %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %3
%17 = load i32, i32* %8
%18 = icmp ne i32 %17, 3
br i1 %18, label %19, label %21
19: ; preds = %16, %3
%20 = load i32, i32* %8
store i32 %20, i32* %4
br label %40
21: ; preds = %16
%22 = load %struct.hashtable*, %struct.hashtable** %5
%23 = load i8*, i8** %6
%24 = call i32 @ht_search(%struct.hashtable* %22, i8* %23, i32* %9)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 1
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load %struct.hashtable*, %struct.hashtable** %5
%30 = load i32, i32* %9
%31 = call i32 @ht_free(%struct.hashtable* %29, i32 %30)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp eq i32 %32, 0
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = load %struct.hashtable*, %struct.hashtable** %5
%37 = load i8*, i8** %6
%38 = load i8*, i8** %7
%39 = call i32 @ht_add(%struct.hashtable* %36, i8* %37, i8* %38)
store i32 %39, i32* %4
br label %40
40: ; preds = %21, %19
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
%struct.hashtable = type { i32 }
define i32 @ht_replace(%struct.hashtable* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32 @ht_add(%struct.hashtable* %0, i8* %1, i8* %2)
%7 = icmp eq i32 %6, 3
br i1 %7, label %8, label %19
8: ; preds = %3
%9 = call i32 @ht_search(%struct.hashtable* %0, i8* %1, i32* %4)
%10 = icmp eq i32 %9, 1
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i32, i32* %4
%14 = call i32 @ht_free(%struct.hashtable* %0, i32 %13)
%15 = icmp eq i32 %14, 0
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = call i32 @ht_add(%struct.hashtable* %0, i8* %1, i8* %2)
br label %19
19: ; preds = %3, %8
%20 = phi i32 [ %18, %8 ], [ %6, %3 ]
ret i32 %20
}