#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 */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ stdout;
char *strchr(char *, char);
char *strrchr(char *, char);
int /*<<< orphan*/ strtok(char *, char *);
int strtol(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ system(char *);
int main(int argc, char *argv[]) {
  FILE *fp;
  char buf[256], *ptr;
  int i, j;
  fp = popen("ldd /home/bbs/bin/bbs|grep libc", "r");
  if (!fp) {
    printf("Error!\n");
    return -1;
  }
  fgets(buf, sizeof(buf), fp);
  pclose(fp);
  ptr = strchr(buf, '(');
  if (!ptr) {
    printf("ldd error!\n");
    return -1;
  }
  strtok(ptr + 1, ")");
  i = strtol(ptr + 1, NULL, 16);
  while (fgets(buf, sizeof(buf), stdin)) {
    fputs(buf, stdout);
    ptr = strrchr(buf, 'x');
    if (!ptr || ptr == buf || '0' != *(ptr - 1))
      continue;
    j = strtol(ptr - 1, NULL, 16);
    snprintf(buf, sizeof(buf), "addr2line -e /usr/lib/debug/libc.so.6 0x%x",
             j - i);
    system(buf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe30(%rip),%rdi
lea    0xe49(%rip),%rsi
call   10c0 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1203 <main+0x53>
lea    0xe30(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1383 <main+0x1d3>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   10a0 <fgets@plt>
mov    -0x18(%rbp),%rdi
call   1060 <pclose@plt>
lea    -0x120(%rbp),%rdi
mov    $0x28,%esi
call   1040 <strchr@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    125f <main+0xaf>
lea    0xddc(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1383 <main+0x1d3>
mov    -0x128(%rbp),%rdi
add    $0x1,%rdi
lea    0xdc5(%rip),%rsi
call   10d0 <strtok@plt>
mov    -0x128(%rbp),%rdi
add    $0x1,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   10b0 <strtol@plt>
mov    %eax,-0x12c(%rbp)
lea    -0x120(%rbp),%rdi
mov    0x2d45(%rip),%rax
mov    (%rax),%rdx
mov    $0x100,%esi
call   10a0 <fgets@plt>
cmp    $0x0,%eax
je     137c <main+0x1cc>
lea    -0x120(%rbp),%rdi
mov    0x2d11(%rip),%rax
mov    (%rax),%esi
call   1090 <fputs@plt>
lea    -0x120(%rbp),%rdi
mov    $0x78,%esi
call   1080 <strrchr@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
je     1323 <main+0x173>
mov    -0x128(%rbp),%rax
lea    -0x120(%rbp),%rcx
cmp    %rcx,%rax
je     1323 <main+0x173>
mov    -0x128(%rbp),%rax
movsbl -0x1(%rax),%ecx
mov    $0x30,%eax
cmp    %ecx,%eax
je     1328 <main+0x178>
jmp    1295 <main+0xe5>
mov    -0x128(%rbp),%rdi
add    $0xffffffffffffffff,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   10b0 <strtol@plt>
mov    %eax,-0x130(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x130(%rbp),%ecx
sub    -0x12c(%rbp),%ecx
mov    $0x100,%esi
lea    0xcd2(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x120(%rbp),%rdi
call   1030 <system@plt>
jmp    1295 <main+0xe5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
lea    0xe3a(%rip),%rdi
lea    0xe53(%rip),%rsi
call   10c0 <popen@plt>
test   %rax,%rax
je     12df <main+0x12f>
mov    %rax,%rbx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   10a0 <fgets@plt>
mov    %rbx,%rdi
call   1060 <pclose@plt>
mov    %rbp,%rdi
mov    $0x28,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     12e8 <main+0x138>
mov    %rax,%rbx
add    $0x1,%rbx
lea    0xe05(%rip),%rsi
mov    %rbx,%rdi
call   10d0 <strtok@plt>
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10b0 <strtol@plt>
mov    %eax,%r15d
mov    0x2da7(%rip),%r12
mov    (%r12),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10a0 <fgets@plt>
test   %eax,%eax
je     12ff <main+0x14f>
mov    0x2d74(%rip),%rbp
lea    0xdb9(%rip),%r14
jmp    1285 <main+0xd5>
nopl   (%rax)
mov    (%r12),%rdx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10a0 <fgets@plt>
test   %eax,%eax
je     12fc <main+0x14c>
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1090 <fputs@plt>
mov    %rbx,%rdi
mov    $0x78,%esi
call   1080 <strrchr@plt>
test   %rax,%rax
je     1270 <main+0xc0>
cmp    %rbx,%rax
je     1270 <main+0xc0>
cmpb   $0x30,-0x1(%rax)
jne    1270 <main+0xc0>
add    $0xffffffffffffffff,%rax
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10b0 <strtol@plt>
sub    %r15d,%eax
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
mov    %eax,%ecx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
jmp    1270 <main+0xc0>
lea    0xd69(%rip),%rdi
jmp    12ef <main+0x13f>
lea    0xd67(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
jmp    12ff <main+0x14f>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"ldd /home/bbs/bin/bbs|grep libc\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"Error!\0A\00"
@.str.3 = [12 x i8] c"ldd error!\0A\00"
@.str.4 = [2 x i8] c")\00"
@stdin = global i32* null
@stdout = global i32 0
@.str.5 = [43 x i8] c"addr2line -e /usr/lib/debug/libc.so.6 0x%x\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [256 x i8]
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32* @popen(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
16: ; preds = %2
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%18 = load i32*, i32** %6
%19 = call i32 @fgets(i8* %17, i32 256, i32* %18)
%20 = load i32*, i32** %6
%21 = call i32 @pclose(i32* %20)
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%23 = call i8* @strchr(i8* %22, i8 signext 40)
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = icmp ne i8* %24, null
br i1 %25, label %28, label %26
26: ; preds = %16
%27 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %71
28: ; preds = %16
%29 = load i8*, i8** %8
%30 = getelementptr inbounds i8, i8* %29, i64 1
%31 = call i32 @strtok(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%32 = load i8*, i8** %8
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = call i32 @strtol(i8* %33, i32* null, i32 16)
store i32 %34, i32* %9
br label %35
35: ; preds = %59, %58, %28
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%37 = load i32*, i32** @stdin
%38 = call i32 @fgets(i8* %36, i32 256, i32* %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %70
40: ; preds = %35
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%42 = load i32, i32* @stdout
%43 = call i32 @fputs(i8* %41, i32 %42)
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%45 = call i8* @strrchr(i8* %44, i8 signext 120)
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %58
48: ; preds = %40
%49 = load i8*, i8** %8
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%51 = icmp eq i8* %49, %50
br i1 %51, label %58, label %52
52: ; preds = %48
%53 = load i8*, i8** %8
%54 = getelementptr inbounds i8, i8* %53, i64 -1
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp ne i32 48, %56
br i1 %57, label %58, label %59
58: ; preds = %52, %48, %40
br label %35
59: ; preds = %52
%60 = load i8*, i8** %8
%61 = getelementptr inbounds i8, i8* %60, i64 -1
%62 = call i32 @strtol(i8* %61, i32* null, i32 16)
store i32 %62, i32* %10
%63 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%64 = load i32, i32* %10
%65 = load i32, i32* %9
%66 = sub nsw i32 %64, %65
%67 = call i32 @snprintf(i8* %63, i32 256, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %66)
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%69 = call i32 @system(i8* %68)
br label %35
70: ; preds = %35
store i32 0, i32* %3
br label %71
71: ; preds = %70, %26, %14
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@.str = [32 x i8] c"ldd /home/bbs/bin/bbs|grep libc\00"
@.str.1 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c")\00"
@stdin = global i32* null
@stdout = global i32 0
@.str.5 = [43 x i8] c"addr2line -e /usr/lib/debug/libc.so.6 0x%x\00"
@str = [7 x i8] c"Error!\00"
@str.6 = [11 x i8] c"ldd error!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @popen(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
br label %43
9: ; preds = %2
%10 = call i32 @fgets(i8* %4, i32 256, i32* %5)
%11 = call i32 @pclose(i32* %5)
%12 = call i8* @strchr(i8* %4, i8 signext 40)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.6, i64 0, i64 0))
br label %43
16: ; preds = %9
%17 = getelementptr inbounds i8, i8* %12, i64 1
%18 = call i32 @strtok(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%19 = call i32 @strtol(i8* nocapture %17, i32* null, i32 16)
%20 = load i32*, i32** @stdin
%21 = call i32 @fgets(i8* %4, i32 256, i32* %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %43, label %23
23: ; preds = %16, %34
%24 = load i32, i32* @stdout
%25 = call i32 @fputs(i8* %4, i32 %24)
%26 = call i8* @strrchr(i8* %4, i8 signext 120)
%27 = icmp eq i8* %26, null
%28 = icmp eq i8* %26, %4
%29 = or i1 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %23
%31 = getelementptr inbounds i8, i8* %26, i64 -1
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 48
br i1 %33, label %38, label %34
34: ; preds = %23, %30, %38
%35 = load i32*, i32** @stdin
%36 = call i32 @fgets(i8* %4, i32 256, i32* %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %43, label %23
38: ; preds = %30
%39 = call i32 @strtol(i8* nocapture %31, i32* null, i32 16)
%40 = sub nsw i32 %39, %19
%41 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %40)
%42 = call i32 @system(i8* %4)
br label %34
43: ; preds = %34, %16, %14, %7
%44 = phi i32 [ -1, %14 ], [ -1, %7 ], [ 0, %16 ], [ 0, %34 ]
ret i32 %44
}