#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 ask(char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ show_warning(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int sscanf(char *, char *, long *);
scalar_t__ strlen(char *);
void *find_sym(char *type, char *name) {
  char *req;
  char ans[128];
  int i;
  void *addr = NULL;
  size_t len = 2;
  len += strlen(type);
  len += strlen(name);
  req = malloc(len);
  if (!req) {
    return NULL;
  }
  sprintf(req, "%s %s", type, name);
  i = ask(req, ans);
  free(req);
  if (i != 0)
    return NULL;
  i = sscanf(ans, "%lx", (long *)&addr);
  if (i != 1) {
    char message[256];
    sprintf(message, "Failed sscanf looking for '%s': %d 0x%lx", name, i,
            (long)addr);
    show_warning(message);
    return NULL;
  }
  return addr;
}


x86 assembly (clang, -O0):
<find_sym>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0xb0(%rbp)
movq   $0x2,-0xb8(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    -0xb8(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
add    -0xb8(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    -0xb8(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ef <find_sym+0x7f>
movq   $0x0,-0x8(%rbp)
jmp    12b9 <find_sym+0x149>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    -0x20(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
call   1040 <ask@plt>
mov    %eax,-0xa4(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0xa4(%rbp)
je     1242 <find_sym+0xd2>
movq   $0x0,-0x8(%rbp)
jmp    12b9 <find_sym+0x149>
lea    -0xa0(%rbp),%rdi
lea    -0xb0(%rbp),%rdx
lea    0xdd4(%rip),%rsi
call   1080 <sscanf@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x1,-0xa4(%rbp)
je     12ae <find_sym+0x13e>
lea    -0x1c0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0xa4(%rbp),%ecx
mov    -0xb0(%rbp),%r8
lea    0xd78(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
lea    -0x1c0(%rbp),%rdi
call   1060 <show_warning@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b9 <find_sym+0x149>
mov    -0xb0(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_sym>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x190,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
movq   $0x0,0x8(%rsp)
call   1050 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     120a <find_sym+0x9a>
mov    %rax,%rbx
lea    0xe46(%rip),%rsi
xor    %r15d,%r15d
mov    %rax,%rdi
mov    %r12,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1090 <sprintf@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <ask@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
test   %ebp,%ebp
jne    123d <find_sym+0xcd>
lea    0xe3c(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    120f <find_sym+0x9f>
mov    0x8(%rsp),%r15
jmp    123d <find_sym+0xcd>
xor    %r15d,%r15d
jmp    123d <find_sym+0xcd>
mov    0x8(%rsp),%r8
lea    0xdeb(%rip),%rsi
xor    %r15d,%r15d
lea    0x90(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1060 <show_warning@plt>
mov    %r15,%rax
add    $0x190,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"%s %s\00"
@.str.1 = [4 x i8] c"%lx\00"
@.str.2 = [41 x i8] c"Failed sscanf looking for '%s': %d 0x%lx\00"
define i8* @find_sym(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [128 x i8]
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca [256 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %9
store i64 2, i64* %10
%12 = load i8*, i8** %4
%13 = call i64 @strlen(i8* %12)
%14 = load i64, i64* %10
%15 = add i64 %14, %13
store i64 %15, i64* %10
%16 = load i8*, i8** %5
%17 = call i64 @strlen(i8* %16)
%18 = load i64, i64* %10
%19 = add i64 %18, %17
store i64 %19, i64* %10
%20 = load i64, i64* %10
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = icmp ne i8* %22, null
br i1 %23, label %25, label %24
24: ; preds = %2
store i8* null, i8** %3
br label %55
25: ; preds = %2
%26 = load i8*, i8** %6
%27 = load i8*, i8** %4
%28 = load i8*, i8** %5
%29 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %27, i8* %28)
%30 = load i8*, i8** %6
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%32 = call i32 @ask(i8* %30, i8* %31)
store i32 %32, i32* %8
%33 = load i8*, i8** %6
%34 = call i32 @free(i8* %33)
%35 = load i32, i32* %8
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %25
store i8* null, i8** %3
br label %55
38: ; preds = %25
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%40 = bitcast i8** %9 to i64*
%41 = call i32 @sscanf(i8* %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %40)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp ne i32 %42, 1
br i1 %43, label %44, label %53
44: ; preds = %38
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%46 = load i8*, i8** %5
%47 = load i32, i32* %8
%48 = load i8*, i8** %9
%49 = ptrtoint i8* %48 to i64
%50 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %45, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %46, i32 %47, i64 %49)
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%52 = call i32 @show_warning(i8* %51)
store i8* null, i8** %3
br label %55
53: ; preds = %38
%54 = load i8*, i8** %9
store i8* %54, i8** %3
br label %55
55: ; preds = %53, %44, %37, %24
%56 = load i8*, i8** %3
ret i8* %56
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s %s\00"
@.str.1 = [4 x i8] c"%lx\00"
@.str.2 = [41 x i8] c"Failed sscanf looking for '%s': %d 0x%lx\00"
define i8* @find_sym(i8* %0, i8* %1) {
%3 = alloca [128 x i8]
%4 = alloca i8*
%5 = alloca [256 x i8]
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%7 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%8 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%9 = add i64 %8, 2
%10 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%11 = add i64 %9, %10
%12 = tail call i8* @malloc(i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %31, label %14
14: ; preds = %2
%15 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%16 = call i32 @ask(i8* %12, i8* %6)
%17 = call i32 @free(i8* %12)
%18 = icmp eq i32 %16, 0
br i1 %18, label %19, label %31
19: ; preds = %14
%20 = bitcast i8** %4 to i64*
%21 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %20)
%22 = icmp eq i32 %21, 1
br i1 %22, label %29, label %23
23: ; preds = %19
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%25 = load i8*, i8** %4
%26 = ptrtoint i8* %25 to i64
%27 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %24, i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %1, i32 %21, i64 %26)
%28 = call i32 @show_warning(i8* %24)
br label %31
29: ; preds = %19
%30 = load i8*, i8** %4
br label %31
31: ; preds = %14, %2, %29, %23
%32 = phi i8* [ null, %23 ], [ %30, %29 ], [ null, %2 ], [ null, %14 ]
ret i8* %32
}