#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 *_find_default(char *);
char *getenv(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int strlen(char *);
int get_env(char *name, char *buf, int size) {
  char *ret;
  ret = getenv(name);
  if ((ret == NULL) || (strlen(ret) > 1024)) {
    ret = _find_default(name);
    if (ret)
      snprintf(buf, size, "%s", ret);
    else
      snprintf(buf, size, "");
  } else {
    snprintf(buf, size, "%s", ret);
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     117f <get_env+0x3f>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x400,%eax
jle    11cf <get_env+0x8f>
mov    -0x8(%rbp),%rdi
call   1060 <_find_default@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b5 <get_env+0x75>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rcx
lea    0xe57(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    11ca <get_env+0x8a>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    0xe3f(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    11e8 <get_env+0xa8>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rcx
lea    0xe1f(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_env>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
call   1030 <getenv@plt>
test   %rax,%rax
je     116c <get_env+0x2c>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <strlen@plt>
cmp    $0x401,%eax
jl     118b <get_env+0x4b>
mov    %rbx,%rdi
call   1060 <_find_default@plt>
test   %rax,%rax
je     11a4 <get_env+0x64>
lea    0xe80(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rax,%rcx
jmp    119b <get_env+0x5b>
lea    0xe6e(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbp,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
jmp    11b8 <get_env+0x78>
lea    0xe57(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [1 x i8] zeroinitializer
define i32 @get_env(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = call i8* @getenv(i8* %8)
store i8* %9, i8** %7
%10 = load i8*, i8** %7
%11 = icmp eq i8* %10, null
br i1 %11, label %16, label %12
12: ; preds = %3
%13 = load i8*, i8** %7
%14 = call i32 @strlen(i8* %13)
%15 = icmp sgt i32 %14, 1024
br i1 %15, label %16, label %31
16: ; preds = %12, %3
%17 = load i8*, i8** %4
%18 = call i8* @_find_default(i8* %17)
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i8*, i8** %5
%23 = load i32, i32* %6
%24 = load i8*, i8** %7
%25 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %22, i32 %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %30
26: ; preds = %16
%27 = load i8*, i8** %5
%28 = load i32, i32* %6
%29 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %27, i32 %28, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %26, %21
br label %36
31: ; preds = %12
%32 = load i8*, i8** %5
%33 = load i32, i32* %6
%34 = load i8*, i8** %7
%35 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %32, i32 %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %31, %30
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [1 x i8] zeroinitializer
define i32 @get_env(i8* %0, i8* %1, i32 %2) {
%4 = tail call i8* @getenv(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %4)
%8 = icmp sgt i32 %7, 1024
br i1 %8, label %9, label %16
9: ; preds = %6, %3
%10 = tail call i8* @_find_default(i8* %0)
%11 = icmp eq i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %18
14: ; preds = %9
%15 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %18
16: ; preds = %6
%17 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
br label %18
18: ; preds = %12, %14, %16
ret i32 0
}