#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strtok(char *, char *);
void flip_authors_names(char *flipped_names, const char *authors_names) {
  char *tmpstr = malloc(255 * sizeof(char));
  char *tmpLastname = malloc(255 * sizeof(char));
  strcpy(tmpstr, authors_names);
  flipped_names[0] = '\0';
  char *token = strtok(tmpstr, ",");
  while (token != NULL) {
    strcpy(tmpLastname, token);
    token = strtok(NULL, "., ");
    while (token != NULL && strcmp(token, "and") != 0) {
      strcat(flipped_names, token);
      strcat(flipped_names, ". ");
      token = strtok(NULL, ". ");
    }
    strcat(flipped_names, tmpLastname);
    if (token != NULL) {
      strcat(flipped_names, ", ");
    }
    token = strtok(NULL, ",");
  }
  free(tmpstr);
  free(tmpLastname);
}


x86 assembly (clang, -O0):
<flip_authors_names>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0xff,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0xff,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1291 <flip_authors_names+0x131>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe2b(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x29(%rbp)
je     120a <flip_authors_names+0xaa>
mov    -0x28(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    121a <flip_authors_names+0xba>
jmp    1250 <flip_authors_names+0xf0>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x8(%rbp),%rdi
lea    0xdd8(%rip),%rsi
call   1080 <strcat@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdc8(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    11e0 <flip_authors_names+0x80>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <strcat@plt>
cmpq   $0x0,-0x28(%rbp)
je     1278 <flip_authors_names+0x118>
mov    -0x8(%rbp),%rdi
lea    0xd90(%rip),%rsi
call   1080 <strcat@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd7d(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    11b4 <flip_authors_names+0x54>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flip_authors_names>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0xff,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    $0xff,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rbp,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
movb   $0x0,(%rbx)
lea    0xe60(%rip),%rsi
mov    %rbp,(%rsp)
mov    %rbp,%rdi
call   1070 <strtok@plt>
test   %rax,%rax
je     125d <flip_authors_names+0xfd>
lea    0xe44(%rip),%r13
lea    0xe43(%rip),%rbp
lea    0xe40(%rip),%r14
jmp    11f6 <flip_authors_names+0x96>
nopl   0x0(%rax)
mov    %r15,%rsi
call   1080 <strcat@plt>
lea    0xe24(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   1070 <strtok@plt>
test   %rax,%rax
je     125d <flip_authors_names+0xfd>
mov    %r15,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
xor    %edi,%edi
lea    0xdf8(%rip),%rsi
call   1070 <strtok@plt>
mov    %r15,%rsi
test   %rax,%rax
je     11df <flip_authors_names+0x7f>
mov    %rax,%r12
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %rbp,%rsi
call   1050 <strcmp@plt>
mov    %rbx,%rdi
test   %rax,%rax
je     11d0 <flip_authors_names+0x70>
mov    %r12,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
xor    %edi,%edi
mov    %r14,%rsi
call   1070 <strtok@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1220 <flip_authors_names+0xc0>
mov    %r15,%rsi
jmp    11df <flip_authors_names+0x7f>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [2 x i8] c",\00"
@.str.1 = [4 x i8] c"., \00"
@.str.2 = [4 x i8] c"and\00"
@.str.3 = [3 x i8] c". \00"
@.str.4 = [3 x i8] c", \00"
define void @flip_authors_names(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = call i8* @malloc(i32 255)
store i8* %8, i8** %5
%9 = call i8* @malloc(i32 255)
store i8* %9, i8** %6
%10 = load i8*, i8** %5
%11 = load i8*, i8** %4
%12 = call i32 @strcpy(i8* %10, i8* %11)
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i64 0
store i8 0, i8* %14
%15 = load i8*, i8** %5
%16 = call i8* @strtok(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %7
br label %17
17: ; preds = %50, %2
%18 = load i8*, i8** %7
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %52
20: ; preds = %17
%21 = load i8*, i8** %6
%22 = load i8*, i8** %7
%23 = call i32 @strcpy(i8* %21, i8* %22)
%24 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i8* %24, i8** %7
br label %25
25: ; preds = %34, %20
%26 = load i8*, i8** %7
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load i8*, i8** %7
%30 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%31 = icmp ne i64 %30, 0
br label %32
32: ; preds = %28, %25
%33 = phi i1 [ false, %25 ], [ %31, %28 ]
br i1 %33, label %34, label %41
34: ; preds = %32
%35 = load i8*, i8** %3
%36 = load i8*, i8** %7
%37 = call i32 @strcat(i8* %35, i8* %36)
%38 = load i8*, i8** %3
%39 = call i32 @strcat(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%40 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i8* %40, i8** %7
br label %25
41: ; preds = %32
%42 = load i8*, i8** %3
%43 = load i8*, i8** %6
%44 = call i32 @strcat(i8* %42, i8* %43)
%45 = load i8*, i8** %7
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %50
47: ; preds = %41
%48 = load i8*, i8** %3
%49 = call i32 @strcat(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %47, %41
%51 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %51, i8** %7
br label %17
52: ; preds = %17
%53 = load i8*, i8** %5
%54 = call i32 @free(i8* %53)
%55 = load i8*, i8** %6
%56 = call i32 @free(i8* %55)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c",\00"
@.str.1 = [4 x i8] c"., \00"
@.str.2 = [4 x i8] c"and\00"
@.str.3 = [3 x i8] c". \00"
@.str.4 = [3 x i8] c", \00"
define void @flip_authors_names(i8* %0, i8* %1) {
%3 = tail call dereferenceable_or_null(255) i8* @malloc(i32 255)
%4 = tail call dereferenceable_or_null(255) i8* @malloc(i32 255)
%5 = tail call i32 @strcpy(i8* %3, i8* %1)
store i8 0, i8* %0
%6 = tail call i8* @strtok(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %29, label %8
8: ; preds = %2, %24
%9 = phi i8* [ %27, %24 ], [ %6, %2 ]
%10 = tail call i32 @strcpy(i8* %4, i8* %9)
%11 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %24, label %13
13: ; preds = %8, %17
%14 = phi i8* [ %20, %17 ], [ %11, %8 ]
%15 = tail call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %13
%18 = tail call i32 @strcat(i8* %0, i8* %14)
%19 = tail call i32 @strcat(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%20 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%21 = icmp eq i8* %20, null
br i1 %21, label %24, label %13
22: ; preds = %13
%23 = tail call i32 @strcat(i8* %0, i8* %4)
br label %24
24: ; preds = %17, %8, %22
%25 = phi i8* [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), %22 ], [ %4, %8 ], [ %4, %17 ]
%26 = tail call i32 @strcat(i8* %0, i8* %25)
%27 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %8
29: ; preds = %24, %2
%30 = tail call i32 @free(i8* %3)
%31 = tail call i32 @free(i8* %4)
ret void
}