#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 *);
int /*<<< orphan*/ free_string_shares(char **, int);
char *join_strings(char **, int);
char **malloc(int);
char *strdup(char const *);
scalar_t__ strlen(char *);
char *strtok_rr(char *, char *, char **);
int /*<<< orphan*/ trim_trailing_whitespace(char *);
char *extract_secret_from_share_strings(const char *string) {
  char **shares = malloc(sizeof(char *) * 255);
  char *share;
  char *saveptr = NULL;
  int i = 0;
  char *temp_string = strdup(string);
  share = strtok_rr(temp_string, "\n", &saveptr);
  shares[i] = strdup(share);
  trim_trailing_whitespace(shares[i]);
  while ((share = strtok_rr(NULL, "\n", &saveptr))) {
    i++;
    shares[i] = strdup(share);
    trim_trailing_whitespace(shares[i]);
    if ((shares[i] != NULL) && (strlen(shares[i]) == 0)) {
      free(shares[i]);
      i--;
    }
  }
  i++;
  char *secret = join_strings(shares, i);
  free_string_shares(shares, i);
  return secret;
}


x86 assembly (clang, -O0):
<extract_secret_from_share_strings>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x7f8,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe3f(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   10a0 <strtok_rr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1050 <trim_trailing_whitespace@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdfe(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   10a0 <strtok_rr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1298 <extract_secret_from_share_strings+0x118>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1050 <trim_trailing_whitespace@plt>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1293 <extract_secret_from_share_strings+0x113>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    1293 <extract_secret_from_share_strings+0x113>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    11f7 <extract_secret_from_share_strings+0x77>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1080 <join_strings@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <free_string_shares@plt>
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_secret_from_share_strings>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0x7f8,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
movq   $0x0,(%rsp)
mov    %rbx,%rdi
call   1080 <strdup@plt>
lea    0xe5e(%rip),%rbx
mov    %rsp,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1090 <strtok_rr@plt>
mov    %rax,%rdi
call   1080 <strdup@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
call   1040 <trim_trailing_whitespace@plt>
xor    %r13d,%r13d
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1090 <strtok_rr@plt>
test   %rax,%rax
je     123b <extract_secret_from_share_strings+0xcb>
xor    %ebp,%ebp
lea    0xe1c(%rip),%r15
mov    %rsp,%r12
jmp    1205 <extract_secret_from_share_strings+0x95>
nopl   0x0(%rax)
xor    %edi,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1090 <strtok_rr@plt>
mov    %r13d,%ebp
test   %rax,%rax
je     123b <extract_secret_from_share_strings+0xcb>
lea    0x1(%rbp),%r13d
mov    %rax,%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
movslq %ebp,%rax
mov    %rbx,0x8(%r14,%rax,8)
mov    %rbx,%rdi
call   1040 <trim_trailing_whitespace@plt>
test   %rbx,%rbx
je     11f0 <extract_secret_from_share_strings+0x80>
cmpb   $0x0,(%rbx)
jne    11f0 <extract_secret_from_share_strings+0x80>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%r13d
jmp    11f0 <extract_secret_from_share_strings+0x80>
add    $0x1,%r13d
mov    %r14,%rdi
mov    %r13d,%esi
call   1070 <join_strings@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r13d,%esi
call   1060 <free_string_shares@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"\0A\00"
define i8* @extract_secret_from_share_strings(i8* nocapture %0) {
%2 = alloca i8*
%3 = tail call i8** @malloc(i32 2040)
%4 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%5 = tail call i8* @strdup(i8* %0)
%6 = call i8* @strtok_rr(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2)
%7 = call i8* @strdup(i8* %6)
store i8* %7, i8** %3
%8 = call i32 @trim_trailing_whitespace(i8* %7)
%9 = call i8* @strtok_rr(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2)
%10 = icmp eq i8* %9, null
br i1 %10, label %29, label %11
11: ; preds = %1, %25
%12 = phi i8* [ %27, %25 ], [ %9, %1 ]
%13 = phi i32 [ %26, %25 ], [ 0, %1 ]
%14 = add nsw i32 %13, 1
%15 = call i8* @strdup(i8* %12)
%16 = sext i32 %14 to i64
%17 = getelementptr inbounds i8*, i8** %3, i64 %16
store i8* %15, i8** %17
%18 = call i32 @trim_trailing_whitespace(i8* %15)
%19 = icmp eq i8* %15, null
br i1 %19, label %25, label %20
20: ; preds = %11
%21 = load i8, i8* %15
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @free(i8* %15)
br label %25
25: ; preds = %23, %20, %11
%26 = phi i32 [ %13, %23 ], [ %14, %20 ], [ %14, %11 ]
%27 = call i8* @strtok_rr(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %11
29: ; preds = %25, %1
%30 = phi i32 [ 0, %1 ], [ %26, %25 ]
%31 = add nsw i32 %30, 1
%32 = call i8* @join_strings(i8** %3, i32 %31)
%33 = call i32 @free_string_shares(i8** %3, i32 %31)
ret i8* %32
}