#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 */
int /*<<< orphan*/ SYS_gettid;
int getpid();
int /*<<< orphan*/ ioctl(int, int, int);
int /*<<< orphan*/ printf(char *, ...);
int syscall(int /*<<< orphan*/);
void *thread_function(void *fd) {
  printf("I am in thread function\n");
  printf("TGID --> %d\n", getpid());
  printf("PID --> %ld\n", syscall(SYS_gettid));
  ioctl(*((int *)fd), syscall(SYS_gettid), 0000);
  return NULL;
}


x86 assembly (clang, -O0):
<thread_function>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xead(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xeaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
call   1060 <syscall@plt>
mov    %eax,%esi
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1060 <syscall@plt>
mov    -0xc(%rbp),%edi
mov    %eax,%esi
xor    %edx,%edx
call   1050 <ioctl@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_function>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xebc(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe8f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e67(%rip),%r14
mov    (%r14),%edi
call   1070 <syscall@plt>
lea    0xe7d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rbx),%ebx
mov    (%r14),%edi
call   1070 <syscall@plt>
mov    %ebx,%edi
mov    %eax,%esi
xor    %edx,%edx
call   1060 <ioctl@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [25 x i8] c"I am in thread function\0A\00"
@.str.1 = [13 x i8] c"TGID --> %d\0A\00"
@.str.2 = [13 x i8] c"PID --> %ld\0A\00"
@SYS_gettid = global i32 0
define i8* @thread_function(i8* %0) {
%2 = alloca i8*
store i8* %0, i8** %2
%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (...) @getpid()
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @SYS_gettid
%7 = call i32 @syscall(i32 %6)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%9 = load i8*, i8** %2
%10 = bitcast i8* %9 to i32*
%11 = load i32, i32* %10
%12 = load i32, i32* @SYS_gettid
%13 = call i32 @syscall(i32 %12)
%14 = call i32 @ioctl(i32 %11, i32 %13, i32 0)
ret i8* null
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c"TGID --> %d\0A\00"
@.str.2 = [13 x i8] c"PID --> %ld\0A\00"
@SYS_gettid = global i32 0
@str = [24 x i8] c"I am in thread function\00"
define noalias i8* @thread_function(i8* nocapture %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @SYS_gettid
%6 = tail call i32 @syscall(i32 %5)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %6)
%8 = bitcast i8* %0 to i32*
%9 = load i32, i32* %8
%10 = load i32, i32* @SYS_gettid
%11 = tail call i32 @syscall(i32 %10)
%12 = tail call i32 @ioctl(i32 %9, i32 %11, i32 0)
ret i8* null
}