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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
scalar_t__ strtoul(char *, int /*<<< orphan*/ *, int);
void *get_linker_addr(pid_t pid) {
  char buf[BUFSIZ], *tok[6];
  int i;
  FILE *fp;
  void *r = NULL;
  snprintf(buf, sizeof(buf), "/proc/%d/maps", pid);
  if ((fp = fopen(buf, "r")) == NULL) {
    perror("get_linker_addr: fopen");
    goto ret;
  }
  while (fgets(buf, sizeof(buf), fp)) {
    i = strlen(buf);
    if (i > 0 && buf[i - 1] == '\n')
      buf[i - 1] = 0;
    tok[0] = strtok(buf, " ");
    for (i = 1; i < 6; i++)
      tok[i] = strtok(NULL, " ");
    if (tok[5] && strcmp(tok[5], "/system/bin/linker64") == 0) {
      r = (void *)strtoul(tok[0], NULL, 16);
      goto close;
    }
  }
close:
  fclose(fp);
ret:
  return r;
}


x86 assembly (clang, -O0):
<get_linker_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x78(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x70(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x68(%rbp)
mov    %eax,%esi
mov    -0x4(%rbp),%ecx
lea    0xe1e(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x70(%rbp),%rdi
lea    0xe1c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x60(%rbp)
cmp    $0x0,%rax
jne    1216 <get_linker_addr+0x86>
lea    0xe04(%rip),%rdi
call   1090 <perror@plt>
jmp    131b <get_linker_addr+0x18b>
jmp    121b <get_linker_addr+0x8b>
mov    -0x70(%rbp),%rdi
mov    -0x78(%rbp),%rax
mov    %eax,%esi
mov    -0x60(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     130d <get_linker_addr+0x17d>
mov    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jle    1279 <get_linker_addr+0xe9>
mov    -0x70(%rbp),%rax
mov    -0x54(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1279 <get_linker_addr+0xe9>
mov    -0x70(%rbp),%rax
mov    -0x54(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x70(%rbp),%rdi
lea    0xda3(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x50(%rbp)
movl   $0x1,-0x54(%rbp)
cmpl   $0x6,-0x54(%rbp)
jge    12c8 <get_linker_addr+0x138>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd7e(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,%rcx
movslq -0x54(%rbp),%rax
mov    %rcx,-0x50(%rbp,%rax,8)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1294 <get_linker_addr+0x104>
cmpq   $0x0,-0x28(%rbp)
je     1308 <get_linker_addr+0x178>
mov    -0x28(%rbp),%rdi
lea    0xd4b(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1308 <get_linker_addr+0x178>
mov    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   10b0 <strtoul@plt>
mov    %rax,-0x68(%rbp)
jmp    1312 <get_linker_addr+0x182>
jmp    121b <get_linker_addr+0x8b>
jmp    1312 <get_linker_addr+0x182>
mov    -0x60(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x68(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_linker_addr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ecx
mov    0x2e39(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe3b(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <snprintf@plt>
lea    0xe37(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     12cd <get_linker_addr+0x13d>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12b1 <get_linker_addr+0x121>
lea    0xe1e(%rip),%r13
jmp    1227 <get_linker_addr+0x97>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12b1 <get_linker_addr+0x121>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    1240 <get_linker_addr+0xb0>
add    $0xffffffff,%eax
cmpb   $0xa,(%rbx,%rax,1)
jne    1240 <get_linker_addr+0xb0>
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    %r13,%rsi
call   10a0 <strtok@plt>
mov    %rax,%r12
xor    %edi,%edi
mov    %r13,%rsi
call   10a0 <strtok@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   10a0 <strtok@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   10a0 <strtok@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   10a0 <strtok@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   10a0 <strtok@plt>
test   %rax,%rax
je     1210 <get_linker_addr+0x80>
mov    %rax,%rdi
lea    0xd9a(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1210 <get_linker_addr+0x80>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10b0 <strtoul@plt>
mov    %rax,%rbx
jmp    12b3 <get_linker_addr+0x123>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd3c(%rip),%rdi
call   1090 <perror@plt>
xor    %ebx,%ebx
jmp    12bb <get_linker_addr+0x12b>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"get_linker_addr: fopen\00"
@.str.3 = [2 x i8] c" \00"
@.str.4 = [21 x i8] c"/system/bin/linker64\00"
define i8* @get_linker_addr(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [6 x i8*]
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
store i32 %0, i32* %2
%9 = load i32, i32* @BUFSIZ
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %3
%12 = alloca i8, i64 %10
store i64 %10, i64* %4
store i8* null, i8** %8
%13 = trunc i64 %10 to i32
%14 = load i32, i32* %2
%15 = call i32 @snprintf(i8* %12, i32 %13, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %7
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %76
20: ; preds = %1
br label %21
21: ; preds = %71, %20
%22 = trunc i64 %10 to i32
%23 = load i32*, i32** %7
%24 = call i64 @fgets(i8* %12, i32 %22, i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %72
26: ; preds = %21
%27 = call i32 @strlen(i8* %12)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %43
30: ; preds = %26
%31 = load i32, i32* %6
%32 = sub nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %12, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 10
br i1 %37, label %38, label %43
38: ; preds = %30
%39 = load i32, i32* %6
%40 = sub nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %12, i64 %41
store i8 0, i8* %42
br label %43
43: ; preds = %38, %30, %26
%44 = call i8* @strtok(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%45 = getelementptr inbounds [6 x i8*], [6 x i8*]* %5, i64 0, i64 0
store i8* %44, i8** %45
store i32 1, i32* %6
br label %46
46: ; preds = %54, %43
%47 = load i32, i32* %6
%48 = icmp slt i32 %47, 6
br i1 %48, label %49, label %57
49: ; preds = %46
%50 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [6 x i8*], [6 x i8*]* %5, i64 0, i64 %52
store i8* %50, i8** %53
br label %54
54: ; preds = %49
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 1
store i32 %56, i32* %6
br label %46
57: ; preds = %46
%58 = getelementptr inbounds [6 x i8*], [6 x i8*]* %5, i64 0, i64 5
%59 = load i8*, i8** %58
%60 = icmp ne i8* %59, null
br i1 %60, label %61, label %71
61: ; preds = %57
%62 = getelementptr inbounds [6 x i8*], [6 x i8*]* %5, i64 0, i64 5
%63 = load i8*, i8** %62
%64 = call i64 @strcmp(i8* %63, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%65 = icmp eq i64 %64, 0
br i1 %65, label %66, label %71
66: ; preds = %61
%67 = getelementptr inbounds [6 x i8*], [6 x i8*]* %5, i64 0, i64 0
%68 = load i8*, i8** %67
%69 = call i64 @strtoul(i8* %68, i32* null, i32 16)
%70 = inttoptr i64 %69 to i8*
store i8* %70, i8** %8
br label %73
71: ; preds = %61, %57
br label %21
72: ; preds = %21
br label %73
73: ; preds = %72, %66
%74 = load i32*, i32** %7
%75 = call i32 @fclose(i32* %74)
br label %76
76: ; preds = %73, %18
%77 = load i8*, i8** %8
%78 = load i8*, i8** %3
ret i8* %77
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"get_linker_addr: fopen\00"
@.str.3 = [2 x i8] c" \00"
@.str.4 = [21 x i8] c"/system/bin/linker64\00"
define i8* @get_linker_addr(i32 %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = call i64 @fgets(i8* %4, i32 %2, i32* %6)
%10 = icmp eq i64 %9, 0
br i1 %10, label %40, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %43
13: ; preds = %8, %37
%14 = call i32 @strlen(i8* %4)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %13
%17 = add nsw i32 %14, -1
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %4, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 10
br i1 %21, label %22, label %23
22: ; preds = %16
store i8 0, i8* %19
br label %23
23: ; preds = %22, %16, %13
%24 = call i8* @strtok(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%25 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%26 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%27 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%28 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%29 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i8* %29, null
br i1 %30, label %37, label %31
31: ; preds = %23
%32 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = call i64 @strtoul(i8* nocapture %24, i32* null, i32 16)
%36 = inttoptr i64 %35 to i8*
br label %40
37: ; preds = %31, %23
%38 = call i64 @fgets(i8* %4, i32 %2, i32* %6)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %13
40: ; preds = %37, %8, %34
%41 = phi i8* [ %36, %34 ], [ null, %8 ], [ null, %37 ]
%42 = call i32 @fclose(i32* %6)
br label %43
43: ; preds = %40, %11
%44 = phi i8* [ null, %11 ], [ %41, %40 ]
ret i8* %44
}