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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
scalar_t__ strlen(char const *);
char *sym_by_value(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
const char *opt_ram(uint8_t addr) {
  static char *buf = NULL;
  const char *sym;
  if (buf) {
    free(buf);
    buf = NULL;
  }
  sym = sym_by_value(addr, 0x0002, NULL);
  if (!sym)
    return "";
  buf = malloc(strlen(sym) + 4);
  if (!buf) {
    perror("malloc");
    exit(1);
  }
  sprintf(buf, "\t; %s", sym);
  return buf;
}


x86 assembly (clang, -O0):
<opt_ram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
cmpq   $0x0,0x2edd(%rip)
je     11a0 <opt_ram+0x30>
mov    0x2ed0(%rip),%rdi
call   1030 <free@plt>
movq   $0x0,0x2ec0(%rip)
mov    -0xc(%rbp),%edi
mov    $0x2,%esi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <sym_by_value@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d0 <opt_ram+0x60>
lea    0xe3f(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1232 <opt_ram+0xc2>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x4,%rdi
call   1050 <malloc@plt>
mov    %rax,0x2e74(%rip)
cmpq   $0x0,0x2e6c(%rip)
jne    1210 <opt_ram+0xa0>
lea    0xdff(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e49(%rip),%rdi
mov    -0x18(%rbp),%rdx
lea    0xde5(%rip),%rsi
call   1080 <sprintf@plt>
mov    0x2e32(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opt_ram>:
push   %rbx
mov    %edi,%ebx
mov    0x2ee6(%rip),%rdi
test   %rdi,%rdi
je     118f <opt_ram+0x1f>
call   1030 <free@plt>
movq   $0x0,0x2ed1(%rip)
mov    %ebx,%edi
mov    $0x2,%esi
xor    %edx,%edx
call   1060 <sym_by_value@plt>
test   %rax,%rax
je     11dd <opt_ram+0x6d>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
lea    0x4(%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,0x2ea3(%rip)
test   %rax,%rax
je     11e6 <opt_ram+0x76>
lea    0xe3e(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1080 <sprintf@plt>
mov    0x2e85(%rip),%rax
pop    %rbx
ret
lea    0xe22(%rip),%rax
pop    %rbx
ret
lea    0xe13(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@opt_ram.buf = internal global i8* null
@.str = [1 x i8] zeroinitializer
@.str.1 = [7 x i8] c"malloc\00"
@.str.2 = [6 x i8] c"\09; %s\00"
define i8* @opt_ram(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
%5 = load i8*, i8** @opt_ram.buf
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i8*, i8** @opt_ram.buf
%9 = call i32 @free(i8* %8)
store i8* null, i8** @opt_ram.buf
br label %10
10: ; preds = %7, %1
%11 = load i32, i32* %3
%12 = call i8* @sym_by_value(i32 %11, i32 2, i32* null)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %10
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %2
br label %31
16: ; preds = %10
%17 = load i8*, i8** %4
%18 = call i64 @strlen(i8* %17)
%19 = add nsw i64 %18, 4
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** @opt_ram.buf
%21 = load i8*, i8** @opt_ram.buf
%22 = icmp ne i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %16
%27 = load i8*, i8** @opt_ram.buf
%28 = load i8*, i8** %4
%29 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = load i8*, i8** @opt_ram.buf
store i8* %30, i8** %2
br label %31
31: ; preds = %26, %15
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
@opt_ram.buf = internal unnamed_addr global i8* null
@.str = [1 x i8] zeroinitializer
@.str.1 = [7 x i8] c"malloc\00"
@.str.2 = [6 x i8] c"\09; %s\00"
define i8* @opt_ram(i32 %0) {
%2 = load i8*, i8** @opt_ram.buf
%3 = icmp eq i8* %2, null
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = tail call i32 @free(i8* %2)
store i8* null, i8** @opt_ram.buf
br label %6
6: ; preds = %4, %1
%7 = tail call i8* @sym_by_value(i32 %0, i32 2, i32* null)
%8 = icmp eq i8* %7, null
br i1 %8, label %20, label %9
9: ; preds = %6
%10 = tail call i64 @strlen(i8* %7)
%11 = add nsw i64 %10, 4
%12 = tail call i8* @malloc(i64 %11)
store i8* %12, i8** @opt_ram.buf
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = tail call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%19 = load i8*, i8** @opt_ram.buf
br label %20
20: ; preds = %6, %17
%21 = phi i8* [ %19, %17 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %6 ]
ret i8* %21
}