#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 */
void *__get_tcb();
int /*<<< orphan*/ __set_tcb(void *);
int /*<<< orphan*/ abort();
int /*<<< orphan*/ bcopy(void *, void *, int);
int /*<<< orphan*/ bzero(void *, int);
void *malloc(int);
void tcb_fixup(int mainthread) {
  void *newtcb, *oldtcb;
  newtcb = malloc((4 * sizeof(void *)) + (2 * sizeof(void *)));
  if (newtcb == NULL)
    abort();
  bzero(newtcb, (2 * sizeof(void *)));
  oldtcb = __get_tcb();
  bcopy(oldtcb, newtcb + (2 * sizeof(void *)), (4 * sizeof(void *)));
  __set_tcb(newtcb + (2 * sizeof(void *)));
}


x86 assembly (clang, -O0):
<tcb_fixup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x30,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118b <tcb_fixup+0x2b>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rdi
mov    $0x10,%esi
call   1080 <bzero@plt>
mov    $0x0,%al
call   1050 <__get_tcb@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
mov    $0x20,%edx
call   1070 <bcopy@plt>
mov    -0x10(%rbp),%rdi
add    $0x10,%rdi
call   1040 <__set_tcb@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcb_fixup>:
push   %rbx
mov    $0x30,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1193 <tcb_fixup+0x43>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x10,%esi
call   1070 <bzero@plt>
xor    %eax,%eax
call   1050 <__get_tcb@plt>
mov    %rbx,%rdi
add    $0x10,%rdi
movups (%rax),%xmm0
movups 0x10(%rax),%xmm1
movups %xmm0,0x10(%rbx)
movups %xmm1,0x20(%rbx)
pop    %rbx
jmp    1040 <__set_tcb@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
define void @tcb_fixup(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
store i32 %0, i32* %2
%5 = call i8* @malloc(i32 48)
store i8* %5, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 (...) @abort()
unreachable
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @bzero(i8* %11, i32 16)
%13 = call i8* (...) @__get_tcb()
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = load i8*, i8** %3
%16 = getelementptr i8, i8* %15, i64 16
%17 = call i32 @bcopy(i8* %14, i8* %16, i32 32)
%18 = load i8*, i8** %3
%19 = getelementptr i8, i8* %18, i64 16
%20 = call i32 @__set_tcb(i8* %19)
ret void
}

LLVM IR (-O2):
define void @tcb_fixup(i32 %0) {
%2 = tail call dereferenceable_or_null(48) i8* @malloc(i32 48)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 (...) @abort()
unreachable
6: ; preds = %1
%7 = tail call i32 @bzero(i8* %2, i32 16)
%8 = tail call i8* (...) @__get_tcb()
%9 = getelementptr i8, i8* %2, i64 16
tail call void @llvm.memmove.p0i8.p0i8.i32(i8* align 1 dereferenceable(32) %9, i8* align 1 dereferenceable(32) %8, i32 32, i1 false)
%10 = tail call i32 @__set_tcb(i8* %9)
ret void
}