#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ SYS_getpid;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
scalar_t__ getpid();
scalar_t__ gettid();
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ syscall(int /*<<< orphan*/);
int gettid_test(void) {
  printf("gettid():\n");
  fflush(NULL);
  pid_t pid = getpid();
  pid_t sys_getpid = syscall(SYS_getpid);
  if (pid != sys_getpid) {
    printf("\tFailed Phase 1: getpid() returned %d, "
           "while syscall(SYS_getpid) returned %d!\n",
           pid, sys_getpid);
    return -1;
  }
  pid_t tid = gettid();
  if (tid != pid) {
    printf("\tFailed Phase 1: getpid() returned %d, "
           "while syscall(SYS_gettid) returned %d!\n",
           pid, tid);
    return -1;
  }
  printf("\t\tpid = %d, tid = %d\n", pid, tid);
  printf("\tSucceeded Phase 1\n");
  fflush(NULL);
  return 0;
}


x86 assembly (clang, -O0):
<gettid_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <fflush@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
call   1060 <syscall@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11bc <gettid_test+0x6c>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <gettid_test+0xdb>
mov    $0x0,%al
call   1030 <gettid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11f7 <gettid_test+0xa7>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <gettid_test+0xdb>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xeab(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <fflush@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gettid_test>:
push   %r14
push   %rbx
push   %rax
lea    0xf49(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1080 <fflush@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rax,%rbx
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1070 <syscall@plt>
cmp    %rax,%rbx
jne    11cc <gettid_test+0x6c>
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <gettid@plt>
cmp    %rbx,%rax
jne    11d5 <gettid_test+0x75>
lea    0xef4(%rip),%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xf00(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1080 <fflush@plt>
jmp    11ef <gettid_test+0x8f>
lea    0xe2d(%rip),%rdi
jmp    11dc <gettid_test+0x7c>
lea    0xe73(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [11 x i8] c"gettid():\0A\00"
@SYS_getpid = global i32 0
@.str.1 = [79 x i8] c"\09Failed Phase 1: getpid() returned %d, while syscall(SYS_getpid) returned %d!\0A\00"
@.str.2 = [79 x i8] c"\09Failed Phase 1: getpid() returned %d, while syscall(SYS_gettid) returned %d!\0A\00"
@.str.3 = [22 x i8] c"\09\09pid = %d, tid = %d\0A\00"
@.str.4 = [20 x i8] c"\09Succeeded Phase 1\0A\00"
define i32 @gettid_test() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @fflush(i32* null)
%7 = call i64 (...) @getpid()
store i64 %7, i64* %2
%8 = load i32, i32* @SYS_getpid
%9 = call i64 @syscall(i32 %8)
store i64 %9, i64* %3
%10 = load i64, i64* %2
%11 = load i64, i64* %3
%12 = icmp ne i64 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %0
%14 = load i64, i64* %2
%15 = load i64, i64* %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.1, i64 0, i64 0), i64 %14, i64 %15)
store i32 -1, i32* %1
br label %32
17: ; preds = %0
%18 = call i64 (...) @gettid()
store i64 %18, i64* %4
%19 = load i64, i64* %4
%20 = load i64, i64* %2
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i64, i64* %2
%24 = load i64, i64* %4
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.2, i64 0, i64 0), i64 %23, i64 %24)
store i32 -1, i32* %1
br label %32
26: ; preds = %17
%27 = load i64, i64* %2
%28 = load i64, i64* %4
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %27, i64 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32 @fflush(i32* null)
store i32 0, i32* %1
br label %32
32: ; preds = %26, %22, %13
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@SYS_getpid = global i32 0
@.str.1 = [79 x i8] c"\09Failed Phase 1: getpid() returned %d, while syscall(SYS_getpid) returned %d!\0A\00"
@.str.2 = [79 x i8] c"\09Failed Phase 1: getpid() returned %d, while syscall(SYS_gettid) returned %d!\0A\00"
@.str.3 = [22 x i8] c"\09\09pid = %d, tid = %d\0A\00"
@str = [10 x i8] c"gettid():\00"
@str.5 = [19 x i8] c"\09Succeeded Phase 1\00"
define i32 @gettid_test() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 @fflush(i32* null)
%3 = tail call i64 (...) @getpid()
%4 = load i32, i32* @SYS_getpid
%5 = tail call i64 @syscall(i32 %4)
%6 = icmp eq i64 %3, %5
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([79 x i8], [79 x i8]* @.str.1, i64 0, i64 0), i64 %3, i64 %5)
br label %18
9: ; preds = %0
%10 = tail call i64 (...) @gettid()
%11 = icmp eq i64 %10, %3
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([79 x i8], [79 x i8]* @.str.2, i64 0, i64 0), i64 %3, i64 %10)
br label %18
14: ; preds = %9
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i64 %3, i64 %3)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%17 = tail call i32 @fflush(i32* null)
br label %18
18: ; preds = %12, %14, %7
%19 = phi i32 [ -1, %7 ], [ -1, %12 ], [ 0, %14 ]
ret i32 %19
}