#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 /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ com_dupenv(char **, size_t *, char *);
int /*<<< orphan*/ com_strcat(char *, int, char *);
int /*<<< orphan*/ com_strcpy(char *, int, char *);
char *com_strdup(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
char *cb_get_env(char *filename, int num) {
  char *path = NULL;
  char buff[256];
  size_t len;
  switch (num) {
  case 1:
    com_dupenv(&path, &len, "OPNCBL_COBDIR");
    break;
  case 2:
    com_dupenv(&path, &len, "OPNCBL_SRCDIR");
    break;
  case 3:
    com_dupenv(&path, &len, "OPNCBL_EROUT");
    break;
  default:
    break;
  }
  if (path == NULL)
    return com_strdup(filename);
  memset(buff, 0, strlen(buff));
  com_strcpy(buff, sizeof(buff), path);
  com_strcat(buff, sizeof(buff), filename);
  return com_strdup(buff);
}


x86 assembly (clang, -O0):
<cb_get_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x12c(%rbp)
sub    $0x1,%eax
je     11b9 <cb_get_env+0x59>
jmp    1191 <cb_get_env+0x31>
mov    -0x12c(%rbp),%eax
sub    $0x2,%eax
je     11d5 <cb_get_env+0x75>
jmp    11a5 <cb_get_env+0x45>
mov    -0x12c(%rbp),%eax
sub    $0x3,%eax
je     11f1 <cb_get_env+0x91>
jmp    120d <cb_get_env+0xad>
lea    -0x20(%rbp),%rdi
lea    -0x128(%rbp),%rsi
lea    0xe35(%rip),%rdx
call   1030 <com_dupenv@plt>
jmp    1212 <cb_get_env+0xb2>
lea    -0x20(%rbp),%rdi
lea    -0x128(%rbp),%rsi
lea    0xe27(%rip),%rdx
call   1030 <com_dupenv@plt>
jmp    1212 <cb_get_env+0xb2>
lea    -0x20(%rbp),%rdi
lea    -0x128(%rbp),%rsi
lea    0xe19(%rip),%rdx
call   1030 <com_dupenv@plt>
jmp    1212 <cb_get_env+0xb2>
jmp    1212 <cb_get_env+0xb2>
cmpq   $0x0,-0x20(%rbp)
jne    122f <cb_get_env+0xcf>
mov    -0x10(%rbp),%rdi
call   1040 <com_strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1293 <cb_get_env+0x133>
lea    -0x120(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x120(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x138(%rbp),%rdi
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    -0x120(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x100,%esi
call   1070 <com_strcpy@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1080 <com_strcat@plt>
lea    -0x120(%rbp),%rdi
call   1040 <com_strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cb_get_env>:
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %rdi,%rbx
movq   $0x0,(%rsp)
add    $0xffffffff,%esi
cmp    $0x2,%esi
ja     11e1 <cb_get_env+0x81>
movslq %esi,%rax
lea    0xea5(%rip),%rcx
movslq (%rcx,%rax,4),%rdx
add    %rcx,%rdx
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
call   1030 <com_dupenv@plt>
cmpq   $0x0,(%rsp)
je     11e1 <cb_get_env+0x81>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    %eax,%edx
call   1060 <memset@plt>
mov    (%rsp),%rdx
mov    %r14,%rdi
mov    $0x100,%esi
call   1070 <com_strcpy@plt>
mov    %r14,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1080 <com_strcat@plt>
mov    %r14,%rdi
jmp    11e4 <cb_get_env+0x84>
mov    %rbx,%rdi
call   1040 <com_strdup@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"OPNCBL_COBDIR\00"
@.str.1 = [14 x i8] c"OPNCBL_SRCDIR\00"
@.str.2 = [13 x i8] c"OPNCBL_EROUT\00"
define i8* @cb_get_env(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [256 x i8]
%8 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* null, i8** %6
%9 = load i32, i32* %5
switch i32 %9, label %16 [
i32 1, label %10
i32 2, label %12
i32 3, label %14
]
10: ; preds = %2
%11 = call i32 @com_dupenv(i8** %6, i64* %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %17
12: ; preds = %2
%13 = call i32 @com_dupenv(i8** %6, i64* %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %17
14: ; preds = %2
%15 = call i32 @com_dupenv(i8** %6, i64* %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %17
16: ; preds = %2
br label %17
17: ; preds = %16, %14, %12, %10
%18 = load i8*, i8** %6
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i8*, i8** %4
%22 = call i8* @com_strdup(i8* %21)
store i8* %22, i8** %3
br label %36
23: ; preds = %17
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%26 = call i32 @strlen(i8* %25)
%27 = call i32 @memset(i8* %24, i32 0, i32 %26)
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%29 = load i8*, i8** %6
%30 = call i32 @com_strcpy(i8* %28, i32 256, i8* %29)
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%32 = load i8*, i8** %4
%33 = call i32 @com_strcat(i8* %31, i32 256, i8* %32)
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%35 = call i8* @com_strdup(i8* %34)
store i8* %35, i8** %3
br label %36
36: ; preds = %23, %20
%37 = load i8*, i8** %3
ret i8* %37
}

LLVM IR (-O2):
@.str = [14 x i8] c"OPNCBL_COBDIR\00"
@.str.1 = [14 x i8] c"OPNCBL_SRCDIR\00"
@.str.2 = [13 x i8] c"OPNCBL_EROUT\00"
@reltable.cb_get_env = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([14 x i8]* @.str to i64), i64 ptrtoint ([3 x i32]* @reltable.cb_get_env to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([14 x i8]* @.str.1 to i64), i64 ptrtoint ([3 x i32]* @reltable.cb_get_env to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([13 x i8]* @.str.2 to i64), i64 ptrtoint ([3 x i32]* @reltable.cb_get_env to i64)) to i32)]
define i8* @cb_get_env(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = alloca i64
%6 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = bitcast i64* %5 to i8*
%9 = add i32 %1, -1
%10 = icmp ult i32 %9, 3
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = sext i32 %9 to i64
%13 = shl i64 %12, 2
%14 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.cb_get_env to i8*), i64 %13)
%15 = call i32 @com_dupenv(i8** %3, i64* %5, i8* %14)
%16 = load i8*, i8** %3
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %2, %11
%19 = call i8* @com_strdup(i8* %0)
br label %27
20: ; preds = %11
%21 = call i32 @strlen(i8* %7)
%22 = call i32 @memset(i8* %7, i32 0, i32 %21)
%23 = load i8*, i8** %3
%24 = call i32 @com_strcpy(i8* %7, i32 256, i8* %23)
%25 = call i32 @com_strcat(i8* %7, i32 256, i8* %0)
%26 = call i8* @com_strdup(i8* %7)
br label %27
27: ; preds = %20, %18
%28 = phi i8* [ %19, %18 ], [ %26, %20 ]
ret i8* %28
}