#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 */
char **__environ;
int gotcha_strlen(char const *);
scalar_t__ gotcha_strncmp(char const *, char *, int);
char *gotcha_getenv(const char *name) {
  char **s;
  int name_len;
  name_len = gotcha_strlen(name);
  for (s = __environ; *s; s++) {
    if (gotcha_strncmp(name, *s, name_len) != 0)
      continue;
    if ((*s)[name_len] != '=')
      continue;
    return (*s) + name_len + 1;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<gotcha_getenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gotcha_strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2eb9(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     11bf <gotcha_getenv+0x9f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <gotcha_strncmp@plt>
cmp    $0x0,%rax
je     1176 <gotcha_getenv+0x56>
jmp    11ae <gotcha_getenv+0x8e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x3d,%eax
je     1193 <gotcha_getenv+0x73>
jmp    11ae <gotcha_getenv+0x8e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11c7 <gotcha_getenv+0xa7>
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
jmp    1146 <gotcha_getenv+0x26>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gotcha_getenv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
call   1040 <gotcha_strlen@plt>
mov    %eax,%ebp
mov    0x2ebf(%rip),%rax
mov    (%rax),%rbx
mov    (%rbx),%rsi
test   %rsi,%rsi
je     1180 <gotcha_getenv+0x60>
movslq %ebp,%r12
add    $0x8,%rbx
xor    %r14d,%r14d
jmp    115c <gotcha_getenv+0x3c>
mov    (%rbx),%rsi
add    $0x8,%rbx
test   %rsi,%rsi
je     1183 <gotcha_getenv+0x63>
mov    %r15,%rdi
mov    %ebp,%edx
call   1030 <gotcha_strncmp@plt>
test   %rax,%rax
jne    1150 <gotcha_getenv+0x30>
mov    -0x8(%rbx),%rax
cmpb   $0x3d,(%rax,%r12,1)
jne    1150 <gotcha_getenv+0x30>
lea    (%rax,%r12,1),%r14
add    $0x1,%r14
jmp    1183 <gotcha_getenv+0x63>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__environ = global i8** null
define i8* @gotcha_getenv(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @gotcha_strlen(i8* %6)
store i32 %7, i32* %5
%8 = load i8**, i8*** @__environ
store i8** %8, i8*** %4
br label %9
9: ; preds = %38, %1
%10 = load i8**, i8*** %4
%11 = load i8*, i8** %10
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %41
13: ; preds = %9
%14 = load i8*, i8** %3
%15 = load i8**, i8*** %4
%16 = load i8*, i8** %15
%17 = load i32, i32* %5
%18 = call i64 @gotcha_strncmp(i8* %14, i8* %16, i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %13
br label %38
21: ; preds = %13
%22 = load i8**, i8*** %4
%23 = load i8*, i8** %22
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 61
br i1 %29, label %30, label %31
30: ; preds = %21
br label %38
31: ; preds = %21
%32 = load i8**, i8*** %4
%33 = load i8*, i8** %32
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = getelementptr inbounds i8, i8* %36, i64 1
store i8* %37, i8** %2
br label %42
38: ; preds = %30, %20
%39 = load i8**, i8*** %4
%40 = getelementptr inbounds i8*, i8** %39, i32 1
store i8** %40, i8*** %4
br label %9
41: ; preds = %9
store i8* null, i8** %2
br label %42
42: ; preds = %41, %31
%43 = load i8*, i8** %2
ret i8* %43
}

LLVM IR (-O2):
@__environ = global i8** null
define i8* @gotcha_getenv(i8* %0) {
%2 = tail call i32 @gotcha_strlen(i8* %0)
%3 = load i8**, i8*** @__environ
%4 = load i8*, i8** %3
%5 = icmp eq i8* %4, null
br i1 %5, label %25, label %6
6: ; preds = %1
%7 = sext i32 %2 to i64
br label %8
8: ; preds = %6, %21
%9 = phi i8* [ %4, %6 ], [ %23, %21 ]
%10 = phi i8** [ %3, %6 ], [ %22, %21 ]
%11 = tail call i64 @gotcha_strncmp(i8* %0, i8* %9, i32 %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %8
%14 = load i8*, i8** %10
%15 = getelementptr inbounds i8, i8* %14, i64 %7
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 61
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = getelementptr inbounds i8, i8* %14, i64 %7
%20 = getelementptr inbounds i8, i8* %19, i64 1
br label %25
21: ; preds = %13, %8
%22 = getelementptr inbounds i8*, i8** %10, i64 1
%23 = load i8*, i8** %22
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %8
25: ; preds = %21, %1, %18
%26 = phi i8* [ %20, %18 ], [ null, %1 ], [ null, %21 ]
ret i8* %26
}