#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__ OPENSSL_issetugid();
int /*<<< orphan*/ OPENSSL_strlcat(char *, char *, size_t);
size_t OPENSSL_strlcpy(char *, char *, size_t);
char *getenv(char *);
size_t strlen(char *);
const char *RAND_file_name(char *buf, size_t size) {
  char *s = NULL;
  int use_randfile = 1;
  if (OPENSSL_issetugid() != 0) {
    use_randfile = 0;
  } else {
    s = getenv("RANDFILE");
    if (s == NULL || *s == '\0') {
      use_randfile = 0;
      s = getenv("HOME");
    }
  }
  if (s != NULL && *s) {
    size_t len = strlen(s);
    if (use_randfile && len + 1 < size) {
      if (OPENSSL_strlcpy(buf, s, size) >= size)
        return NULL;
    } else if (len + strlen(".rnd") + 2 < size) {
      OPENSSL_strlcpy(buf, s, size);
      OPENSSL_strlcat(buf, "/", size);
      OPENSSL_strlcat(buf, ".rnd", size);
    }
  } else {
    buf[0] = '\0';
  }
  return buf[0] ? buf : NULL;
}


x86 assembly (clang, -O0):
<RAND_file_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    $0x0,%al
call   1060 <OPENSSL_issetugid@plt>
cmp    $0x0,%rax
je     118c <RAND_file_name+0x3c>
movl   $0x0,-0x24(%rbp)
jmp    11d3 <RAND_file_name+0x83>
lea    0xe6d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b7 <RAND_file_name+0x67>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11ce <RAND_file_name+0x7e>
movl   $0x0,-0x24(%rbp)
lea    0xe44(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
jmp    11d3 <RAND_file_name+0x83>
cmpq   $0x0,-0x20(%rbp)
je     12b3 <RAND_file_name+0x163>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12b3 <RAND_file_name+0x163>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1244 <RAND_file_name+0xf4>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
cmp    -0x18(%rbp),%rax
jae    1244 <RAND_file_name+0xf4>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <OPENSSL_strlcpy@plt>
cmp    -0x18(%rbp),%rax
jb     123f <RAND_file_name+0xef>
movq   $0x0,-0x8(%rbp)
jmp    12ea <RAND_file_name+0x19a>
jmp    12ae <RAND_file_name+0x15e>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xdbb(%rip),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
add    $0x2,%rax
cmp    -0x18(%rbp),%rax
jae    12a9 <RAND_file_name+0x159>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <OPENSSL_strlcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd83(%rip),%rsi
call   1070 <OPENSSL_strlcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd6a(%rip),%rsi
call   1070 <OPENSSL_strlcat@plt>
jmp    12ae <RAND_file_name+0x15e>
jmp    12ba <RAND_file_name+0x16a>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12d7 <RAND_file_name+0x187>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    12e2 <RAND_file_name+0x192>
xor    %eax,%eax
mov    %rax,-0x40(%rbp)
jmp    12e2 <RAND_file_name+0x192>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RAND_file_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1060 <OPENSSL_issetugid@plt>
test   %rax,%rax
je     1180 <RAND_file_name+0x30>
movb   $0x0,(%rbx)
xor    %eax,%eax
cmpb   $0x0,(%rbx)
cmovne %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe79(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     119c <RAND_file_name+0x4c>
mov    %rax,%r15
mov    $0x1,%bpl
cmpb   $0x0,(%rax)
jne    11b7 <RAND_file_name+0x67>
lea    0xe66(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1169 <RAND_file_name+0x19>
mov    %rax,%r15
cmpb   $0x0,(%rax)
je     1169 <RAND_file_name+0x19>
xor    %ebp,%ebp
mov    %r15,%rdi
call   1050 <strlen@plt>
test   %bpl,%bpl
je     11e4 <RAND_file_name+0x94>
lea    0x1(%rax),%rcx
cmp    %r14,%rcx
jae    11e4 <RAND_file_name+0x94>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <OPENSSL_strlcpy@plt>
cmp    %r14,%rax
jb     116c <RAND_file_name+0x1c>
xor    %eax,%eax
jmp    1175 <RAND_file_name+0x25>
add    $0x6,%rax
cmp    %r14,%rax
jae    116c <RAND_file_name+0x1c>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <OPENSSL_strlcpy@plt>
lea    0xe0d(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1070 <OPENSSL_strlcat@plt>
lea    0xdf6(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1070 <OPENSSL_strlcat@plt>
jmp    116c <RAND_file_name+0x1c>

LLVM IR (-O0):
@.str = [9 x i8] c"RANDFILE\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [5 x i8] c".rnd\00"
@.str.3 = [2 x i8] c"/\00"
define i8* @RAND_file_name(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* null, i8** %6
store i32 1, i32* %7
%9 = call i64 (...) @OPENSSL_issetugid()
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 0, i32* %7
br label %24
12: ; preds = %2
%13 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = load i8*, i8** %6
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16, %12
store i32 0, i32* %7
%22 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %22, i8** %6
br label %23
23: ; preds = %21, %16
br label %24
24: ; preds = %23, %11
%25 = load i8*, i8** %6
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %71
27: ; preds = %24
%28 = load i8*, i8** %6
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %71
32: ; preds = %27
%33 = load i8*, i8** %6
%34 = call i64 @strlen(i8* %33)
store i64 %34, i64* %8
%35 = load i32, i32* %7
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %51
37: ; preds = %32
%38 = load i64, i64* %8
%39 = add i64 %38, 1
%40 = load i64, i64* %5
%41 = icmp ult i64 %39, %40
br i1 %41, label %42, label %51
42: ; preds = %37
%43 = load i8*, i8** %4
%44 = load i8*, i8** %6
%45 = load i64, i64* %5
%46 = call i64 @OPENSSL_strlcpy(i8* %43, i8* %44, i64 %45)
%47 = load i64, i64* %5
%48 = icmp uge i64 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %42
store i8* null, i8** %3
br label %85
50: ; preds = %42
br label %70
51: ; preds = %37, %32
%52 = load i64, i64* %8
%53 = call i64 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%54 = add i64 %52, %53
%55 = add i64 %54, 2
%56 = load i64, i64* %5
%57 = icmp ult i64 %55, %56
br i1 %57, label %58, label %69
58: ; preds = %51
%59 = load i8*, i8** %4
%60 = load i8*, i8** %6
%61 = load i64, i64* %5
%62 = call i64 @OPENSSL_strlcpy(i8* %59, i8* %60, i64 %61)
%63 = load i8*, i8** %4
%64 = load i64, i64* %5
%65 = call i32 @OPENSSL_strlcat(i8* %63, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i64 %64)
%66 = load i8*, i8** %4
%67 = load i64, i64* %5
%68 = call i32 @OPENSSL_strlcat(i8* %66, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %67)
br label %69
69: ; preds = %58, %51
br label %70
70: ; preds = %69, %50
br label %74
71: ; preds = %27, %24
%72 = load i8*, i8** %4
%73 = getelementptr inbounds i8, i8* %72, i64 0
store i8 0, i8* %73
br label %74
74: ; preds = %71, %70
%75 = load i8*, i8** %4
%76 = getelementptr inbounds i8, i8* %75, i64 0
%77 = load i8, i8* %76
%78 = sext i8 %77 to i32
%79 = icmp ne i32 %78, 0
br i1 %79, label %80, label %82
80: ; preds = %74
%81 = load i8*, i8** %4
br label %83
82: ; preds = %74
br label %83
83: ; preds = %82, %80
%84 = phi i8* [ %81, %80 ], [ null, %82 ]
store i8* %84, i8** %3
br label %85
85: ; preds = %83, %49
%86 = load i8*, i8** %3
ret i8* %86
}

LLVM IR (-O2):
@.str = [9 x i8] c"RANDFILE\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [5 x i8] c".rnd\00"
@.str.3 = [2 x i8] c"/\00"
define i8* @RAND_file_name(i8* %0, i64 %1) {
%3 = tail call i64 (...) @OPENSSL_issetugid()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %34
5: ; preds = %2
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = load i8, i8* %6
%10 = icmp eq i8 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %5, %8
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %34, label %14
14: ; preds = %11
%15 = load i8, i8* %12
%16 = icmp eq i8 %15, 0
br i1 %16, label %34, label %17
17: ; preds = %8, %14
%18 = phi i8* [ %12, %14 ], [ %6, %8 ]
%19 = phi i1 [ false, %14 ], [ true, %8 ]
%20 = tail call i64 @strlen(i8* %18)
%21 = add i64 %20, 1
%22 = icmp ult i64 %21, %1
%23 = select i1 %19, i1 %22, i1 false
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = tail call i64 @OPENSSL_strlcpy(i8* %0, i8* %18, i64 %1)
%26 = icmp ult i64 %25, %1
br i1 %26, label %35, label %39
27: ; preds = %17
%28 = add i64 %20, 6
%29 = icmp ult i64 %28, %1
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = tail call i64 @OPENSSL_strlcpy(i8* %0, i8* %18, i64 %1)
%32 = tail call i32 @OPENSSL_strlcat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i64 %1)
%33 = tail call i32 @OPENSSL_strlcat(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %1)
br label %35
34: ; preds = %2, %14, %11
store i8 0, i8* %0
br label %35
35: ; preds = %24, %30, %27, %34
%36 = load i8, i8* %0
%37 = icmp eq i8 %36, 0
%38 = select i1 %37, i8* null, i8* %0
br label %39
39: ; preds = %24, %35
%40 = phi i8* [ %38, %35 ], [ null, %24 ]
ret i8* %40
}