#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 ssize_t;

/* Variables and functions */
scalar_t__ errno;
char *getenv(char *);
long strtol(char const *, int /*<<< orphan*/ *, int);
ssize_t get_content_length() {
  const char *value = getenv("CONTENT_LENGTH");
  if (value && *value != '\0') {
    errno = 0;
    long res = strtol(value, NULL, 10);
    if (errno == 0 && res >= 0) {
      return (ssize_t)res;
    } else {
      return -1;
    }
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<get_content_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xed1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ab <get_content_length+0x8b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11ab <get_content_length+0x8b>
mov    0x2e86(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e62(%rip),%rax
cmpq   $0x0,(%rax)
jne    119f <get_content_length+0x7f>
cmpq   $0x0,-0x18(%rbp)
jl     119f <get_content_length+0x7f>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11b2 <get_content_length+0x92>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b2 <get_content_length+0x92>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_content_length>:
push   %r14
push   %rbx
push   %rax
lea    0xed5(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     1171 <get_content_length+0x51>
cmpb   $0x0,(%rdi)
je     1171 <get_content_length+0x51>
mov    0x2e97(%rip),%rbx
movq   $0x0,(%rbx)
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
test   %rax,%rax
mov    $0xffffffff,%ecx
cmovns %eax,%ecx
cmp    (%rbx),%r14
sbb    %eax,%eax
or     %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [15 x i8] c"CONTENT_LENGTH\00"
@errno = global i64 0
define i32 @get_content_length() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %2
%5 = load i8*, i8** %2
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %24
7: ; preds = %0
%8 = load i8*, i8** %2
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %7
store i64 0, i64* @errno
%13 = load i8*, i8** %2
%14 = call i64 @strtol(i8* %13, i32* null, i32 10)
store i64 %14, i64* %3
%15 = load i64, i64* @errno
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %12
%18 = load i64, i64* %3
%19 = icmp sge i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i64, i64* %3
%22 = trunc i64 %21 to i32
store i32 %22, i32* %1
br label %25
23: ; preds = %17, %12
store i32 -1, i32* %1
br label %25
24: ; preds = %7, %0
store i32 -1, i32* %1
br label %25
25: ; preds = %24, %23, %20
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str = [15 x i8] c"CONTENT_LENGTH\00"
@errno = global i64 0
define i32 @get_content_length() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %14, label %3
3: ; preds = %0
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 0
br i1 %5, label %14, label %6
6: ; preds = %3
store i64 0, i64* @errno
%7 = tail call i64 @strtol(i8* nocapture %1, i32* null, i32 10)
%8 = load i64, i64* @errno
%9 = icmp eq i64 %8, 0
%10 = icmp sgt i64 %7, -1
%11 = select i1 %9, i1 %10, i1 false
%12 = trunc i64 %7 to i32
%13 = select i1 %11, i32 %12, i32 -1
br label %14
14: ; preds = %0, %3, %6
%15 = phi i32 [ %13, %6 ], [ -1, %3 ], [ -1, %0 ]
ret i32 %15
}