#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 */
scalar_t__ errno;
char *getenv(char const *);
unsigned long strtoul(char *, char **, int /*<<< orphan*/);
unsigned env_var_as_unsigned(const char *var_name, unsigned default_value) {
  char *str = getenv(var_name);
  if (str) {
    char *end;
    unsigned long result;
    errno = 0;
    result = strtoul(str, &end, 0);
    if (errno == 0 && end != str && *end == '\0')
      return result;
  }
  return default_value;
}


x86 assembly (clang, -O0):
<env_var_as_unsigned>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a8 <env_var_as_unsigned+0x88>
mov    0x2e92(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1040 <strtoul@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e71(%rip),%rax
cmpq   $0x0,(%rax)
jne    11a3 <env_var_as_unsigned+0x83>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11a3 <env_var_as_unsigned+0x83>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11a3 <env_var_as_unsigned+0x83>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11ae <env_var_as_unsigned+0x8e>
jmp    11a8 <env_var_as_unsigned+0x88>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<env_var_as_unsigned>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
call   1030 <getenv@plt>
test   %rax,%rax
je     115a <env_var_as_unsigned+0x3a>
mov    %rax,%rbx
mov    0x2ea2(%rip),%r14
movq   $0x0,(%r14)
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1040 <strtoul@plt>
cmpq   $0x0,(%r14)
je     1165 <env_var_as_unsigned+0x45>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x8(%rsp),%rcx
cmp    %rbx,%rcx
je     115a <env_var_as_unsigned+0x3a>
cmpb   $0x0,(%rcx)
jne    115a <env_var_as_unsigned+0x3a>
mov    %eax,%ebp
jmp    115a <env_var_as_unsigned+0x3a>

LLVM IR (-O0):
@errno = global i64 0
define i32 @env_var_as_unsigned(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call i8* @getenv(i8* %9)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %31
13: ; preds = %2
store i64 0, i64* @errno
%14 = load i8*, i8** %6
%15 = call i64 @strtoul(i8* %14, i8** %7, i32 0)
store i64 %15, i64* %8
%16 = load i64, i64* @errno
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %13
%19 = load i8*, i8** %7
%20 = load i8*, i8** %6
%21 = icmp ne i8* %19, %20
br i1 %21, label %22, label %30
22: ; preds = %18
%23 = load i8*, i8** %7
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i64, i64* %8
%29 = trunc i64 %28 to i32
store i32 %29, i32* %3
br label %33
30: ; preds = %22, %18, %13
br label %31
31: ; preds = %30, %2
%32 = load i32, i32* %5
store i32 %32, i32* %3
br label %33
33: ; preds = %31, %27
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@errno = global i64 0
define i32 @env_var_as_unsigned(i8* nocapture %0, i32 %1) {
%3 = alloca i8*
%4 = tail call i8* @getenv(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %19, label %6
6: ; preds = %2
%7 = bitcast i8** %3 to i8*
store i64 0, i64* @errno
%8 = call i64 @strtoul(i8* %4, i8** %3, i32 0)
%9 = load i64, i64* @errno
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i8*, i8** %3
%13 = icmp eq i8* %12, %4
br i1 %13, label %14, label %15
14: ; preds = %11, %6
br label %19
15: ; preds = %11
%16 = load i8, i8* %12
%17 = icmp eq i8 %16, 0
%18 = trunc i64 %8 to i32
br i1 %17, label %20, label %19
19: ; preds = %14, %15, %2
br label %20
20: ; preds = %15, %19
%21 = phi i32 [ %1, %19 ], [ %18, %15 ]
ret i32 %21
}