#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*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, int, char *);
int strlen(char *);
int extract_name(char *fname, char *name) {
  int len = strlen(fname);
  int i;
  int dot_idx = -1;
  for (i = len - 1; i > 0; i--) {
    if (fname[i] == '.') {
      dot_idx = i;
      break;
    }
  }
  if (dot_idx > 0) {
    memcpy(&name[0], &fname[0], dot_idx);
    name[dot_idx] = 0;
    printf("found idx%d => %s \n", dot_idx, name);
    return 1;
  } else {
    name[0] = 0;
    return 0;
  }
}


x86 assembly (clang, -O0):
<extract_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
movl   $0xffffffff,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    1199 <extract_name+0x69>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
jne    1186 <extract_name+0x56>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1199 <extract_name+0x69>
jmp    118b <extract_name+0x5b>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    115c <extract_name+0x2c>
cmpl   $0x0,-0x24(%rbp)
jle    11de <extract_name+0xae>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xe33(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11ec <extract_name+0xbc>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_name>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %eax,%ecx
add    $0xffffffff,%eax
lea    (%rax,%r14,1),%rbx
add    $0x1,%rbx
cmp    $0x2,%ecx
jl     1192 <extract_name+0x62>
add    $0xffffffffffffffff,%rbx
lea    -0x1(%rcx),%r12
cmpb   $0x2e,-0x1(%r15,%rcx,1)
mov    %r12,%rcx
jne    1150 <extract_name+0x20>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
movb   $0x0,(%rbx)
lea    0xe80(%rip),%rdi
mov    %r12d,%esi
mov    %r14,%rdx
call   1040 <printf@plt>
mov    $0x1,%eax
jmp    1198 <extract_name+0x68>
movb   $0x0,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [20 x i8] c"found idx%d => %s \0A\00"
define i32 @extract_name(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %6
store i32 -1, i32* %8
%11 = load i32, i32* %6
%12 = sub nsw i32 %11, 1
store i32 %12, i32* %7
br label %13
13: ; preds = %27, %2
%14 = load i32, i32* %7
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %13
%17 = load i8*, i8** %4
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 46
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = load i32, i32* %7
store i32 %25, i32* %8
br label %30
26: ; preds = %16
br label %27
27: ; preds = %26
%28 = load i32, i32* %7
%29 = add nsw i32 %28, -1
store i32 %29, i32* %7
br label %13
30: ; preds = %24, %13
%31 = load i32, i32* %8
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %30
%34 = load i8*, i8** %5
%35 = getelementptr inbounds i8, i8* %34, i64 0
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i64 0
%38 = load i32, i32* %8
%39 = call i32 @memcpy(i8* %35, i8* %37, i32 %38)
%40 = load i8*, i8** %5
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
%44 = load i32, i32* %8
%45 = load i8*, i8** %5
%46 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %44, i8* %45)
store i32 1, i32* %3
br label %50
47: ; preds = %30
%48 = load i8*, i8** %5
%49 = getelementptr inbounds i8, i8* %48, i64 0
store i8 0, i8* %49
store i32 0, i32* %3
br label %50
50: ; preds = %47, %33
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str = [20 x i8] c"found idx%d => %s \0A\00"
define i32 @extract_name(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = zext i32 %3 to i64
br label %5
5: ; preds = %10, %2
%6 = phi i64 [ %7, %10 ], [ %4, %2 ]
%7 = add nsw i64 %6, -1
%8 = trunc i64 %6 to i32
%9 = icmp sgt i32 %8, 1
br i1 %9, label %10, label %20
10: ; preds = %5
%11 = getelementptr inbounds i8, i8* %0, i64 %7
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 46
br i1 %13, label %14, label %5
14: ; preds = %10
%15 = trunc i64 %7 to i32
%16 = tail call i32 @memcpy(i8* %1, i8* %0, i32 %15)
%17 = and i64 %7, 4294967295
%18 = getelementptr inbounds i8, i8* %1, i64 %17
store i8 0, i8* %18
%19 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %15, i8* %1)
br label %21
20: ; preds = %5
store i8 0, i8* %1
br label %21
21: ; preds = %20, %14
%22 = phi i32 [ 1, %14 ], [ 0, %20 ]
ret i32 %22
}