#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 size_t ssize_t;
typedef int pid_t;
typedef int /*<<< orphan*/ buf2;
typedef int /*<<< orphan*/ buf1;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
size_t readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
int streq(char *, char *);
size_t strlen(char *);
int sys_getpid();
int sys_gettid();
int f(void *arg) {
  char buf1[64], buf2[64];
  pid_t pid, tid;
  ssize_t rv;
  pid = sys_getpid();
  tid = sys_gettid();
  snprintf(buf1, sizeof(buf1), "%u/task/%u", pid, tid);
  rv = readlink("/proc/thread-self", buf2, sizeof(buf2));
  assert(rv == strlen(buf1));
  buf2[rv] = '\0';
  assert(streq(buf1, buf2));
  if (arg)
    exit(0);
  return 0;
}


x86 assembly (clang, -O0):
<f>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1030 <sys_getpid@plt>
mov    %eax,-0x94(%rbp)
mov    $0x0,%al
call   1090 <sys_gettid@plt>
mov    %eax,-0x98(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x94(%rbp),%ecx
mov    -0x98(%rbp),%r8d
mov    $0x40,%esi
lea    0xe3a(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x90(%rbp),%rsi
lea    0xe32(%rip),%rdi
mov    $0x40,%edx
call   1040 <readlink@plt>
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rax
mov    %rax,-0xa8(%rbp)
lea    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0xa8(%rbp),%rax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0xa0(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
lea    -0x50(%rbp),%rdi
lea    -0x90(%rbp),%rsi
call   1070 <streq@plt>
mov    %eax,%edi
call   1080 <assert@plt>
cmpq   $0x0,-0x8(%rbp)
je     1253 <f+0xd3>
xor    %edi,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<f>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   1030 <sys_getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1090 <sys_gettid@plt>
lea    0xe59(%rip),%rdx
lea    0x40(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %ebp,%ecx
mov    %eax,%r8d
call   1060 <snprintf@plt>
lea    0xe46(%rip),%rdi
mov    %rsp,%r15
mov    %r15,%rsi
mov    $0x40,%edx
call   1040 <readlink@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
xor    %edi,%edi
cmp    %rax,%rbp
sete   %dil
call   1080 <assert@plt>
movb   $0x0,(%rsp,%rbp,1)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <streq@plt>
mov    %eax,%edi
call   1080 <assert@plt>
test   %r14,%r14
jne    1219 <f+0x99>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"%u/task/%u\00"
@.str.1 = [18 x i8] c"/proc/thread-self\00"
define i32 @f(i8* %0) {
%2 = alloca i8*
%3 = alloca [64 x i8]
%4 = alloca [64 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %2
%8 = call i32 (...) @sys_getpid()
store i32 %8, i32* %5
%9 = call i32 (...) @sys_gettid()
store i32 %9, i32* %6
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%11 = load i32, i32* %5
%12 = load i32, i32* %6
%13 = call i32 @snprintf(i8* %10, i32 64, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%15 = call i64 @readlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %14, i32 64)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%18 = call i64 @strlen(i8* %17)
%19 = icmp eq i64 %16, %18
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i64, i64* %7
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 %22
store i8 0, i8* %23
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%26 = call i32 @streq(i8* %24, i8* %25)
%27 = call i32 @assert(i32 %26)
%28 = load i8*, i8** %2
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %1
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %1
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"%u/task/%u\00"
@.str.1 = [18 x i8] c"/proc/thread-self\00"
define i32 @f(i8* readnone %0) {
%2 = alloca [64 x i8]
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%6 = tail call i32 (...) @sys_getpid()
%7 = tail call i32 (...) @sys_gettid()
%8 = call i32 @snprintf(i8* %4, i32 64, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
%9 = call i64 @readlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %5, i32 64)
%10 = call i64 @strlen(i8* %4)
%11 = icmp eq i64 %9, %10
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 %9
store i8 0, i8* %14
%15 = call i32 @streq(i8* %4, i8* %5)
%16 = call i32 @assert(i32 %15)
%17 = icmp eq i8* %0, null
br i1 %17, label %20, label %18
18: ; preds = %1
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %1
ret i32 0
}