#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 */
struct r_debug {
  struct link_map *r_map;
};
struct link_map {
  char *l_name;
};

/* Variables and functions */
struct r_debug _r_debug;
uintptr_t strtoul(char const *, char **, int /*<<< orphan*/);
void NaClHandleRDebug(const char *switch_value, char *argv0) {
  char *endp = NULL;
  uintptr_t r_debug_addr = strtoul(switch_value, &endp, 0);
  if (r_debug_addr != 0 && *endp == '\0') {
    struct link_map *l;
    struct r_debug *bootstrap_r_debug = (struct r_debug *)r_debug_addr;
    *bootstrap_r_debug = _r_debug;
    l = _r_debug.r_map;
    if (l->l_name[0] == '\0')
      l->l_name = argv0;
  }
}


x86 assembly (clang, -O0):
<NaClHandleRDebug>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a0 <NaClHandleRDebug+0x90>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11a0 <NaClHandleRDebug+0x90>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2e7f(%rip),%rcx
mov    (%rcx),%rcx
mov    %rcx,(%rax)
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    119b <NaClHandleRDebug+0x8b>
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
jmp    11a0 <NaClHandleRDebug+0x90>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<NaClHandleRDebug>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <strtoul@plt>
test   %rax,%rax
je     1157 <NaClHandleRDebug+0x47>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    1157 <NaClHandleRDebug+0x47>
mov    0x2ea5(%rip),%rcx
mov    (%rcx),%rdx
mov    %rdx,(%rax)
mov    (%rcx),%rax
mov    (%rax),%rcx
cmpb   $0x0,(%rcx)
jne    1157 <NaClHandleRDebug+0x47>
mov    %rbx,(%rax)
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.r_debug = type { %struct.link_map* }
%struct.link_map = type { i8* }
@_r_debug = global %struct.r_debug zeroinitializer
define void @NaClHandleRDebug(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.link_map*
%8 = alloca %struct.r_debug*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i8* null, i8** %5
%9 = load i8*, i8** %3
%10 = call i64 @strtoul(i8* %9, i8** %5, i32 0)
store i64 %10, i64* %6
%11 = load i64, i64* %6
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %36
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %36
18: ; preds = %13
%19 = load i64, i64* %6
%20 = inttoptr i64 %19 to %struct.r_debug*
store %struct.r_debug* %20, %struct.r_debug** %8
%21 = load %struct.r_debug*, %struct.r_debug** %8
%22 = bitcast %struct.r_debug* %21 to i8*
%23 = load %struct.link_map*, %struct.link_map** getelementptr inbounds (%struct.r_debug, %struct.r_debug* @_r_debug, i32 0, i32 0)
store %struct.link_map* %23, %struct.link_map** %7
%24 = load %struct.link_map*, %struct.link_map** %7
%25 = getelementptr inbounds %struct.link_map, %struct.link_map* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = getelementptr inbounds i8, i8* %26, i64 0
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %18
%32 = load i8*, i8** %4
%33 = load %struct.link_map*, %struct.link_map** %7
%34 = getelementptr inbounds %struct.link_map, %struct.link_map* %33, i32 0, i32 0
store i8* %32, i8** %34
br label %35
35: ; preds = %31, %18
br label %36
36: ; preds = %35, %13, %2
ret void
}

LLVM IR (-O2):
%struct.r_debug = type { %struct.link_map* }
%struct.link_map = type { i8* }
@_r_debug = global %struct.r_debug zeroinitializer
define void @NaClHandleRDebug(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%5 = call i64 @strtoul(i8* %0, i8** %3, i32 0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %20, label %7
7: ; preds = %2
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %7
%12 = inttoptr i64 %5 to i64*
%13 = load i64, i64* bitcast (%struct.r_debug* @_r_debug to i64*)
store i64 %13, i64* %12
%14 = load %struct.link_map*, %struct.link_map** getelementptr inbounds (%struct.r_debug, %struct.r_debug* @_r_debug, i64 0, i32 0)
%15 = getelementptr inbounds %struct.link_map, %struct.link_map* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %11
store i8* %1, i8** %15
br label %20
20: ; preds = %11, %19, %7, %2
ret void
}