#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  scalar_t__ rm_so;
  scalar_t__ rm_eo;
};
typedef TYPE_1__ regmatch_t;
typedef int /*<<< orphan*/ regex_t;
typedef int /*<<< orphan*/ matches;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ REG_EXTENDED;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int regcomp(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
int regexec(int /*<<< orphan*/ *, char *, int, TYPE_1__ *, int /*<<< orphan*/);
int /*<<< orphan*/ regfree(int /*<<< orphan*/ *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int libamg_sip_get_register(char *response, int maxlen) {
  regex_t regex;
  regmatch_t matches[10];
  int reti;
  FILE *pipe;
  char buffer[256];
  pipe = popen("/sbin/asterisk -rx \"sip show registry\"", "r");
  if (pipe == NULL)
    return -1;
  reti =
      regcomp(&regex,
              "^[[:digit:].:]+[[:blank:]]+[A-Z][[:blank:]]+[[:alnum:]]+[[:"
              "blank:]]+[[:digit:]]+[[:blank:]]+([[:alpha:]]+[ ]?[[:alpha:]]*)",
              REG_EXTENDED);
  if (reti != 0) {
    fclose(pipe);
    return -1;
  }
  while (feof(pipe) == 0) {
    if (fgets(buffer, 256, pipe) == 0)
      break;
    reti = regexec(&regex, buffer, sizeof(matches) / sizeof(regmatch_t),
                   matches, 0);
    if (reti != 0)
      continue;
    if (matches[1].rm_so > 0 && (matches[1].rm_eo - matches[1].rm_so) > 0) {
      buffer[matches[1].rm_eo] = '\0';
      strncpy(response, &buffer[matches[1].rm_so], maxlen);
    }
  }
  fclose(pipe);
  regfree(&regex);
  return 0;
}


x86 assembly (clang, -O0):
<libamg_sip_get_register>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1d0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe67(%rip),%rdi
lea    0xe87(%rip),%rsi
call   10a0 <popen@plt>
mov    %rax,-0xd0(%rbp)
cmpq   $0x0,-0xd0(%rbp)
jne    11c6 <libamg_sip_get_register+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <libamg_sip_get_register+0x173>
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1060 <regcomp@plt>
mov    %eax,-0xc4(%rbp)
cmpl   $0x0,-0xc4(%rbp)
je     120a <libamg_sip_get_register+0x8a>
mov    -0xd0(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <libamg_sip_get_register+0x173>
jmp    120f <libamg_sip_get_register+0x8f>
mov    -0xd0(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%rax
jne    12d7 <libamg_sip_get_register+0x157>
lea    -0x1d0(%rbp),%rdi
mov    -0xd0(%rbp),%rdx
mov    $0x100,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    124c <libamg_sip_get_register+0xcc>
jmp    12d7 <libamg_sip_get_register+0x157>
lea    -0x1d0(%rbp),%rsi
lea    -0xc0(%rbp),%rcx
lea    -0x18(%rbp),%rdi
mov    $0xa,%edx
xor    %r8d,%r8d
call   1090 <regexec@plt>
mov    %eax,-0xc4(%rbp)
cmpl   $0x0,-0xc4(%rbp)
je     1283 <libamg_sip_get_register+0x103>
jmp    120f <libamg_sip_get_register+0x8f>
cmpq   $0x0,-0xb0(%rbp)
jle    12d2 <libamg_sip_get_register+0x152>
mov    -0xa8(%rbp),%rax
sub    -0xb0(%rbp),%rax
cmp    $0x0,%rax
jle    12d2 <libamg_sip_get_register+0x152>
mov    -0xa8(%rbp),%rax
movb   $0x0,-0x1d0(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
lea    -0x1d0(%rbp),%rsi
add    -0xb0(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
jmp    120f <libamg_sip_get_register+0x8f>
mov    -0xd0(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x18(%rbp),%rdi
call   1050 <regfree@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libamg_sip_get_register>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1b8,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
lea    0xe62(%rip),%rdi
lea    0xe82(%rip),%rsi
call   10a0 <popen@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     1200 <libamg_sip_get_register+0x80>
mov    %rax,%rbp
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
lea    0xe62(%rip),%rsi
lea    0xc(%rsp),%rdi
call   1060 <regcomp@plt>
test   %eax,%eax
je     11df <libamg_sip_get_register+0x5f>
mov    %rbp,%rdi
call   1040 <fclose@plt>
jmp    1200 <libamg_sip_get_register+0x80>
mov    %rbp,%rdi
call   1080 <feof@plt>
test   %rax,%rax
je     1214 <libamg_sip_get_register+0x94>
mov    %rbp,%rdi
call   1040 <fclose@plt>
lea    0xc(%rsp),%rdi
call   1050 <regfree@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x1b8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xb0(%rsp),%rbx
lea    0xc(%rsp),%r12
lea    0x10(%rsp),%r13
jmp    123d <libamg_sip_get_register+0xbd>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1080 <feof@plt>
test   %rax,%rax
jne    11ec <libamg_sip_get_register+0x6c>
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %rbp,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     11ec <libamg_sip_get_register+0x6c>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0xa,%edx
mov    %r13,%rcx
xor    %r8d,%r8d
call   1090 <regexec@plt>
test   %eax,%eax
jne    1230 <libamg_sip_get_register+0xb0>
mov    0x20(%rsp),%rax
test   %rax,%rax
jle    1230 <libamg_sip_get_register+0xb0>
mov    0x28(%rsp),%rcx
cmp    %rax,%rcx
jle    1230 <libamg_sip_get_register+0xb0>
movb   $0x0,0xb0(%rsp,%rcx,1)
lea    (%rsp,%rax,1),%rsi
add    $0xb0,%rsi
mov    %r15,%rdi
mov    %r14d,%edx
call   1030 <strncpy@plt>
jmp    1230 <libamg_sip_get_register+0xb0>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i64, i64 }
@.str = [39 x i8] c"/sbin/asterisk -rx \22sip show registry\22\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [123 x i8] c"^[[:digit:].:]+[[:blank:]]+[A-Z][[:blank:]]+[[:alnum:]]+[[:blank:]]+[[:digit:]]+[[:blank:]]+([[:alpha:]]+[ ]?[[:alpha:]]*)\00"
@REG_EXTENDED = global i32 0
define i32 @libamg_sip_get_register(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [10 x %struct.TYPE_3__]
%8 = alloca i32
%9 = alloca i32*
%10 = alloca [256 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = call i32* @popen(i8* getelementptr inbounds ([39 x i8], [39 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** %9
%12 = load i32*, i32** %9
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %72
15: ; preds = %2
%16 = load i32, i32* @REG_EXTENDED
%17 = call i32 @regcomp(i32* %6, i8* getelementptr inbounds ([123 x i8], [123 x i8]* @.str.2, i64 0, i64 0), i32 %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32*, i32** %9
%22 = call i32 @fclose(i32* %21)
store i32 -1, i32* %3
br label %72
23: ; preds = %15
br label %24
24: ; preds = %67, %40, %23
%25 = load i32*, i32** %9
%26 = call i64 @feof(i32* %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %68
28: ; preds = %24
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%30 = load i32*, i32** %9
%31 = call i64 @fgets(i8* %29, i32 256, i32* %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28
br label %68
34: ; preds = %28
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%36 = getelementptr inbounds [10 x %struct.TYPE_3__], [10 x %struct.TYPE_3__]* %7, i64 0, i64 0
%37 = call i32 @regexec(i32* %6, i8* %35, i32 10, %struct.TYPE_3__* %36, i32 0)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %34
br label %24
41: ; preds = %34
%42 = getelementptr inbounds [10 x %struct.TYPE_3__], [10 x %struct.TYPE_3__]* %7, i64 0, i64 1
%43 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %42, i32 0, i32 0
%44 = load i64, i64* %43
%45 = icmp sgt i64 %44, 0
br i1 %45, label %46, label %67
46: ; preds = %41
%47 = getelementptr inbounds [10 x %struct.TYPE_3__], [10 x %struct.TYPE_3__]* %7, i64 0, i64 1
%48 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %47, i32 0, i32 1
%49 = load i64, i64* %48
%50 = getelementptr inbounds [10 x %struct.TYPE_3__], [10 x %struct.TYPE_3__]* %7, i64 0, i64 1
%51 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %50, i32 0, i32 0
%52 = load i64, i64* %51
%53 = sub nsw i64 %49, %52
%54 = icmp sgt i64 %53, 0
br i1 %54, label %55, label %67
55: ; preds = %46
%56 = getelementptr inbounds [10 x %struct.TYPE_3__], [10 x %struct.TYPE_3__]* %7, i64 0, i64 1
%57 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %56, i32 0, i32 1
%58 = load i64, i64* %57
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 %58
store i8 0, i8* %59
%60 = load i8*, i8** %4
%61 = getelementptr inbounds [10 x %struct.TYPE_3__], [10 x %struct.TYPE_3__]* %7, i64 0, i64 1
%62 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %61, i32 0, i32 0
%63 = load i64, i64* %62
%64 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 %63
%65 = load i32, i32* %5
%66 = call i32 @strncpy(i8* %60, i8* %64, i32 %65)
br label %67
67: ; preds = %55, %46, %41
br label %24
68: ; preds = %33, %24
%69 = load i32*, i32** %9
%70 = call i32 @fclose(i32* %69)
%71 = call i32 @regfree(i32* %6)
store i32 0, i32* %3
br label %72
72: ; preds = %68, %20, %14
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i64, i64 }
@.str = [39 x i8] c"/sbin/asterisk -rx \22sip show registry\22\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [123 x i8] c"^[[:digit:].:]+[[:blank:]]+[A-Z][[:blank:]]+[[:alnum:]]+[[:blank:]]+[[:digit:]]+[[:blank:]]+([[:alpha:]]+[ ]?[[:alpha:]]*)\00"
@REG_EXTENDED = global i32 0
define i32 @libamg_sip_get_register(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca [10 x %struct.TYPE_3__]
%5 = alloca [256 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast [10 x %struct.TYPE_3__]* %4 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%9 = tail call i32* @popen(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %46, label %11
11: ; preds = %2
%12 = load i32, i32* @REG_EXTENDED
%13 = call i32 @regcomp(i32* %3, i8* getelementptr inbounds ([123 x i8], [123 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %11
%16 = call i64 @feof(i32* %9)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %43
18: ; preds = %15
%19 = getelementptr inbounds [10 x %struct.TYPE_3__], [10 x %struct.TYPE_3__]* %4, i64 0, i64 0
%20 = getelementptr inbounds [10 x %struct.TYPE_3__], [10 x %struct.TYPE_3__]* %4, i64 0, i64 1, i32 0
%21 = getelementptr inbounds [10 x %struct.TYPE_3__], [10 x %struct.TYPE_3__]* %4, i64 0, i64 1, i32 1
br label %24
22: ; preds = %11
%23 = call i32 @fclose(i32* %9)
br label %46
24: ; preds = %18, %40
%25 = call i64 @fgets(i8* %8, i32 256, i32* %9)
%26 = icmp eq i64 %25, 0
br i1 %26, label %43, label %27
27: ; preds = %24
%28 = call i32 @regexec(i32* %3, i8* %8, i32 10, %struct.TYPE_3__* %19, i32 0)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %40
30: ; preds = %27
%31 = load i64, i64* %20
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = load i64, i64* %21
%35 = icmp sgt i64 %34, %31
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %34
store i8 0, i8* %37
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %31
%39 = call i32 @strncpy(i8* %0, i8* %38, i32 %1)
br label %40
40: ; preds = %30, %33, %36, %27
%41 = call i64 @feof(i32* %9)
%42 = icmp eq i64 %41, 0
br i1 %42, label %24, label %43
43: ; preds = %40, %24, %15
%44 = call i32 @fclose(i32* %9)
%45 = call i32 @regfree(i32* %3)
br label %46
46: ; preds = %2, %43, %22
%47 = phi i32 [ -1, %22 ], [ 0, %43 ], [ -1, %2 ]
ret i32 %47
}