#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 */
char *dlerror();
void *dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, size_t);
int /*<<< orphan*/ stderr;
void *malloc(size_t size) {
  static void *(*pmalloc)();
  void *handle;
  char *error;
  if (!pmalloc) {
    *(void **)(&pmalloc) = dlsym(((void *)-1l), "malloc");
    if ((error = dlerror()) != NULL) {
      fprintf(stderr, "%s\n", error);
      exit(1);
    }
  }
  printf("malloc(%d) is called\n", size);
  return (pmalloc(size));
}


x86 assembly (clang, -O0):
<malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,0x2eec(%rip)
jne    11c1 <malloc+0x71>
mov    $0xffffffffffffffff,%rdi
lea    0xe88(%rip),%rsi
call   1060 <dlsym@plt>
mov    %rax,0x2ecc(%rip)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11bc <malloc+0x6c>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe5a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11c1 <malloc+0x71>
mov    -0x8(%rbp),%rsi
lea    0xe3f(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e78(%rip),%rcx
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   *%rcx
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<malloc>:
push   %rbx
mov    %rdi,%rbx
cmpq   $0x0,0x2ef4(%rip)
jne    1184 <malloc+0x34>
lea    0xe9b(%rip),%rsi
mov    $0xffffffffffffffff,%rdi
call   1060 <dlsym@plt>
mov    %rax,0x2ed8(%rip)
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
jne    11a2 <malloc+0x52>
lea    0xe80(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x2eb6(%rip),%rcx
mov    %rbx,%rdi
xor    %eax,%eax
pop    %rbx
jmp    *%rcx
mov    0x2e37(%rip),%rcx
mov    (%rcx),%edi
lea    0xe55(%rip),%rsi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@malloc.pmalloc = internal global i8* (...)* null
@.str = [7 x i8] c"malloc\00"
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [22 x i8] c"malloc(%d) is called\0A\00"
define i8* @malloc(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
store i64 %0, i64* %2
%5 = load i8* (...)*, i8* (...)** @malloc.pmalloc
%6 = icmp ne i8* (...)* %5, null
br i1 %6, label %17, label %7
7: ; preds = %1
%8 = call i8* @dlsym(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** bitcast (i8* (...)** @malloc.pmalloc to i8**)
%9 = call i8* (...) @dlerror()
store i8* %9, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %4
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %7
br label %17
17: ; preds = %16, %1
%18 = load i64, i64* %2
%19 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %18)
%20 = load i8* (...)*, i8* (...)** @malloc.pmalloc
%21 = load i64, i64* %2
%22 = bitcast i8* (...)* %20 to i8* (i64, ...)*
%23 = call i8* (i64, ...) %22(i64 %21)
ret i8* %23
}

LLVM IR (-O2):
@malloc.pmalloc = internal unnamed_addr global i8* (...)* null
@.str = [7 x i8] c"malloc\00"
@stderr = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [22 x i8] c"malloc(%d) is called\0A\00"
define i8* @malloc(i64 %0) {
%2 = load i8* (...)*, i8* (...)** @malloc.pmalloc
%3 = icmp eq i8* (...)* %2, null
br i1 %3, label %4, label %12
4: ; preds = %1
%5 = tail call i8* @dlsym(i8* inttoptr (i64 -1 to i8*), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** bitcast (i8* (...)** @malloc.pmalloc to i8**)
%6 = tail call i8* (...) @dlerror()
%7 = icmp eq i8* %6, null
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %4, %1
%13 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %0)
%14 = load i8* (i64, ...)*, i8* (i64, ...)** bitcast (i8* (...)** @malloc.pmalloc to i8* (i64, ...)**)
%15 = tail call i8* (i64, ...) %14(i64 %0)
ret i8* %15
}