#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 */
void *malloc(int);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char **callback(char *str) {
  char *table[] = {"hello", "hxllo", "this is a", "this is a test", NULL};
  if (!strcmp(str, "helll"))
    table[0] = "helllo";
  int tablelen = 1;
  int i;
  for (i = 0; table[i] != NULL; i++)
    tablelen++;
  char **ret = malloc(sizeof(char *) * tablelen);
  for (i = 0; i < tablelen; i++) {
    if (table[i]) {
      ret[i] = malloc(strlen(table[i]) + 1);
      strcpy(ret[i], table[i]);
    } else {
      ret[i] = NULL;
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<callback>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
lea    0xe8d(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe88(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe82(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11bc <callback+0x6c>
lea    0xe73(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movslq -0x38(%rbp),%rax
cmpq   $0x0,-0x30(%rbp,%rax,8)
je     11f1 <callback+0xa1>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11ca <callback+0x7a>
movslq -0x34(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1290 <callback+0x140>
movslq -0x38(%rbp),%rax
cmpq   $0x0,-0x30(%rbp,%rax,8)
je     126d <callback+0x11d>
movslq -0x38(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%rdx
mov    -0x40(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x40(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
movslq -0x38(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rsi
call   1030 <strcpy@plt>
jmp    127d <callback+0x12d>
mov    -0x40(%rbp),%rax
movslq -0x38(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
jmp    1282 <callback+0x132>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    120b <callback+0xbb>
mov    -0x40(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<callback>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
movq   $0x0,0x20(%rsp)
lea    0xea3(%rip),%r15
mov    %r15,(%rsp)
lea    0xe9e(%rip),%rax
mov    %rax,0x8(%rsp)
lea    0xe98(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xe96(%rip),%rax
mov    %rax,0x18(%rsp)
lea    0xe99(%rip),%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
jne    11a0 <callback+0x60>
lea    0xe8f(%rip),%r15
mov    %r15,(%rsp)
mov    $0x1,%ebx
mov    $0x8,%edi
nopw   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%rbx
cmpq   $0x0,(%rsp,%rdi,1)
lea    0x8(%rdi),%rdi
jne    11b0 <callback+0x70>
call   1060 <malloc@plt>
mov    %rax,%r14
neg    %rbx
mov    $0xffffffffffffffff,%r13
test   %r15,%r15
je     1200 <callback+0xc0>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
jmp    1203 <callback+0xc3>
xor    %r12d,%r12d
mov    %r12,0x8(%r14,%r13,8)
cmp    %r13,%rbx
je     121d <callback+0xdd>
mov    0x10(%rsp,%r13,8),%r15
add    $0x1,%r13
test   %r15,%r15
jne    11e0 <callback+0xa0>
jmp    1200 <callback+0xc0>
mov    %r14,%rax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"hello\00"
@.str.1 = [6 x i8] c"hxllo\00"
@.str.2 = [10 x i8] c"this is a\00"
@.str.3 = [15 x i8] c"this is a test\00"
@.str.4 = [6 x i8] c"helll\00"
@.str.5 = [7 x i8] c"helllo\00"
define i8** @callback(i8* %0) {
%2 = alloca i8*
%3 = alloca [5 x i8*]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
store i8* %0, i8** %2
%7 = bitcast [5 x i8*]* %3 to i8*
%8 = bitcast i8* %7 to [5 x i8*]*
%9 = getelementptr inbounds [5 x i8*], [5 x i8*]* %8, i32 0, i32 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i32 0, i32 0), i8** %9
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %8, i32 0, i32 1
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8** %10
%11 = getelementptr inbounds [5 x i8*], [5 x i8*]* %8, i32 0, i32 2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i32 0, i32 0), i8** %11
%12 = getelementptr inbounds [5 x i8*], [5 x i8*]* %8, i32 0, i32 3
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i32 0, i32 0), i8** %12
%13 = load i8*, i8** %2
%14 = call i32 @strcmp(i8* %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%15 = icmp ne i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %1
%17 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8** %17
br label %18
18: ; preds = %16, %1
store i32 1, i32* %4
store i32 0, i32* %5
br label %19
19: ; preds = %28, %18
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 %21
%23 = load i8*, i8** %22
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %31
25: ; preds = %19
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
br label %28
28: ; preds = %25
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
store i32 %30, i32* %5
br label %19
31: ; preds = %19
%32 = load i32, i32* %4
%33 = sext i32 %32 to i64
%34 = mul i64 8, %33
%35 = trunc i64 %34 to i32
%36 = call i8* @malloc(i32 %35)
%37 = bitcast i8* %36 to i8**
store i8** %37, i8*** %6
store i32 0, i32* %5
br label %38
38: ; preds = %76, %31
%39 = load i32, i32* %5
%40 = load i32, i32* %4
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %79
42: ; preds = %38
%43 = load i32, i32* %5
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 %44
%46 = load i8*, i8** %45
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %70
48: ; preds = %42
%49 = load i32, i32* %5
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 %50
%52 = load i8*, i8** %51
%53 = call i32 @strlen(i8* %52)
%54 = add nsw i32 %53, 1
%55 = call i8* @malloc(i32 %54)
%56 = load i8**, i8*** %6
%57 = load i32, i32* %5
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8*, i8** %56, i64 %58
store i8* %55, i8** %59
%60 = load i8**, i8*** %6
%61 = load i32, i32* %5
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8*, i8** %60, i64 %62
%64 = load i8*, i8** %63
%65 = load i32, i32* %5
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds [5 x i8*], [5 x i8*]* %3, i64 0, i64 %66
%68 = load i8*, i8** %67
%69 = call i32 @strcpy(i8* %64, i8* %68)
br label %75
70: ; preds = %42
%71 = load i8**, i8*** %6
%72 = load i32, i32* %5
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8*, i8** %71, i64 %73
store i8* null, i8** %74
br label %75
75: ; preds = %70, %48
br label %76
76: ; preds = %75
%77 = load i32, i32* %5
%78 = add nsw i32 %77, 1
store i32 %78, i32* %5
br label %38
79: ; preds = %38
%80 = load i8**, i8*** %6
ret i8** %80
}

LLVM IR (-O2):
@.str = [6 x i8] c"hello\00"
@.str.1 = [6 x i8] c"hxllo\00"
@.str.2 = [10 x i8] c"this is a\00"
@.str.3 = [15 x i8] c"this is a test\00"
@.str.4 = [6 x i8] c"helll\00"
@.str.5 = [7 x i8] c"helllo\00"
define noalias i8** @callback(i8* nocapture %0) {
%2 = alloca [5 x i8*]
%3 = bitcast [5 x i8*]* %2 to i8*
%4 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 4
%5 = bitcast i8** %4 to i64*
store i64 0, i64* %5
%6 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %6
%7 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 1
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 2
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8** %8
%9 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 3
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8** %9
%10 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8** %6
br label %13
13: ; preds = %1, %12
%14 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), %12 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %1 ]
br label %15
15: ; preds = %15, %13
%16 = phi i64 [ 0, %13 ], [ %19, %15 ]
%17 = phi i32 [ 1, %13 ], [ %18, %15 ]
%18 = add nuw nsw i32 %17, 1
%19 = add nuw nsw i64 %16, 1
%20 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 %19
%21 = load i8*, i8** %20
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %15
23: ; preds = %15
%24 = shl i32 %18, 3
%25 = tail call i8* @malloc(i32 %24)
%26 = bitcast i8* %25 to i8**
%27 = zext i32 %18 to i64
br label %28
28: ; preds = %42, %23
%29 = phi i8* [ %14, %23 ], [ %44, %42 ]
%30 = phi i64 [ 0, %23 ], [ %40, %42 ]
%31 = icmp eq i8* %29, null
br i1 %31, label %37, label %32
32: ; preds = %28
%33 = tail call i32 @strlen(i8* %29)
%34 = add nsw i32 %33, 1
%35 = tail call i8* @malloc(i32 %34)
%36 = tail call i32 @strcpy(i8* %35, i8* %29)
br label %37
37: ; preds = %28, %32
%38 = phi i8* [ %35, %32 ], [ null, %28 ]
%39 = getelementptr inbounds i8*, i8** %26, i64 %30
store i8* %38, i8** %39
%40 = add nuw nsw i64 %30, 1
%41 = icmp eq i64 %40, %27
br i1 %41, label %45, label %42
42: ; preds = %37
%43 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 %40
%44 = load i8*, i8** %43
br label %28
45: ; preds = %37
ret i8** %26
}