#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 pthread_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int);
int pthread_self();
void *compute_prime(void *arg) {
  pthread_t currentThreadId = pthread_self();
  printf("work thread id : %lu\n", currentThreadId);
  printf("print the arg from the main thread : %d\n", *((int *)arg));
  int candidate = 2;
  int n = *((int *)arg);
  while (1) {
    int factor;
    int is_prime = 1;
    for (factor = 2; factor < candidate; ++factor)
      if (candidate % factor == 0) {
        is_prime = 0;
        printf("print the arg from the main thread : %d\n", *((int *)arg));
        break;
      }
    if (is_prime) {
      if (--n == 0) {
        printf("print the arg from the main thread : %d\n", *((int *)arg));
        return (void *)candidate;
      }
    }
    ++candidate;
  }
  printf("print the arg from the main thread : %d\n", *((int *)arg));
  return NULL;
}


x86 assembly (clang, -O0):
<compute_prime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <pthread_self@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xec0(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
lea    0xec4(%rip),%rdi
call   1030 <printf@plt>
movl   $0x2,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x2,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11c2 <compute_prime+0xa2>
mov    -0x10(%rbp),%eax
cltd
idivl  -0x18(%rbp)
cmp    $0x0,%edx
jne    11af <compute_prime+0x8f>
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
jmp    11c2 <compute_prime+0xa2>
jmp    11b4 <compute_prime+0x94>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1175 <compute_prime+0x55>
cmpl   $0x0,-0x1c(%rbp)
je     11ff <compute_prime+0xdf>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    11fa <compute_prime+0xda>
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
lea    0xe2b(%rip),%rdi
call   1030 <printf@plt>
movslq -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret
jmp    11ff <compute_prime+0xdf>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1167 <compute_prime+0x47>

x86 assembly (clang, -O2):
<compute_prime>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <pthread_self@plt>
lea    0xec5(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    (%r15),%esi
lea    0xeca(%rip),%r14
mov    %r14,%rdi
call   1030 <printf@plt>
mov    (%r15),%r12d
mov    $0x2,%ebp
jmp    116c <compute_prime+0x4c>
xchg   %ax,%ax
add    $0xffffffff,%r12d
je     11ad <compute_prime+0x8d>
add    $0x1,%ebp
add    $0xffffffff,%ebx
cmp    $0x3,%ebp
jb     1160 <compute_prime+0x40>
mov    $0x2,%ecx
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%eax
xor    %edx,%edx
div    %ecx
test   %edx,%edx
je     11a0 <compute_prime+0x80>
add    $0x1,%ecx
lea    (%rbx,%rcx,1),%eax
cmp    $0x2,%eax
jne    1180 <compute_prime+0x60>
jmp    1160 <compute_prime+0x40>
nopw   0x0(%rax,%rax,1)
mov    (%r15),%esi
mov    %r14,%rdi
call   1030 <printf@plt>
jmp    1166 <compute_prime+0x46>
mov    (%r15),%esi
lea    0xe5f(%rip),%rdi
call   1030 <printf@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"work thread id : %lu\0A\00"
@.str.1 = [41 x i8] c"print the arg from the main thread : %d\0A\00"
define i8* @compute_prime(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = call i32 (...) @pthread_self()
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i8*, i8** %2
%12 = bitcast i8* %11 to i32*
%13 = load i32, i32* %12
%14 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 2, i32* %4
%15 = load i8*, i8** %2
%16 = bitcast i8* %15 to i32*
%17 = load i32, i32* %16
store i32 %17, i32* %5
br label %18
18: ; preds = %1, %53
store i32 1, i32* %7
store i32 2, i32* %6
br label %19
19: ; preds = %34, %18
%20 = load i32, i32* %6
%21 = load i32, i32* %4
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %37
23: ; preds = %19
%24 = load i32, i32* %4
%25 = load i32, i32* %6
%26 = srem i32 %24, %25
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %23
store i32 0, i32* %7
%29 = load i8*, i8** %2
%30 = bitcast i8* %29 to i32*
%31 = load i32, i32* %30
%32 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %31)
br label %37
33: ; preds = %23
br label %34
34: ; preds = %33
%35 = load i32, i32* %6
%36 = add nsw i32 %35, 1
store i32 %36, i32* %6
br label %19
37: ; preds = %28, %19
%38 = load i32, i32* %7
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %53
40: ; preds = %37
%41 = load i32, i32* %5
%42 = add nsw i32 %41, -1
store i32 %42, i32* %5
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %52
44: ; preds = %40
%45 = load i8*, i8** %2
%46 = bitcast i8* %45 to i32*
%47 = load i32, i32* %46
%48 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %47)
%49 = load i32, i32* %4
%50 = sext i32 %49 to i64
%51 = inttoptr i64 %50 to i8*
ret i8* %51
52: ; preds = %40
br label %53
53: ; preds = %52, %37
%54 = load i32, i32* %4
%55 = add nsw i32 %54, 1
store i32 %55, i32* %4
br label %18
}

LLVM IR (-O2):
@.str = [22 x i8] c"work thread id : %lu\0A\00"
@.str.1 = [41 x i8] c"print the arg from the main thread : %d\0A\00"
define i8* @compute_prime(i8* nocapture %0) {
%2 = tail call i32 (...) @pthread_self()
%3 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = bitcast i8* %0 to i32*
%5 = load i32, i32* %4
%6 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = load i32, i32* %4
br label %8
8: ; preds = %25, %1
%9 = phi i32 [ 2, %1 ], [ %27, %25 ]
%10 = phi i32 [ %7, %1 ], [ %26, %25 ]
%11 = icmp ugt i32 %9, 2
br i1 %11, label %14, label %22
12: ; preds = %14
%13 = icmp eq i32 %18, %9
br i1 %13, label %22, label %14
14: ; preds = %8, %12
%15 = phi i32 [ %18, %12 ], [ 2, %8 ]
%16 = urem i32 %9, %15
%17 = icmp eq i32 %16, 0
%18 = add nuw nsw i32 %15, 1
br i1 %17, label %19, label %12
19: ; preds = %14
%20 = load i32, i32* %4
%21 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %25
22: ; preds = %12, %8
%23 = add nsw i32 %10, -1
%24 = icmp eq i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22, %19
%26 = phi i32 [ %23, %22 ], [ %10, %19 ]
%27 = add nuw nsw i32 %9, 1
br label %8
28: ; preds = %22
%29 = load i32, i32* %4
%30 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = zext i32 %9 to i64
%32 = inttoptr i64 %31 to i8*
ret i8* %32
}