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

/* Variables and functions */
int /*<<< orphan*/ log_printf(int, char *);
int /*<<< orphan*/ *realloc(int /*<<< orphan*/ *, int);
scalar_t__ tid_exists(int /*<<< orphan*/ const, unsigned int,
                      int /*<<< orphan*/ *);
int tid_add(const pid_t tid, unsigned *tid_nr, pid_t **tid_map) {
  pid_t *tids;
  if (tid_exists(tid, *tid_nr, *tid_map))
    return 0;
  tids = realloc(*tid_map, sizeof(pid_t) * (*tid_nr + 1));
  if (tids == NULL) {
    log_printf((1 << 2), "ERROR: "
                         "TID map allocation error!\n");
    return 1;
  }
  tids[*tid_nr] = tid;
  (*tid_nr)++;
  *tid_map = tids;
  return 0;
}


x86 assembly (clang, -O0):
<tid_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
call   1030 <tid_exists@plt>
cmp    $0x0,%rax
je     116e <tid_add+0x3e>
movl   $0x0,-0x4(%rbp)
jmp    11e4 <tid_add+0xb4>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x2,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b7 <tid_add+0x87>
mov    $0x4,%edi
lea    0xe5a(%rip),%rsi
call   1040 <log_printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e4 <tid_add+0xb4>
mov    -0x8(%rbp),%edx
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%ecx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tid_add>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    (%rsi),%esi
mov    (%rdx),%rdx
call   1030 <tid_exists@plt>
xor    %r15d,%r15d
test   %rax,%rax
jne    118b <tid_add+0x5b>
mov    (%r14),%rdi
mov    (%rbx),%eax
lea    0x4(,%rax,4),%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     1174 <tid_add+0x44>
mov    (%rbx),%ecx
mov    %ebp,(%rax,%rcx,4)
addl   $0x1,(%rbx)
mov    %rax,(%r14)
jmp    118b <tid_add+0x5b>
lea    0xe85(%rip),%rsi
mov    $0x4,%edi
call   1040 <log_printf@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"ERROR: TID map allocation error!\0A\00"
define i32 @tid_add(i32 %0, i32* %1, i32** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32**
%8 = alloca i32*
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32** %2, i32*** %7
%9 = load i32, i32* %5
%10 = load i32*, i32** %6
%11 = load i32, i32* %10
%12 = load i32**, i32*** %7
%13 = load i32*, i32** %12
%14 = call i64 @tid_exists(i32 %9, i32 %11, i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 0, i32* %4
br label %43
17: ; preds = %3
%18 = load i32**, i32*** %7
%19 = load i32*, i32** %18
%20 = load i32*, i32** %6
%21 = load i32, i32* %20
%22 = add i32 %21, 1
%23 = zext i32 %22 to i64
%24 = mul i64 4, %23
%25 = trunc i64 %24 to i32
%26 = call i32* @realloc(i32* %19, i32 %25)
store i32* %26, i32** %8
%27 = load i32*, i32** %8
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %31
29: ; preds = %17
%30 = call i32 @log_printf(i32 4, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %43
31: ; preds = %17
%32 = load i32, i32* %5
%33 = load i32*, i32** %8
%34 = load i32*, i32** %6
%35 = load i32, i32* %34
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %33, i64 %36
store i32 %32, i32* %37
%38 = load i32*, i32** %6
%39 = load i32, i32* %38
%40 = add i32 %39, 1
store i32 %40, i32* %38
%41 = load i32*, i32** %8
%42 = load i32**, i32*** %7
store i32* %41, i32** %42
store i32 0, i32* %4
br label %43
43: ; preds = %31, %29, %16
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
@.str = [34 x i8] c"ERROR: TID map allocation error!\0A\00"
define i32 @tid_add(i32 %0, i32* nocapture %1, i32** nocapture %2) {
%4 = load i32, i32* %1
%5 = load i32*, i32** %2
%6 = tail call i64 @tid_exists(i32 %0, i32 %4, i32* %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %23
8: ; preds = %3
%9 = load i32*, i32** %2
%10 = load i32, i32* %1
%11 = shl i32 %10, 2
%12 = add i32 %11, 4
%13 = tail call i32* @realloc(i32* %9, i32 %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = tail call i32 @log_printf(i32 4, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %23
17: ; preds = %8
%18 = load i32, i32* %1
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %13, i64 %19
store i32 %0, i32* %20
%21 = load i32, i32* %1
%22 = add i32 %21, 1
store i32 %22, i32* %1
store i32* %13, i32** %2
br label %23
23: ; preds = %3, %17, %15
%24 = phi i32 [ 1, %15 ], [ 0, %17 ], [ 0, %3 ]
ret i32 %24
}