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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
char *getenv(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
char *strerror(int /*<<< orphan*/);
int confd_ipc_access_get_secret(unsigned char *result, int rsize) {
  char *filename;
  FILE *fp;
  int n;
  if ((filename = getenv("CONFD_IPC_ACCESS_FILE")) == NULL &&
      (filename = getenv("NCS_IPC_ACCESS_FILE")) == NULL) {
    return 0;
  }
  if ((fp = fopen(filename, "r")) == NULL) {
    snprintf((char *)result, rsize, "Failed to open %s: %s", filename,
             strerror(errno));
    return -1;
  }
  if ((n = fread(result, 1, rsize - 1, fp)) == 0) {
    snprintf((char *)result, rsize, "Failed to read %s: %s", filename,
             ferror(fp) ? strerror(errno) : "Empty file");
    fclose(fp);
    return -1;
  }
  fclose(fp);
  result[n] = '\0';
  return 1;
}


x86 assembly (clang, -O0):
<confd_ipc_access_get_secret>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe7a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11bf <confd_ipc_access_get_secret+0x4f>
lea    0xe76(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11bf <confd_ipc_access_get_secret+0x4f>
movl   $0x0,-0x4(%rbp)
jmp    12e7 <confd_ipc_access_get_secret+0x177>
mov    -0x20(%rbp),%rdi
lea    0xe60(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1227 <confd_ipc_access_get_secret+0xb7>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rcx
mov    %rax,%r8
lea    0xe16(%rip),%rdx
call   1070 <snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <confd_ipc_access_get_secret+0x177>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jne    12cb <confd_ipc_access_get_secret+0x15b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     128b <confd_ipc_access_get_secret+0x11b>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,-0x68(%rbp)
jmp    129b <confd_ipc_access_get_secret+0x12b>
lea    0xdc6(%rip),%rax
mov    %rax,-0x68(%rbp)
jmp    129b <confd_ipc_access_get_secret+0x12b>
mov    -0x50(%rbp),%rcx
mov    -0x54(%rbp),%esi
mov    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%r8
lea    0xd91(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <confd_ipc_access_get_secret+0x177>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<confd_ipc_access_get_secret>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
lea    0xe78(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11ad <confd_ipc_access_get_secret+0x3d>
lea    0xe7a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r13
test   %rax,%rax
je     123d <confd_ipc_access_get_secret+0xcd>
lea    0xe76(%rip),%rsi
mov    %r13,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f5 <confd_ipc_access_get_secret+0x85>
mov    %rax,%r12
lea    -0x1(%r14),%edx
mov    $0x1,%ebp
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rax,%rcx
call   1050 <fread@plt>
test   %eax,%eax
je     121d <confd_ipc_access_get_secret+0xad>
mov    %eax,%ebx
mov    %r12,%rdi
call   1060 <fclose@plt>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
jmp    126a <confd_ipc_access_get_secret+0xfa>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe22(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %r13,%rcx
mov    %rax,%r8
call   1070 <snprintf@plt>
jmp    1265 <confd_ipc_access_get_secret+0xf5>
mov    %r12,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     1241 <confd_ipc_access_get_secret+0xd1>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%r8
jmp    1248 <confd_ipc_access_get_secret+0xd8>
xor    %ebp,%ebp
jmp    126a <confd_ipc_access_get_secret+0xfa>
lea    0xe10(%rip),%r8
lea    0xdf3(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %r13,%rcx
call   1070 <snprintf@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"CONFD_IPC_ACCESS_FILE\00"
@.str.1 = [20 x i8] c"NCS_IPC_ACCESS_FILE\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [22 x i8] c"Failed to open %s: %s\00"
@errno = global i32 0
@.str.4 = [22 x i8] c"Failed to read %s: %s\00"
@.str.5 = [11 x i8] c"Empty file\00"
define i32 @confd_ipc_access_get_secret(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = call i8* @getenv(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %6
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = call i8* @getenv(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i8* %12, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %11
store i32 0, i32* %3
br label %56
15: ; preds = %11, %2
%16 = load i8*, i8** %6
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %17, i32** %7
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = load i8*, i8** %4
%21 = load i32, i32* %5
%22 = load i8*, i8** %6
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @snprintf(i8* %20, i32 %21, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %22, i8* %24)
store i32 -1, i32* %3
br label %56
26: ; preds = %15
%27 = load i8*, i8** %4
%28 = load i32, i32* %5
%29 = sub nsw i32 %28, 1
%30 = load i32*, i32** %7
%31 = call i32 @fread(i8* %27, i32 1, i32 %29, i32* %30)
store i32 %31, i32* %8
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %49
33: ; preds = %26
%34 = load i8*, i8** %4
%35 = load i32, i32* %5
%36 = load i8*, i8** %6
%37 = load i32*, i32** %7
%38 = call i64 @ferror(i32* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
br label %44
43: ; preds = %33
br label %44
44: ; preds = %43, %40
%45 = phi i8* [ %42, %40 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), %43 ]
%46 = call i32 @snprintf(i8* %34, i32 %35, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %36, i8* %45)
%47 = load i32*, i32** %7
%48 = call i32 @fclose(i32* %47)
store i32 -1, i32* %3
br label %56
49: ; preds = %26
%50 = load i32*, i32** %7
%51 = call i32 @fclose(i32* %50)
%52 = load i8*, i8** %4
%53 = load i32, i32* %8
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8 0, i8* %55
store i32 1, i32* %3
br label %56
56: ; preds = %49, %44, %19, %14
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str = [22 x i8] c"CONFD_IPC_ACCESS_FILE\00"
@.str.1 = [20 x i8] c"NCS_IPC_ACCESS_FILE\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [22 x i8] c"Failed to open %s: %s\00"
@errno = global i32 0
@.str.4 = [22 x i8] c"Failed to read %s: %s\00"
@.str.5 = [11 x i8] c"Empty file\00"
define i32 @confd_ipc_access_get_secret(i8* %0, i32 %1) {
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %34, label %8
8: ; preds = %5, %2
%9 = phi i8* [ %6, %5 ], [ %3, %2 ]
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %9, i8* %14)
br label %34
16: ; preds = %8
%17 = add nsw i32 %1, -1
%18 = tail call i32 @fread(i8* %0, i32 1, i32 %17, i32* %10)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = tail call i64 @ferror(i32* %10)
%22 = icmp eq i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
br label %26
26: ; preds = %20, %23
%27 = phi i8* [ %25, %23 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), %20 ]
%28 = tail call i32 @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %9, i8* %27)
%29 = tail call i32 @fclose(i32* %10)
br label %34
30: ; preds = %16
%31 = tail call i32 @fclose(i32* %10)
%32 = sext i32 %18 to i64
%33 = getelementptr inbounds i8, i8* %0, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %5, %30, %26, %12
%35 = phi i32 [ -1, %12 ], [ -1, %26 ], [ 1, %30 ], [ 0, %5 ]
ret i32 %35
}