#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*/ uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ strtoull(char *, int /*<<< orphan*/ *, int);
uint64_t get_kernel_symbol_addr(char *target) {
  char cmd[256];
  FILE *fp = NULL;
  char addr[17];
  snprintf(cmd, 256, "nm /System/Library/Kernels/kernel | grep \"%s\"$",
           target);
  fp = popen(cmd, "r");
  fgets(addr, 17, fp);
  pclose(fp);
  return (uint64_t)strtoull(addr, NULL, 16);
}


x86 assembly (clang, -O0):
<get_kernel_symbol_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x118(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x100,%esi
lea    0xe7f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe9b(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x118(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x118(%rbp),%rdx
mov    $0x11,%esi
call   1050 <fgets@plt>
mov    -0x118(%rbp),%rdi
call   1030 <pclose@plt>
lea    -0x130(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtoull@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_kernel_symbol_addr>:
push   %r14
push   %rbx
sub    $0x128,%rsp
mov    %rdi,%rcx
lea    0xe9c(%rip),%rdx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1040 <snprintf@plt>
lea    0xeb2(%rip),%rsi
mov    %rbx,%rdi
call   1070 <popen@plt>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x11,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %rbx,%rdi
call   1030 <pclose@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <strtoull@plt>
add    $0x128,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [47 x i8] c"nm /System/Library/Kernels/kernel | grep \22%s\22$\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_kernel_symbol_addr(i8* %0) {
%2 = alloca i8*
%3 = alloca [256 x i8]
%4 = alloca i32*
%5 = alloca [17 x i8]
store i8* %0, i8** %2
store i32* null, i32** %4
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = load i8*, i8** %2
%8 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%10 = call i32* @popen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %4
%11 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 0
%12 = load i32*, i32** %4
%13 = call i32 @fgets(i8* %11, i32 17, i32* %12)
%14 = load i32*, i32** %4
%15 = call i32 @pclose(i32* %14)
%16 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 0
%17 = call i32 @strtoull(i8* %16, i32* null, i32 16)
ret i32 %17
}

LLVM IR (-O2):
@.str = [47 x i8] c"nm /System/Library/Kernels/kernel | grep \22%s\22$\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_kernel_symbol_addr(i8* %0) {
%2 = alloca [256 x i8]
%3 = alloca [17 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [17 x i8], [17 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = call i32* @popen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 @fgets(i8* %5, i32 17, i32* %7)
%9 = call i32 @pclose(i32* %7)
%10 = call i32 @strtoull(i8* nocapture %5, i32* null, i32 16)
ret i32 %10
}