#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*/ *dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
char *sprintf_alloc(char *, char *);
char *is_layer(void *handle) {
  int valid = 0;
  char *prefix, *fname;
  prefix = (char *)dlsym(handle, "harmony_layer_name");
  if (!prefix)
    return NULL;
  fname = sprintf_alloc("%s_generate", prefix);
  if (!fname) {
    perror("Could not allocate space for function name");
    exit(-1);
  }
  if (dlsym(handle, fname) != NULL) {
    valid = 1;
  } else {
    sprintf(fname, "%s_analyze", prefix);
    if (dlsym(handle, fname) != NULL)
      valid = 1;
  }
  free(fname);
  return (valid ? prefix : NULL);
}


x86 assembly (clang, -O0):
<is_layer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119f <is_layer+0x3f>
movq   $0x0,-0x8(%rbp)
jmp    1261 <is_layer+0x101>
mov    -0x20(%rbp),%rsi
lea    0xe69(%rip),%rdi
call   1040 <sprintf_alloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d4 <is_layer+0x74>
lea    0xe5a(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <dlsym@plt>
cmp    $0x0,%rax
je     11f7 <is_layer+0x97>
movl   $0x1,-0x14(%rbp)
jmp    122e <is_layer+0xce>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe44(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <dlsym@plt>
cmp    $0x0,%rax
je     1229 <is_layer+0xc9>
movl   $0x1,-0x14(%rbp)
jmp    122e <is_layer+0xce>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x14(%rbp)
je     124e <is_layer+0xee>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1259 <is_layer+0xf9>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    1259 <is_layer+0xf9>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_layer>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe91(%rip),%rsi
call   1060 <dlsym@plt>
test   %rax,%rax
je     11d1 <is_layer+0x71>
mov    %rax,%rbx
lea    0xe90(%rip),%rdi
mov    %rax,%rsi
call   1040 <sprintf_alloc@plt>
test   %rax,%rax
je     11dc <is_layer+0x7c>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
call   1060 <dlsym@plt>
test   %rax,%rax
jne    11c7 <is_layer+0x67>
lea    0xea0(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   1070 <sprintf@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <dlsym@plt>
test   %rax,%rax
cmove  %rax,%rbx
mov    %r15,%rdi
call   1030 <free@plt>
jmp    11d3 <is_layer+0x73>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe3c(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"harmony_layer_name\00"
@.str.1 = [12 x i8] c"%s_generate\00"
@.str.2 = [43 x i8] c"Could not allocate space for function name\00"
@.str.3 = [11 x i8] c"%s_analyze\00"
define i8* @is_layer(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i32 0, i32* %4
%7 = load i8*, i8** %3
%8 = call i32* @dlsym(i8* %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%9 = bitcast i32* %8 to i8*
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %1
store i8* null, i8** %2
br label %47
13: ; preds = %1
%14 = load i8*, i8** %5
%15 = call i8* @sprintf_alloc(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %13
%22 = load i8*, i8** %3
%23 = load i8*, i8** %6
%24 = call i32* @dlsym(i8* %22, i8* %23)
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 1, i32* %4
br label %37
27: ; preds = %21
%28 = load i8*, i8** %6
%29 = load i8*, i8** %5
%30 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %3
%32 = load i8*, i8** %6
%33 = call i32* @dlsym(i8* %31, i8* %32)
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %36
35: ; preds = %27
store i32 1, i32* %4
br label %36
36: ; preds = %35, %27
br label %37
37: ; preds = %36, %26
%38 = load i8*, i8** %6
%39 = call i32 @free(i8* %38)
%40 = load i32, i32* %4
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = load i8*, i8** %5
br label %45
44: ; preds = %37
br label %45
45: ; preds = %44, %42
%46 = phi i8* [ %43, %42 ], [ null, %44 ]
store i8* %46, i8** %2
br label %47
47: ; preds = %45, %12
%48 = load i8*, i8** %2
ret i8* %48
}

LLVM IR (-O2):
@.str = [19 x i8] c"harmony_layer_name\00"
@.str.1 = [12 x i8] c"%s_generate\00"
@.str.2 = [43 x i8] c"Could not allocate space for function name\00"
@.str.3 = [11 x i8] c"%s_analyze\00"
define i8* @is_layer(i8* %0) {
%2 = tail call i32* @dlsym(i8* %0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%3 = bitcast i32* %2 to i8*
%4 = icmp eq i32* %2, null
br i1 %4, label %22, label %5
5: ; preds = %1
%6 = tail call i8* @sprintf_alloc(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = tail call i32 @perror(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %5
%12 = tail call i32* @dlsym(i8* %0, i8* %6)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = tail call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%16 = tail call i32* @dlsym(i8* %0, i8* %6)
%17 = icmp eq i32* %16, null
%18 = select i1 %17, i8* null, i8* %3
br label %19
19: ; preds = %14, %11
%20 = phi i8* [ %3, %11 ], [ %18, %14 ]
%21 = tail call i32 @free(i8* %6)
br label %22
22: ; preds = %1, %19
%23 = phi i8* [ %20, %19 ], [ null, %1 ]
ret i8* %23
}