#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 /*<<< orphan*/ _SC_NPROCESSORS_ONLN;
int atoi(char *);
char *getenv(char *);
int sysconf(int /*<<< orphan*/);
int get_thread_number(void) {
  char *thread_number = getenv("OMP_NUM_THREADS");
  int nthreads = 0;
  if (thread_number) {
    nthreads = atoi(thread_number);
  }
  if (nthreads < 1) {
    nthreads = sysconf(_SC_NPROCESSORS_ONLN);
  }
  if (nthreads < 1) {
    nthreads = 1;
  }
  return nthreads;
}


x86 assembly (clang, -O0):
<get_thread_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xec1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1166 <get_thread_number+0x36>
mov    -0x8(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
jge    1181 <get_thread_number+0x51>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
jge    1192 <get_thread_number+0x62>
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_thread_number>:
push   %rax
lea    0xec8(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     114e <get_thread_number+0x1e>
mov    %rax,%rdi
call   1050 <atoi@plt>
test   %eax,%eax
jg     115c <get_thread_number+0x2c>
mov    0x2e93(%rip),%rax
mov    (%rax),%edi
call   1040 <sysconf@plt>
test   %eax,%eax
mov    $0x1,%ecx
cmovg  %eax,%ecx
mov    %ecx,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [16 x i8] c"OMP_NUM_THREADS\00"
@_SC_NPROCESSORS_ONLN = global i32 0
define i32 @get_thread_number() {
%1 = alloca i8*
%2 = alloca i32
%3 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* %3, i8** %1
store i32 0, i32* %2
%4 = load i8*, i8** %1
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i8*, i8** %1
%8 = call i32 @atoi(i8* %7)
store i32 %8, i32* %2
br label %9
9: ; preds = %6, %0
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @_SC_NPROCESSORS_ONLN
%14 = call i32 @sysconf(i32 %13)
store i32 %14, i32* %2
br label %15
15: ; preds = %12, %9
%16 = load i32, i32* %2
%17 = icmp slt i32 %16, 1
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 1, i32* %2
br label %19
19: ; preds = %18, %15
%20 = load i32, i32* %2
ret i32 %20
}

LLVM IR (-O2):
@.str = [16 x i8] c"OMP_NUM_THREADS\00"
@_SC_NPROCESSORS_ONLN = global i32 0
define i32 @get_thread_number() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %6, label %3
3: ; preds = %0
%4 = tail call i32 @atoi(i8* %1)
%5 = icmp slt i32 %4, 1
br i1 %5, label %6, label %9
6: ; preds = %0, %3
%7 = load i32, i32* @_SC_NPROCESSORS_ONLN
%8 = tail call i32 @sysconf(i32 %7)
br label %9
9: ; preds = %6, %3
%10 = phi i32 [ %8, %6 ], [ %4, %3 ]
%11 = icmp sgt i32 %10, 1
%12 = select i1 %11, i32 %10, i32 1
ret i32 %12
}