#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*/ p1;
typedef int /*<<< orphan*/ p0;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pstrchr(char *, char, int, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, scalar_t__);
void getsubstr(char *str, char sp, int n, char *substr) {
  char p0[1024];
  char p1[1024];
  memset(p0, 0, sizeof(p0));
  memset(p1, 0, sizeof(p1));
  if (!str || n <= 0)
    return;
  if (n == 1) {
    strcpy(p0, str);
  } else {
    pstrchr(str, sp, n - 1, p0);
  }
  pstrchr(str, sp, n, p1);
  if (n == 1) {
    strncpy(substr, p0, strlen(p0) - strlen(p1));
  } else {
    strncpy(substr, p0 + 1, strlen(p0 + 1) - strlen(p1));
  }
}


x86 assembly (clang, -O0):
<getsubstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
cmpq   $0x0,-0x8(%rbp)
je     11a7 <getsubstr+0x57>
cmpl   $0x0,-0x10(%rbp)
jg     11ac <getsubstr+0x5c>
jmp    12c1 <getsubstr+0x171>
cmpl   $0x1,-0x10(%rbp)
jne    11cb <getsubstr+0x7b>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    11e7 <getsubstr+0x97>
mov    -0x8(%rbp),%rdi
mov    -0x9(%rbp),%al
mov    -0x10(%rbp),%edx
sub    $0x1,%edx
lea    -0x420(%rbp),%rcx
movsbl %al,%esi
call   1070 <pstrchr@plt>
mov    -0x8(%rbp),%rdi
mov    -0x9(%rbp),%al
mov    -0x10(%rbp),%edx
lea    -0x820(%rbp),%rcx
movsbl %al,%esi
call   1070 <pstrchr@plt>
cmpl   $0x1,-0x10(%rbp)
jne    1264 <getsubstr+0x114>
mov    -0x18(%rbp),%rax
mov    %rax,-0x830(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x828(%rbp)
lea    -0x420(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x838(%rbp)
lea    -0x820(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x838(%rbp),%rdx
mov    -0x830(%rbp),%rdi
mov    -0x828(%rbp),%rsi
sub    %rax,%rdx
call   1030 <strncpy@plt>
jmp    12c1 <getsubstr+0x171>
mov    -0x18(%rbp),%rax
mov    %rax,-0x848(%rbp)
lea    -0x420(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x840(%rbp)
lea    -0x420(%rbp),%rdi
add    $0x1,%rdi
call   1050 <strlen@plt>
mov    %rax,-0x850(%rbp)
lea    -0x820(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x850(%rbp),%rdx
mov    -0x848(%rbp),%rdi
mov    -0x840(%rbp),%rsi
sub    %rax,%rdx
call   1030 <strncpy@plt>
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getsubstr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %rcx,%r14
mov    %edx,%r12d
mov    %esi,%ebp
mov    %rdi,%r15
lea    0x400(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
test   %r15,%r15
je     121c <getsubstr+0xcc>
test   %r12d,%r12d
jle    121c <getsubstr+0xcc>
cmp    $0x1,%r12d
jne    11c5 <getsubstr+0x75>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
movsbl %bpl,%esi
mov    %rsp,%rcx
mov    %r15,%rdi
mov    $0x1,%edx
call   1070 <pstrchr@plt>
jmp    11f8 <getsubstr+0xa8>
lea    -0x1(%r12),%edx
movsbl %bpl,%ebx
lea    0x400(%rsp),%rcx
mov    %r15,%rdi
mov    %ebx,%esi
call   1070 <pstrchr@plt>
mov    %rsp,%rcx
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r12d,%edx
call   1070 <pstrchr@plt>
lea    0x401(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %rsp,%rdi
call   1050 <strlen@plt>
sub    %rax,%rbp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <strncpy@plt>
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @getsubstr(i8* %0, i8 signext %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [1024 x i8]
%10 = alloca [1024 x i8]
store i8* %0, i8** %5
store i8 %1, i8* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 1024)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i32 0, i32 1024)
%15 = load i8*, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = load i32, i32* %7
%19 = icmp sle i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17, %4
br label %63
21: ; preds = %17
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%26 = load i8*, i8** %5
%27 = call i32 @strcpy(i8* %25, i8* %26)
br label %35
28: ; preds = %21
%29 = load i8*, i8** %5
%30 = load i8, i8* %6
%31 = load i32, i32* %7
%32 = sub nsw i32 %31, 1
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%34 = call i32 @pstrchr(i8* %29, i8 signext %30, i32 %32, i8* %33)
br label %35
35: ; preds = %28, %24
%36 = load i8*, i8** %5
%37 = load i8, i8* %6
%38 = load i32, i32* %7
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%40 = call i32 @pstrchr(i8* %36, i8 signext %37, i32 %38, i8* %39)
%41 = load i32, i32* %7
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %52
43: ; preds = %35
%44 = load i8*, i8** %8
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%47 = call i64 @strlen(i8* %46)
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%49 = call i64 @strlen(i8* %48)
%50 = sub nsw i64 %47, %49
%51 = call i32 @strncpy(i8* %44, i8* %45, i64 %50)
br label %63
52: ; preds = %35
%53 = load i8*, i8** %8
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%55 = getelementptr inbounds i8, i8* %54, i64 1
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%57 = getelementptr inbounds i8, i8* %56, i64 1
%58 = call i64 @strlen(i8* %57)
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%60 = call i64 @strlen(i8* %59)
%61 = sub nsw i64 %58, %60
%62 = call i32 @strncpy(i8* %53, i8* %55, i64 %61)
br label %63
63: ; preds = %20, %52, %43
ret void
}

LLVM IR (-O2):
define void @getsubstr(i8* %0, i8 signext %1, i32 %2, i8* %3) {
%5 = alloca [1024 x i8]
%6 = alloca [1024 x i8]
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%9 = call i32 @memset(i8* %7, i32 0, i32 1024)
%10 = call i32 @memset(i8* %8, i32 0, i32 1024)
%11 = icmp eq i8* %0, null
%12 = icmp slt i32 %2, 1
%13 = or i1 %11, %12
br i1 %13, label %30, label %14
14: ; preds = %4
%15 = icmp eq i32 %2, 1
br i1 %15, label %16, label %19
16: ; preds = %14
%17 = call i32 @strcpy(i8* %7, i8* %0)
%18 = call i32 @pstrchr(i8* %0, i8 signext %1, i32 1, i8* %8)
br label %24
19: ; preds = %14
%20 = add nsw i32 %2, -1
%21 = call i32 @pstrchr(i8* %0, i8 signext %1, i32 %20, i8* %7)
%22 = call i32 @pstrchr(i8* %0, i8 signext %1, i32 %2, i8* %8)
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 1
br label %24
24: ; preds = %19, %16
%25 = phi i8* [ %7, %16 ], [ %23, %19 ]
%26 = call i64 @strlen(i8* %25)
%27 = call i64 @strlen(i8* %8)
%28 = sub nsw i64 %26, %27
%29 = call i32 @strncpy(i8* %3, i8* %25, i64 %28)
br label %30
30: ; preds = %24, %4
ret void
}