#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 */

/* Variables and functions */
int /*<<< orphan*/ fillstr(char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *substr(char *target, char *source, int pos, int len) {
  if (target == NULL)
    return NULL;
  if (source == NULL)
    target[0] = '\0';
  else {
    strncpy(target, &source[pos], len);
    fillstr(target, len);
  }
  return target;
}


x86 assembly (clang, -O0):
<substr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114e <substr+0x2e>
movq   $0x0,-0x8(%rbp)
jmp    1190 <substr+0x70>
cmpq   $0x0,-0x18(%rbp)
jne    1165 <substr+0x45>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    1188 <substr+0x68>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1040 <fillstr@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substr>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %rdi,%rdi
je     1151 <substr+0x31>
test   %rsi,%rsi
je     114e <substr+0x2e>
mov    %ecx,%ebp
movslq %edx,%rax
add    %rax,%rsi
mov    %rbx,%rdi
mov    %ecx,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <fillstr@plt>
jmp    1151 <substr+0x31>
movb   $0x0,(%rbx)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i8* @substr(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%10 = load i8*, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %4
store i8* null, i8** %5
br label %32
13: ; preds = %4
%14 = load i8*, i8** %7
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i8*, i8** %6
%18 = getelementptr inbounds i8, i8* %17, i64 0
store i8 0, i8* %18
br label %30
19: ; preds = %13
%20 = load i8*, i8** %6
%21 = load i8*, i8** %7
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i32, i32* %9
%26 = call i32 @strncpy(i8* %20, i8* %24, i32 %25)
%27 = load i8*, i8** %6
%28 = load i32, i32* %9
%29 = call i32 @fillstr(i8* %27, i32 %28)
br label %30
30: ; preds = %19, %16
%31 = load i8*, i8** %6
store i8* %31, i8** %5
br label %32
32: ; preds = %30, %12
%33 = load i8*, i8** %5
ret i8* %33
}

LLVM IR (-O2):
define i8* @substr(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = icmp eq i8* %0, null
br i1 %5, label %14, label %6
6: ; preds = %4
%7 = icmp eq i8* %1, null
br i1 %7, label %8, label %9
8: ; preds = %6
store i8 0, i8* %0
br label %14
9: ; preds = %6
%10 = sext i32 %2 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
%12 = tail call i32 @strncpy(i8* %0, i8* %11, i32 %3)
%13 = tail call i32 @fillstr(i8* %0, i32 %3)
br label %14
14: ; preds = %8, %9, %4
ret i8* %0
}