#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*/ LOG_INFO;
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ strcmp(char const *, char *);
int /*<<< orphan*/ *stub1(char const *, char const *);
int /*<<< orphan*/ *stub2(char const *, char const *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ write_cpuinfo();
FILE *fopen(const char *path, const char *mode) {
  static FILE *(*func)(const char *, const char *) = NULL;
  write_cpuinfo();
  if (!func)
    func =
        (FILE * (*)(const char *, const char *)) dlsym(((void *)-1L), "fopen");
  if (!strcmp(path, "/proc/cpuinfo")) {
    { syslog(LOG_INFO, "ca.so fopen %s mapped to %s", path, "/tmp/cpuinfo"); };
    return (*func)("/tmp/cpuinfo", mode);
  };
  return (*func)(path, mode);
}


x86 assembly (clang, -O0):
<fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1030 <write_cpuinfo@plt>
cmpq   $0x0,0x2ee9(%rip)
jne    117f <fopen+0x3f>
mov    $0xffffffffffffffff,%rdi
lea    0xe8d(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,0x2ec9(%rip)
mov    -0x10(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11d5 <fopen+0x95>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe68(%rip),%rsi
lea    0xe7d(%rip),%rcx
call   1050 <syslog@plt>
mov    0x2e89(%rip),%rax
mov    -0x18(%rbp),%rsi
lea    0xe66(%rip),%rdi
call   *%rax
mov    %rax,-0x8(%rbp)
jmp    11ea <fopen+0xaa>
mov    0x2e6c(%rip),%rax
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   *%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <write_cpuinfo@plt>
mov    0x2eef(%rip),%rax
test   %rax,%rax
jne    1178 <fopen+0x38>
lea    0xe9b(%rip),%rsi
mov    $0xffffffffffffffff,%rdi
call   1060 <dlsym@plt>
mov    %rax,0x2ed0(%rip)
mov    %rax,%r15
lea    0xe84(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %eax,%eax
je     1199 <fopen+0x59>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rax
jmp    11c8 <fopen+0x88>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
lea    0xe80(%rip),%r15
mov    %rbx,%rdx
mov    %r15,%rcx
call   1050 <syslog@plt>
mov    0x2e86(%rip),%rax
mov    %r15,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    *%rax

LLVM IR (-O0):
@fopen.func = internal global i32* (i8*, i8*)* null
@.str = [6 x i8] c"fopen\00"
@.str.1 = [14 x i8] c"/proc/cpuinfo\00"
@LOG_INFO = global i32 0
@.str.2 = [28 x i8] c"ca.so fopen %s mapped to %s\00"
@.str.3 = [13 x i8] c"/tmp/cpuinfo\00"
define i32* @fopen(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%6 = call i32 (...) @write_cpuinfo()
%7 = load i32* (i8*, i8*)*, i32* (i8*, i8*)** @fopen.func
%8 = icmp ne i32* (i8*, i8*)* %7, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = call i64 @dlsym(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%11 = inttoptr i64 %10 to i32* (i8*, i8*)*
store i32* (i8*, i8*)* %11, i32* (i8*, i8*)** @fopen.func
br label %12
12: ; preds = %9, %2
%13 = load i8*, i8** %4
%14 = call i32 @strcmp(i8* %13, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp ne i32 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %12
%17 = load i32, i32* @LOG_INFO
%18 = load i8*, i8** %4
%19 = call i32 @syslog(i32 %17, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %18, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%20 = load i32* (i8*, i8*)*, i32* (i8*, i8*)** @fopen.func
%21 = load i8*, i8** %5
%22 = call i32* %20(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %21)
store i32* %22, i32** %3
br label %28
23: ; preds = %12
%24 = load i32* (i8*, i8*)*, i32* (i8*, i8*)** @fopen.func
%25 = load i8*, i8** %4
%26 = load i8*, i8** %5
%27 = call i32* %24(i8* %25, i8* %26)
store i32* %27, i32** %3
br label %28
28: ; preds = %23, %16
%29 = load i32*, i32** %3
ret i32* %29
}

LLVM IR (-O2):
@fopen.func = internal unnamed_addr global i32* (i8*, i8*)* null
@.str = [6 x i8] c"fopen\00"
@.str.1 = [14 x i8] c"/proc/cpuinfo\00"
@LOG_INFO = global i32 0
@.str.2 = [28 x i8] c"ca.so fopen %s mapped to %s\00"
@.str.3 = [13 x i8] c"/tmp/cpuinfo\00"
define i32* @fopen(i8* %0, i8* %1) {
%3 = tail call i32 (...) @write_cpuinfo()
%4 = load i32* (i8*, i8*)*, i32* (i8*, i8*)** @fopen.func
%5 = icmp eq i32* (i8*, i8*)* %4, null
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i64 @dlsym(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = inttoptr i64 %7 to i32* (i8*, i8*)*
store i32* (i8*, i8*)* %8, i32* (i8*, i8*)** @fopen.func
br label %9
9: ; preds = %6, %2
%10 = phi i32* (i8*, i8*)* [ %8, %6 ], [ %4, %2 ]
%11 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(14) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = load i32, i32* @LOG_INFO
%15 = tail call i32 @syslog(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%16 = load i32* (i8*, i8*)*, i32* (i8*, i8*)** @fopen.func
%17 = tail call i32* %16(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %20
18: ; preds = %9
%19 = tail call i32* %10(i8* %0, i8* %1)
br label %20
20: ; preds = %18, %13
%21 = phi i32* [ %19, %18 ], [ %17, %13 ]
ret i32* %21
}