#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 /*<<< orphan*/ lines;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ cg_enter_current_thread(char *);
scalar_t__ proc_read_strstr(int /*<<< orphan*/, int, char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int strlen(char *);
void *migrating_thread_fn(void *arg) {
  int g, i, n_iterations = 1000;
  char **grps = arg;
  char lines[3][PATH_MAX];
  for (g = 1; g < 3; ++g)
    snprintf(lines[g], sizeof(lines[g]), "0::%s", grps[g] + strlen(grps[0]));
  for (i = 0; i < n_iterations; ++i) {
    cg_enter_current_thread(grps[(i % 2) + 1]);
    if (proc_read_strstr(0, 1, "cgroup", lines[(i % 2) + 1]))
      return (void *)-1;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<migrating_thread_fn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x3e8,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
lea    0xf(%rax,%rax,2),%rdx
movabs $0x7fffffff0,%rcx
and    %rcx,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jge    1203 <migrating_thread_fn+0xc3>
mov    -0x50(%rbp),%rax
mov    -0x48(%rbp),%rcx
movslq -0x14(%rbp),%rdx
imul   %rax,%rdx
add    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %eax,-0x54(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    -0x68(%rbp),%rcx
mov    -0x60(%rbp),%rdi
mov    -0x54(%rbp),%esi
cltq
add    %rax,%rcx
lea    0xe10(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1199 <migrating_thread_fn+0x59>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12a4 <migrating_thread_fn+0x164>
mov    -0x28(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x70(%rbp),%rax
add    $0x1,%edx
movslq %edx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <cg_enter_current_thread@plt>
mov    -0x48(%rbp),%rcx
mov    -0x18(%rbp),%eax
mov    $0x2,%esi
cltd
idiv   %esi
mov    %edx,%eax
mov    -0x50(%rbp),%rdx
add    $0x1,%eax
cltq
imul   %rdx,%rax
add    %rax,%rcx
xor    %edi,%edi
mov    $0x1,%esi
lea    0xd9b(%rip),%rdx
call   1060 <proc_read_strstr@plt>
cmp    $0x0,%rax
je     1291 <migrating_thread_fn+0x151>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12b3 <migrating_thread_fn+0x173>
jmp    1296 <migrating_thread_fn+0x156>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    120a <migrating_thread_fn+0xca>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<migrating_thread_fn>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e98(%rip),%rax
mov    (%rax),%r13d
lea    0xf(,%r13,2),%rax
add    %r13,%rax
movabs $0x7fffffff0,%rcx
and    %rax,%rcx
mov    %rsp,%r15
sub    %rcx,%r15
mov    %r15,%rsp
lea    (%r15,%r13,1),%r12
mov    (%rdi),%rdi
mov    0x8(%r14),%rbx
call   1030 <strlen@plt>
movslq %eax,%rcx
add    %rbx,%rcx
lea    0xe67(%rip),%rdx
mov    %r12,%rdi
mov    %r13d,%esi
call   1050 <snprintf@plt>
lea    (%r15,%r13,2),%r12
mov    (%r14),%rdi
mov    0x10(%r14),%rbx
call   1030 <strlen@plt>
movslq %eax,%rcx
add    %rbx,%rcx
mov    %r12,%rdi
mov    %r13d,%esi
lea    0xe39(%rip),%rdx
call   1050 <snprintf@plt>
xor    %r12d,%r12d
nop
mov    %r12d,%ebx
and    $0x1,%ebx
mov    0x8(%r14,%rbx,8),%rdi
add    $0x1,%rbx
call   1040 <cg_enter_current_thread@plt>
imul   %r13,%rbx
add    %r15,%rbx
xor    %edi,%edi
mov    $0x1,%esi
lea    0xe0d(%rip),%rdx
mov    %rbx,%rcx
call   1060 <proc_read_strstr@plt>
test   %rax,%rax
jne    1217 <migrating_thread_fn+0xd7>
add    $0x1,%r12d
cmp    $0x3e8,%r12d
jne    11d0 <migrating_thread_fn+0x90>
xor    %eax,%eax
jmp    121e <migrating_thread_fn+0xde>
mov    $0xffffffffffffffff,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [6 x i8] c"0::%s\00"
@.str.1 = [7 x i8] c"cgroup\00"
define i8* @migrating_thread_fn(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %3
store i32 1000, i32* %6
%11 = load i8*, i8** %3
%12 = bitcast i8* %11 to i8**
store i8** %12, i8*** %7
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %8
%16 = mul nuw i64 3, %14
%17 = alloca i8, i64 %16
store i64 %14, i64* %9
store i32 1, i32* %4
br label %18
18: ; preds = %43, %1
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 3
br i1 %20, label %21, label %46
21: ; preds = %18
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = mul nsw i64 %23, %14
%25 = getelementptr inbounds i8, i8* %17, i64 %24
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = mul nsw i64 %27, %14
%29 = getelementptr inbounds i8, i8* %17, i64 %28
%30 = trunc i64 %14 to i32
%31 = load i8**, i8*** %7
%32 = load i32, i32* %4
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %31, i64 %33
%35 = load i8*, i8** %34
%36 = load i8**, i8*** %7
%37 = getelementptr inbounds i8*, i8** %36, i64 0
%38 = load i8*, i8** %37
%39 = call i32 @strlen(i8* %38)
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %35, i64 %40
%42 = call i32 @snprintf(i8* %25, i32 %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %21
%44 = load i32, i32* %4
%45 = add nsw i32 %44, 1
store i32 %45, i32* %4
br label %18
46: ; preds = %18
store i32 0, i32* %5
br label %47
47: ; preds = %70, %46
%48 = load i32, i32* %5
%49 = load i32, i32* %6
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %73
51: ; preds = %47
%52 = load i8**, i8*** %7
%53 = load i32, i32* %5
%54 = srem i32 %53, 2
%55 = add nsw i32 %54, 1
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8*, i8** %52, i64 %56
%58 = load i8*, i8** %57
%59 = call i32 @cg_enter_current_thread(i8* %58)
%60 = load i32, i32* %5
%61 = srem i32 %60, 2
%62 = add nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = mul nsw i64 %63, %14
%65 = getelementptr inbounds i8, i8* %17, i64 %64
%66 = call i64 @proc_read_strstr(i32 0, i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %51
store i8* inttoptr (i64 -1 to i8*), i8** %2
store i32 1, i32* %10
br label %74
69: ; preds = %51
br label %70
70: ; preds = %69
%71 = load i32, i32* %5
%72 = add nsw i32 %71, 1
store i32 %72, i32* %5
br label %47
73: ; preds = %47
store i8* null, i8** %2
store i32 1, i32* %10
br label %74
74: ; preds = %73, %68
%75 = load i8*, i8** %8
%76 = load i8*, i8** %2
ret i8* %76
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [6 x i8] c"0::%s\00"
@.str.1 = [7 x i8] c"cgroup\00"
define i8* @migrating_thread_fn(i8* nocapture %0) {
%2 = bitcast i8* %0 to i8**
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = mul nuw nsw i64 %4, 3
%6 = alloca i8, i64 %5
%7 = getelementptr inbounds i8, i8* %6, i64 %4
%8 = getelementptr inbounds i8, i8* %0, i64 8
%9 = bitcast i8* %8 to i8**
%10 = load i8*, i8** %9
%11 = load i8*, i8** %2
%12 = call i32 @strlen(i8* %11)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %10, i64 %13
%15 = call i32 @snprintf(i8* %7, i32 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = shl nuw nsw i64 %4, 1
%17 = getelementptr inbounds i8, i8* %6, i64 %16
%18 = getelementptr inbounds i8, i8* %0, i64 16
%19 = bitcast i8* %18 to i8**
%20 = load i8*, i8** %19
%21 = load i8*, i8** %2
%22 = call i32 @strlen(i8* %21)
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = call i32 @snprintf(i8* %17, i32 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %29
26: ; preds = %29
%27 = add nuw nsw i32 %30, 1
%28 = icmp eq i32 %27, 1000
br i1 %28, label %41, label %29
29: ; preds = %1, %26
%30 = phi i32 [ %27, %26 ], [ 0, %1 ]
%31 = and i32 %30, 1
%32 = add nuw nsw i32 %31, 1
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %2, i64 %33
%35 = load i8*, i8** %34
%36 = call i32 @cg_enter_current_thread(i8* %35)
%37 = mul nuw nsw i64 %33, %4
%38 = getelementptr inbounds i8, i8* %6, i64 %37
%39 = call i64 @proc_read_strstr(i32 0, i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %26, label %41
41: ; preds = %26, %29
%42 = phi i8* [ inttoptr (i64 -1 to i8*), %29 ], [ null, %26 ]
ret i8* %42
}