#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__ {
  int rm_eo;
  int rm_so;
};
typedef TYPE_1__ regmatch_t;

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
char *strndup(char *, int);
char *string_from_regmatch(char *src, regmatch_t *pmatch, int substrno) {
  char *dest;
  int copylen = pmatch[substrno].rm_eo - pmatch[substrno].rm_so;
  if ((dest = strndup(src + pmatch[substrno].rm_so, (copylen + 1))) != NULL) {
    dest[copylen] = '\0';
  } else {
    perror("substring from pmatch, strndup out of memory");
    return NULL;
  }
  return dest;
}


x86 assembly (clang, -O0):
<string_from_regmatch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%eax
mov    -0x18(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
sub    0x4(%rcx,%rdx,8),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movslq 0x4(%rax,%rcx,8),%rax
add    %rax,%rdi
mov    -0x2c(%rbp),%esi
add    $0x1,%esi
call   1030 <strndup@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     118b <string_from_regmatch+0x6b>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a4 <string_from_regmatch+0x84>
lea    0xe6e(%rip),%rdi
call   1040 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ac <string_from_regmatch+0x8c>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_from_regmatch>:
push   %r14
push   %rbx
push   %rax
movslq %edx,%rax
movslq (%rsi,%rax,8),%rbx
movslq 0x4(%rsi,%rax,8),%rax
sub    %rax,%rbx
add    %rax,%rdi
lea    0x1(%rbx),%esi
call   1030 <strndup@plt>
mov    %rax,%r14
test   %rax,%rax
je     1156 <string_from_regmatch+0x36>
movb   $0x0,(%r14,%rbx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xea3(%rip),%rdi
call   1040 <perror@plt>
jmp    114b <string_from_regmatch+0x2b>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32 }
@.str = [45 x i8] c"substring from pmatch, strndup out of memory\00"
define i8* @string_from_regmatch(i8* %0, %struct.TYPE_3__* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.TYPE_3__*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %6
store i32 %2, i32* %7
%10 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%11 = load i32, i32* %7
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %10, i64 %12
%14 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %16, i64 %18
%20 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %19, i32 0, i32 1
%21 = load i32, i32* %20
%22 = sub nsw i32 %15, %21
store i32 %22, i32* %9
%23 = load i8*, i8** %5
%24 = load %struct.TYPE_3__*, %struct.TYPE_3__** %6
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %24, i64 %26
%28 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %27, i32 0, i32 1
%29 = load i32, i32* %28
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %23, i64 %30
%32 = load i32, i32* %9
%33 = add nsw i32 %32, 1
%34 = call i8* @strndup(i8* %31, i32 %33)
store i8* %34, i8** %8
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %41
36: ; preds = %3
%37 = load i8*, i8** %8
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
br label %43
41: ; preds = %3
%42 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %4
br label %45
43: ; preds = %36
%44 = load i8*, i8** %8
store i8* %44, i8** %4
br label %45
45: ; preds = %43, %41
%46 = load i8*, i8** %4
ret i8* %46
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32 }
@.str = [45 x i8] c"substring from pmatch, strndup out of memory\00"
define noalias i8* @string_from_regmatch(i8* nocapture %0, %struct.TYPE_3__* nocapture %1, i32 %2) {
%4 = sext i32 %2 to i64
%5 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 %4, i32 0
%6 = load i32, i32* %5
%7 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 %4, i32 1
%8 = load i32, i32* %7
%9 = sub nsw i32 %6, %8
%10 = sext i32 %8 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = add nsw i32 %9, 1
%13 = tail call i8* @strndup(i8* %11, i32 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = sext i32 %9 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
store i8 0, i8* %17
br label %20
18: ; preds = %3
%19 = tail call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %15, %18
ret i8* %13
}