#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 triple {
  int dummy;
};
struct compile_state {
  int dummy;
};

/* Variables and functions */
struct triple **triple_targ(struct compile_state *, struct triple *,
                            struct triple **);
int /*<<< orphan*/ unuse_triple(struct triple *, struct triple *);
int /*<<< orphan*/ use_triple(struct triple *, struct triple *);
int replace_targ_use(struct compile_state *state, struct triple *orig,
                     struct triple *new, struct triple *use) {
  struct triple **expr;
  int found;
  found = 0;
  expr = triple_targ(state, use, 0);
  for (; expr; expr = triple_targ(state, use, expr)) {
    if (*expr == orig) {
      *expr = new;
      found = 1;
    }
  }
  if (found) {
    unuse_triple(orig, use);
    use_triple(new, use);
  }
  return found;
}


x86 assembly (clang, -O0):
<replace_targ_use>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <triple_targ@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11b1 <replace_targ_use+0x81>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
cmp    -0x10(%rbp),%rax
jne    1192 <replace_targ_use+0x62>
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x2c(%rbp)
jmp    1197 <replace_targ_use+0x67>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <triple_targ@plt>
mov    %rax,-0x28(%rbp)
jmp    1164 <replace_targ_use+0x34>
cmpl   $0x0,-0x2c(%rbp)
je     11d5 <replace_targ_use+0xa5>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <unuse_triple@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <use_triple@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_targ_use>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r12
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%rbx
xor    %r14d,%r14d
mov    %rcx,%rsi
xor    %edx,%edx
call   1050 <triple_targ@plt>
test   %rax,%rax
je     11a3 <replace_targ_use+0x73>
xor    %r14d,%r14d
jmp    1173 <replace_targ_use+0x43>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %rax,%rdx
call   1050 <triple_targ@plt>
test   %rax,%rax
je     1183 <replace_targ_use+0x53>
cmp    %r13,(%rax)
jne    1160 <replace_targ_use+0x30>
mov    %r15,(%rax)
mov    $0x1,%r14d
jmp    1160 <replace_targ_use+0x30>
test   %r14d,%r14d
je     11a0 <replace_targ_use+0x70>
mov    %r13,%rdi
mov    %r12,%rsi
call   1030 <unuse_triple@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <use_triple@plt>
jmp    11a3 <replace_targ_use+0x73>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.compile_state = type { i32 }
%struct.triple = type { i32 }
define i32 @replace_targ_use(%struct.compile_state* %0, %struct.triple* %1, %struct.triple* %2, %struct.triple* %3) {
%5 = tail call %struct.triple** @triple_targ(%struct.compile_state* %0, %struct.triple* %3, %struct.triple** null)
%6 = icmp eq %struct.triple** %5, null
br i1 %6, label %22, label %7
7: ; preds = %4, %13
%8 = phi i32 [ %14, %13 ], [ 0, %4 ]
%9 = phi %struct.triple** [ %15, %13 ], [ %5, %4 ]
%10 = load %struct.triple*, %struct.triple** %9
%11 = icmp eq %struct.triple* %10, %1
br i1 %11, label %12, label %13
12: ; preds = %7
store %struct.triple* %2, %struct.triple** %9
br label %13
13: ; preds = %7, %12
%14 = phi i32 [ 1, %12 ], [ %8, %7 ]
%15 = tail call %struct.triple** @triple_targ(%struct.compile_state* %0, %struct.triple* %3, %struct.triple** %9)
%16 = icmp eq %struct.triple** %15, null
br i1 %16, label %17, label %7
17: ; preds = %13
%18 = icmp eq i32 %14, 0
br i1 %18, label %22, label %19
19: ; preds = %17
%20 = tail call i32 @unuse_triple(%struct.triple* %1, %struct.triple* %3)
%21 = tail call i32 @use_triple(%struct.triple* %2, %struct.triple* %3)
br label %22
22: ; preds = %4, %19, %17
%23 = phi i32 [ %14, %19 ], [ 0, %17 ], [ 0, %4 ]
ret i32 %23
}