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

/* Variables and functions */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ strstr(char *, char *);
char *strtok(char *, char *);
unsigned long strtol(char *, int /*<<< orphan*/ *, int);
unsigned long getAddress(char *strName) {
  char buf[1000] = {0};
  char *sAddr = NULL;
  unsigned long uRet = 0;
  FILE *fp = NULL;
  sprintf(buf, "/proc/%d/maps", getpid());
  if (!(fp = fopen(buf, "r"))) {
    perror("Error when open file\n");
    return uRet;
  }
  while (fgets(buf, sizeof(buf), fp)) {
    if (strstr(buf, strName)) {
      sAddr = strtok(buf, "-");
      uRet = strtol(sAddr, NULL, 16);
      break;
    }
  }
  return uRet;
}


x86 assembly (clang, -O0):
<getAddress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x400(%rbp),%rdi
xor    %esi,%esi
mov    $0x3e8,%edx
call   1040 <memset@plt>
movq   $0x0,-0x408(%rbp)
movq   $0x0,-0x410(%rbp)
movq   $0x0,-0x418(%rbp)
lea    -0x400(%rbp),%rax
mov    %rax,-0x420(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x420(%rbp),%rdi
mov    %eax,%edx
lea    0xe08(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x400(%rbp),%rdi
lea    0xe03(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
jne    123d <getAddress+0xad>
lea    0xde8(%rip),%rdi
call   1080 <perror@plt>
mov    -0x410(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12c9 <getAddress+0x139>
jmp    1242 <getAddress+0xb2>
lea    -0x400(%rbp),%rdi
mov    -0x418(%rbp),%rdx
mov    $0x3e8,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     12be <getAddress+0x12e>
lea    -0x400(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     12b9 <getAddress+0x129>
lea    -0x400(%rbp),%rdi
lea    0xd9a(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x408(%rbp)
mov    -0x408(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %rax,-0x410(%rbp)
jmp    12be <getAddress+0x12e>
jmp    1242 <getAddress+0xb2>
mov    -0x410(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getAddress>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x3e8,%rsp
mov    %rdi,%r15
mov    %rsp,%rbx
mov    $0x3e8,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe3f(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   10a0 <sprintf@plt>
lea    0xe3c(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1248 <getAddress+0xb8>
mov    %rax,%rbx
mov    %rsp,%r12
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x3e8,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1236 <getAddress+0xa6>
mov    %r12,%rdi
mov    %r15,%rsi
call   10b0 <strstr@plt>
test   %rax,%rax
je     11f0 <getAddress+0x60>
lea    0xe0a(%rip),%rsi
mov    %rsp,%rdi
call   1090 <strtok@plt>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %rax,%r14
mov    %r14,%rax
add    $0x3e8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdc1(%rip),%rdi
call   1080 <perror@plt>
xor    %r14d,%r14d
jmp    1236 <getAddress+0xa6>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [22 x i8] c"Error when open file\0A\00"
@.str.3 = [2 x i8] c"-\00"
define i64 @getAddress(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca [1000 x i8]
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
store i8* %0, i8** %3
%8 = bitcast [1000 x i8]* %4 to i8*
store i8* null, i8** %5
store i64 0, i64* %6
store i32* null, i32** %7
%9 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%10 = call i32 (...) @getpid()
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %7
%14 = icmp ne i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%17 = load i64, i64* %6
store i64 %17, i64* %2
br label %37
18: ; preds = %1
br label %19
19: ; preds = %34, %18
%20 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%21 = load i32*, i32** %7
%22 = call i64 @fgets(i8* %20, i32 1000, i32* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %19
%25 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%26 = load i8*, i8** %3
%27 = call i64 @strstr(i8* %25, i8* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %24
%30 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%31 = call i8* @strtok(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %31, i8** %5
%32 = load i8*, i8** %5
%33 = call i64 @strtol(i8* %32, i32* null, i32 16)
store i64 %33, i64* %6
br label %35
34: ; preds = %24
br label %19
35: ; preds = %29, %19
%36 = load i64, i64* %6
store i64 %36, i64* %2
br label %37
37: ; preds = %35, %15
%38 = load i64, i64* %2
ret i64 %38
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/maps\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [22 x i8] c"Error when open file\0A\00"
@.str.3 = [2 x i8] c"-\00"
define i64 @getAddress(i8* %0) {
%2 = alloca [1000 x i8]
%3 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%4 = tail call i32 (...) @getpid()
%5 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i32* @fopen(i8* %3, 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 %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %19
10: ; preds = %1, %13
%11 = call i64 @fgets(i8* %3, i32 1000, i32* %6)
%12 = icmp eq i64 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = call i64 @strstr(i8* %3, i8* %0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %10, label %16
16: ; preds = %13
%17 = call i8* @strtok(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%18 = call i64 @strtol(i8* nocapture %17, i32* null, i32 16)
br label %19
19: ; preds = %10, %16, %8
%20 = phi i64 [ 0, %8 ], [ %18, %16 ], [ 0, %10 ]
ret i64 %20
}