#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 */
char *str_new(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *const);
char *str_mapi(char *const s, char (*f)(unsigned int, char)) {
  char *new = str_new(strlen(s));
  for (size_t i = 0; s[i] != '\0'; i++) {
    new[i] = f(i, s[i]);
  }
  return (new);
}


x86 assembly (clang, -O0):
<str_mapi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%edi
call   1040 <str_new@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1197 <str_mapi+0x77>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %ecx,%edi
mov    -0x8(%rbp),%rcx
mov    -0x20(%rbp),%rdx
movsbl (%rcx,%rdx,1),%esi
call   *%rax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    114c <str_mapi+0x2c>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_mapi>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %eax,%edi
call   1040 <str_new@plt>
mov    %rax,%r12
mov    (%r15),%al
test   %al,%al
je     116a <str_mapi+0x4a>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movsbl %al,%esi
mov    %ebx,%edi
call   *%r14
mov    %al,(%r12,%rbx,1)
movzbl 0x1(%r15,%rbx,1),%eax
add    $0x1,%rbx
test   %al,%al
jne    1150 <str_mapi+0x30>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @str_mapi(i8* %0, i8 (i32, i8)* nocapture %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i8* @str_new(i32 %3)
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %8, %2
ret i8* %4
8: ; preds = %2, %8
%9 = phi i8 [ %16, %8 ], [ %5, %2 ]
%10 = phi i64 [ %14, %8 ], [ 0, %2 ]
%11 = trunc i64 %10 to i32
%12 = tail call signext i8 %1(i32 %11, i8 signext %9)
%13 = getelementptr inbounds i8, i8* %4, i64 %10
store i8 %12, i8* %13
%14 = add i64 %10, 1
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %7, label %8
}