#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*/ Error(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *strrchr(char *, char);
void InvertName(char result[], char name[]) {
  int len;
  char *sptr;
  len = strlen(name);
  sptr = strrchr(name, ' ');
  if (sptr != NULL)
    len++;
  if (len > 40)
    Error("Name too long");
  if (sptr == NULL) {
    strcpy(result, name);
  } else {
    strcpy(result, sptr + 1);
    strcat(result, ", ");
    strncat(result, name, sptr - name);
    result[len] = '\0';
  }
}


x86 assembly (clang, -O0):
<InvertName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x20,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a2 <InvertName+0x42>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x28,-0x14(%rbp)
jle    11b8 <InvertName+0x58>
lea    0xe4d(%rip),%rdi
call   1050 <Error@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11d5 <InvertName+0x75>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    121c <InvertName+0xbc>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rdi
lea    0xe1d(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1070 <strncat@plt>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<InvertName>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
mov    $0x20,%esi
call   1060 <strrchr@plt>
mov    %rax,%rbx
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
cmp    $0x29,%ebp
jl     119f <InvertName+0x3f>
lea    0xe66(%rip),%rdi
call   1050 <Error@plt>
test   %rbx,%rbx
je     11e5 <InvertName+0x85>
mov    %rbx,%rsi
add    $0x1,%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
lea    0xe54(%rip),%rsi
mov    %r14,%rdi
call   1080 <strcat@plt>
sub    %r15d,%ebx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1070 <strncat@plt>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <strcpy@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"Name too long\00"
@.str.1 = [3 x i8] c", \00"
define void @InvertName(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %5
%9 = load i8*, i8** %4
%10 = call i8* @strrchr(i8* %9, i8 signext 32)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* %5
%15 = add nsw i32 %14, 1
store i32 %15, i32* %5
br label %16
16: ; preds = %13, %2
%17 = load i32, i32* %5
%18 = icmp sgt i32 %17, 40
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @Error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = load i8*, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i8*, i8** %3
%26 = load i8*, i8** %4
%27 = call i32 @strcpy(i8* %25, i8* %26)
br label %48
28: ; preds = %21
%29 = load i8*, i8** %3
%30 = load i8*, i8** %6
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = call i32 @strcpy(i8* %29, i8* %31)
%33 = load i8*, i8** %3
%34 = call i32 @strcat(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%35 = load i8*, i8** %3
%36 = load i8*, i8** %4
%37 = load i8*, i8** %6
%38 = load i8*, i8** %4
%39 = ptrtoint i8* %37 to i64
%40 = ptrtoint i8* %38 to i64
%41 = sub i64 %39, %40
%42 = trunc i64 %41 to i32
%43 = call i32 @strncat(i8* %35, i8* %36, i32 %42)
%44 = load i8*, i8** %3
%45 = load i32, i32* %5
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
br label %48
48: ; preds = %28, %24
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"Name too long\00"
@.str.1 = [3 x i8] c", \00"
define void @InvertName(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %1, i8 signext 32)
%5 = icmp eq i8* %4, null
%6 = xor i1 %5, true
%7 = zext i1 %6 to i32
%8 = add nsw i32 %3, %7
%9 = icmp sgt i32 %8, 40
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @Error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
br i1 %5, label %13, label %15
13: ; preds = %12
%14 = tail call i32 @strcpy(i8* %0, i8* %1)
br label %26
15: ; preds = %12
%16 = getelementptr inbounds i8, i8* %4, i64 1
%17 = tail call i32 @strcpy(i8* %0, i8* %16)
%18 = tail call i32 @strcat(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%19 = ptrtoint i8* %4 to i64
%20 = ptrtoint i8* %1 to i64
%21 = sub i64 %19, %20
%22 = trunc i64 %21 to i32
%23 = tail call i32 @strncat(i8* %0, i8* %1, i32 %22)
%24 = sext i32 %8 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %15, %13
ret void
}