#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 */
unsigned long UINT_MAX;
int /*<<< orphan*/ die(char *);
char *getenv(char *);
long strtol(char const *, char **, int);
bool device_num(unsigned int *out) {
  const char *s = getenv("CLRUNON_DEVICE_NUM");
  char *end;
  if (s == NULL)
    return false;
  long v = strtol(s, &end, 10);
  if (end == s || *end || v < 0 || (unsigned long)v > UINT_MAX)
    die("CLRUNON_DEVICE_NUM"
        " was not set to a valid value\n");
  *out = v;
  return true;
}


x86 assembly (clang, -O0):
<device_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xebd(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1163 <device_num+0x33>
movl   $0x0,-0x4(%rbp)
jmp    11d5 <device_num+0xa5>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11b6 <device_num+0x86>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11b6 <device_num+0x86>
cmpq   $0x0,-0x28(%rbp)
jl     11b6 <device_num+0x86>
mov    -0x28(%rbp),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
jbe    11c2 <device_num+0x92>
lea    0xe56(%rip),%rdi
call   1050 <die@plt>
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<device_num>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xebd(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     119b <device_num+0x6b>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,%r15
mov    0x8(%rsp),%rax
cmp    %rbx,%rax
je     1185 <device_num+0x55>
cmpb   $0x0,(%rax)
jne    1185 <device_num+0x55>
test   %r15,%r15
js     1185 <device_num+0x55>
mov    0x2e68(%rip),%rax
cmp    (%rax),%r15
jbe    1191 <device_num+0x61>
lea    0xe87(%rip),%rdi
call   1050 <die@plt>
mov    %r15d,(%r14)
mov    $0x1,%eax
jmp    119d <device_num+0x6d>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [19 x i8] c"CLRUNON_DEVICE_NUM\00"
@UINT_MAX = global i64 0
@.str.1 = [49 x i8] c"CLRUNON_DEVICE_NUM was not set to a valid value\0A\00"
define i32 @device_num(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
store i32* %0, i32** %3
%7 = call i8* @getenv(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 0, i32* %2
br label %35
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = call i64 @strtol(i8* %12, i8** %5, i32 10)
store i64 %13, i64* %6
%14 = load i8*, i8** %5
%15 = load i8*, i8** %4
%16 = icmp eq i8* %14, %15
br i1 %16, label %29, label %17
17: ; preds = %11
%18 = load i8*, i8** %5
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br i1 %21, label %29, label %22
22: ; preds = %17
%23 = load i64, i64* %6
%24 = icmp slt i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %22
%26 = load i64, i64* %6
%27 = load i64, i64* @UINT_MAX
%28 = icmp ugt i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25, %22, %17, %11
%30 = call i32 @die(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
%32 = load i64, i64* %6
%33 = trunc i64 %32 to i32
%34 = load i32*, i32** %3
store i32 %33, i32* %34
store i32 1, i32* %2
br label %35
35: ; preds = %31, %10
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
@.str = [19 x i8] c"CLRUNON_DEVICE_NUM\00"
@UINT_MAX = global i64 0
@.str.1 = [49 x i8] c"CLRUNON_DEVICE_NUM was not set to a valid value\0A\00"
define i32 @device_num(i32* nocapture writeonly %0) {
%2 = alloca i8*
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%4 = bitcast i8** %2 to i8*
%5 = icmp eq i8* %3, null
br i1 %5, label %22, label %6
6: ; preds = %1
%7 = call i64 @strtol(i8* %3, i8** %2, i32 10)
%8 = load i8*, i8** %2
%9 = icmp eq i8* %8, %3
br i1 %9, label %18, label %10
10: ; preds = %6
%11 = load i8, i8* %8
%12 = icmp ne i8 %11, 0
%13 = icmp slt i64 %7, 0
%14 = select i1 %12, i1 true, i1 %13
%15 = load i64, i64* @UINT_MAX
%16 = icmp ugt i64 %7, %15
%17 = select i1 %14, i1 true, i1 %16
br i1 %17, label %18, label %20
18: ; preds = %10, %6
%19 = tail call i32 @die(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %10, %18
%21 = trunc i64 %7 to i32
store i32 %21, i32* %0
br label %22
22: ; preds = %1, %20
%23 = phi i32 [ 1, %20 ], [ 0, %1 ]
ret i32 %23
}