#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 offline_thread(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, char *);
int set_all_threads_off(int cpu, int smt_state) {
  int i;
  char path[128];
  int rc = 0;
  for (i = cpu + smt_state - 1; i >= cpu; i--) {
    snprintf(path, 128,
             "/sys/devices/system/cpu/cpu%d"
             "/%s",
             i, "online");
    rc = offline_thread(path);
    if (rc == -1)
      printf("Unable to take cpu%d offline", i);
  }
  return rc;
}


x86 assembly (clang, -O0):
<set_all_threads_off>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x94(%rbp)
mov    -0x4(%rbp),%eax
add    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jl     11c6 <set_all_threads_off+0x96>
lea    -0x90(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x80,%esi
lea    0xe87(%rip),%rdx
lea    0xea1(%rip),%r8
call   1040 <snprintf@plt>
lea    -0x90(%rbp),%rdi
call   1050 <offline_thread@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0xffffffff,-0x94(%rbp)
jne    11b3 <set_all_threads_off+0x83>
mov    -0xc(%rbp),%esi
lea    0xe7a(%rip),%rdi
call   1030 <printf@plt>
jmp    11b8 <set_all_threads_off+0x88>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1157 <set_all_threads_off+0x27>
mov    -0x94(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_all_threads_off>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
test   %esi,%esi
jle    119d <set_all_threads_off+0x6d>
mov    %esi,%ebx
mov    %edi,%r12d
add    %edi,%ebx
lea    0xead(%rip),%r14
lea    0xec7(%rip),%r15
mov    %rsp,%rbp
jmp    1165 <set_all_threads_off+0x35>
nop
cmp    %r12d,%ebx
jle    11a0 <set_all_threads_off+0x70>
add    $0xffffffff,%ebx
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
mov    %r15,%r8
call   1040 <snprintf@plt>
mov    %rbp,%rdi
call   1050 <offline_thread@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    1160 <set_all_threads_off+0x30>
lea    0xe94(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
jmp    1160 <set_all_threads_off+0x30>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"/sys/devices/system/cpu/cpu%d/%s\00"
@.str.1 = [7 x i8] c"online\00"
@.str.2 = [29 x i8] c"Unable to take cpu%d offline\00"
define i32 @set_all_threads_off(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %7
%8 = load i32, i32* %3
%9 = load i32, i32* %4
%10 = add nsw i32 %8, %9
%11 = sub nsw i32 %10, 1
store i32 %11, i32* %5
br label %12
12: ; preds = %28, %2
%13 = load i32, i32* %5
%14 = load i32, i32* %3
%15 = icmp sge i32 %13, %14
br i1 %15, label %16, label %31
16: ; preds = %12
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%18 = load i32, i32* %5
%19 = call i32 @snprintf(i8* %17, i32 128, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%21 = call i32 @offline_thread(i8* %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load i32, i32* %5
%26 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %24, %16
br label %28
28: ; preds = %27
%29 = load i32, i32* %5
%30 = add nsw i32 %29, -1
store i32 %30, i32* %5
br label %12
31: ; preds = %12
%32 = load i32, i32* %7
ret i32 %32
}

LLVM IR (-O2):
@.str = [33 x i8] c"/sys/devices/system/cpu/cpu%d/%s\00"
@.str.1 = [7 x i8] c"online\00"
@.str.2 = [29 x i8] c"Unable to take cpu%d offline\00"
define i32 @set_all_threads_off(i32 %0, i32 %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %18
6: ; preds = %2
%7 = add nsw i32 %1, %0
br label %8
8: ; preds = %6, %16
%9 = phi i32 [ %10, %16 ], [ %7, %6 ]
%10 = add nsw i32 %9, -1
%11 = call i32 @snprintf(i8* %4, i32 128, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @offline_thread(i8* %4)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %10)
br label %16
16: ; preds = %8, %14
%17 = icmp sgt i32 %10, %0
br i1 %17, label %8, label %18
18: ; preds = %16, %2
%19 = phi i32 [ 0, %2 ], [ %12, %16 ]
ret i32 %19
}