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


x86 assembly (clang, -O0):
<RAND_file_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1050 <OPENSSL_issetugid@plt>
cmp    $0x0,%rax
jne    1189 <RAND_file_name+0x39>
lea    0xe80(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11e9 <RAND_file_name+0x99>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11e9 <RAND_file_name+0x99>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    -0x18(%rbp),%rax
jae    11e9 <RAND_file_name+0x99>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <BUF_strlcpy@plt>
cmp    -0x18(%rbp),%rax
jb     11e4 <RAND_file_name+0x94>
movq   $0x0,-0x8(%rbp)
jmp    12a5 <RAND_file_name+0x155>
jmp    129d <RAND_file_name+0x14d>
mov    $0x0,%al
call   1050 <OPENSSL_issetugid@plt>
cmp    $0x0,%rax
jne    120a <RAND_file_name+0xba>
lea    0xe08(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1291 <RAND_file_name+0x141>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1291 <RAND_file_name+0x141>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
lea    0xdd6(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
cmp    -0x18(%rbp),%rax
jae    1291 <RAND_file_name+0x141>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <BUF_strlcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xda0(%rip),%rsi
call   1070 <BUF_strlcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd87(%rip),%rsi
call   1070 <BUF_strlcat@plt>
jmp    1298 <RAND_file_name+0x148>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    129d <RAND_file_name+0x14d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RAND_file_name>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <OPENSSL_issetugid@plt>
test   %rax,%rax
je     1182 <RAND_file_name+0x32>
xor    %eax,%eax
call   1050 <OPENSSL_issetugid@plt>
test   %rax,%rax
je     11c9 <RAND_file_name+0x79>
test   %rbx,%rbx
jne    11dd <RAND_file_name+0x8d>
jmp    123a <RAND_file_name+0xea>
lea    0xe77(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     124d <RAND_file_name+0xfd>
mov    %rax,%rbx
cmpb   $0x0,(%rax)
je     116c <RAND_file_name+0x1c>
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    %r14,%rax
jae    116c <RAND_file_name+0x1c>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <BUF_strlcpy@plt>
cmp    %r14,%rax
jb     123e <RAND_file_name+0xee>
xor    %r15d,%r15d
jmp    123e <RAND_file_name+0xee>
lea    0xe39(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rbx,%rbx
je     123a <RAND_file_name+0xea>
cmpb   $0x0,(%rbx)
je     123a <RAND_file_name+0xea>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
lea    0xe1a(%rip),%rdi
call   1040 <strlen@plt>
add    %r12d,%eax
add    $0x2,%eax
cltq
cmp    %r14,%rax
jae    123a <RAND_file_name+0xea>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <BUF_strlcpy@plt>
lea    0xdf8(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1070 <BUF_strlcat@plt>
lea    0xde1(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1070 <BUF_strlcat@plt>
jmp    123e <RAND_file_name+0xee>
movb   $0x0,(%r15)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %ebx,%ebx
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*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* null, i8** %6
%7 = call i64 (...) @OPENSSL_issetugid()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %6
br label %11
11: ; preds = %9, %2
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %35
14: ; preds = %11
%15 = load i8*, i8** %6
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %14
%20 = load i8*, i8** %6
%21 = call i32 @strlen(i8* %20)
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = load i64, i64* %5
%25 = icmp ult i64 %23, %24
br i1 %25, label %26, label %35
26: ; preds = %19
%27 = load i8*, i8** %4
%28 = load i8*, i8** %6
%29 = load i64, i64* %5
%30 = call i64 @BUF_strlcpy(i8* %27, i8* %28, i64 %29)
%31 = load i64, i64* %5
%32 = icmp uge i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %26
store i8* null, i8** %3
br label %74
34: ; preds = %26
br label %72
35: ; preds = %19, %14, %11
%36 = call i64 (...) @OPENSSL_issetugid()
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %39, i8** %6
br label %40
40: ; preds = %38, %35
%41 = load i8*, i8** %6
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %68
43: ; preds = %40
%44 = load i8*, i8** %6
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %68
48: ; preds = %43
%49 = load i8*, i8** %6
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%52 = add nsw i32 %50, %51
%53 = add nsw i32 %52, 2
%54 = sext i32 %53 to i64
%55 = load i64, i64* %5
%56 = icmp ult i64 %54, %55
br i1 %56, label %57, label %68
57: ; preds = %48
%58 = load i8*, i8** %4
%59 = load i8*, i8** %6
%60 = load i64, i64* %5
%61 = call i64 @BUF_strlcpy(i8* %58, i8* %59, i64 %60)
%62 = load i8*, i8** %4
%63 = load i64, i64* %5
%64 = call i32 @BUF_strlcat(i8* %62, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i64 %63)
%65 = load i8*, i8** %4
%66 = load i64, i64* %5
%67 = call i32 @BUF_strlcat(i8* %65, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %66)
br label %71
68: ; preds = %48, %43, %40
%69 = load i8*, i8** %4
%70 = getelementptr inbounds i8, i8* %69, i64 0
store i8 0, i8* %70
br label %71
71: ; preds = %68, %57
br label %72
72: ; preds = %71, %34
%73 = load i8*, i8** %4
store i8* %73, i8** %3
br label %74
74: ; preds = %72, %33
%75 = load i8*, i8** %3
ret i8* %75
}

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 %19
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 %19, label %8
8: ; preds = %5
%9 = load i8, i8* %6
%10 = icmp eq i8 %9, 0
br i1 %10, label %19, label %11
11: ; preds = %8
%12 = tail call i32 @strlen(i8* %6)
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = icmp ult i64 %14, %1
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = tail call i64 @BUF_strlcpy(i8* %0, i8* %6, i64 %1)
%18 = icmp ult i64 %17, %1
br i1 %18, label %43, label %44
19: ; preds = %2, %11, %8, %5
%20 = phi i8* [ %6, %11 ], [ %6, %8 ], [ null, %5 ], [ null, %2 ]
%21 = tail call i64 (...) @OPENSSL_issetugid()
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = phi i8* [ %24, %23 ], [ %20, %19 ]
%27 = icmp eq i8* %26, null
br i1 %27, label %42, label %28
28: ; preds = %25
%29 = load i8, i8* %26
%30 = icmp eq i8 %29, 0
br i1 %30, label %42, label %31
31: ; preds = %28
%32 = tail call i32 @strlen(i8* %26)
%33 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%34 = add i32 %32, 2
%35 = add i32 %34, %33
%36 = sext i32 %35 to i64
%37 = icmp ult i64 %36, %1
br i1 %37, label %38, label %42
38: ; preds = %31
%39 = tail call i64 @BUF_strlcpy(i8* %0, i8* %26, i64 %1)
%40 = tail call i32 @BUF_strlcat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i64 %1)
%41 = tail call i32 @BUF_strlcat(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 %1)
br label %43
42: ; preds = %31, %28, %25
store i8 0, i8* %0
br label %43
43: ; preds = %38, %42, %16
br label %44
44: ; preds = %16, %43
%45 = phi i8* [ %0, %43 ], [ null, %16 ]
ret i8* %45
}