#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 *getAppListPath();
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
char *strcat(char *, char *);
scalar_t__ strlen(char *);
int writeTextFile(char *, char *);
int writeAppList(char **list) {
  int i = -1;
  int fileSize = 1;
  while (list[++i])
    fileSize += strlen(list[i]) + 1;
  char *appListStr = malloc(fileSize);
  appListStr[0] = '\0';
  i = -1;
  while (list[++i])
    strcat(strcat(appListStr, list[i]), "\n");
  char *appListPath = getAppListPath();
  int result = writeTextFile(appListPath, appListStr);
  if (!result)
    printf("Failed to write %s\n", appListPath);
  free(appListPath);
  free(appListStr);
  return result;
}


x86 assembly (clang, -O0):
<writeAppList>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0xc(%rbp)
movslq %ecx,%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11cc <writeAppList+0x5c>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
add    $0x1,%rcx
movslq -0x10(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x10(%rbp)
jmp    118a <writeAppList+0x1a>
mov    -0x10(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0xffffffff,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0xc(%rbp)
movslq %ecx,%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     122a <writeAppList+0xba>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1090 <strcat@plt>
mov    %rax,%rdi
lea    0xdf2(%rip),%rsi
call   1090 <strcat@plt>
jmp    11e6 <writeAppList+0x76>
mov    $0x0,%al
call   1060 <getAppListPath@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <writeTextFile@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    125f <writeAppList+0xef>
mov    -0x20(%rbp),%rsi
lea    0xda6(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeAppList>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    (%rdi),%r15
test   %r15,%r15
je     11e9 <writeAppList+0x79>
mov    %rdi,%r12
lea    0x8(%rdi),%rbp
mov    $0x1,%ebx
mov    %r15,%rdi
nop
call   1040 <strlen@plt>
add    %eax,%ebx
add    $0x1,%ebx
mov    0x0(%rbp),%rdi
add    $0x8,%rbp
test   %rdi,%rdi
jne    1190 <writeAppList+0x20>
mov    %ebx,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
movb   $0x0,(%rax)
test   %r15,%r15
je     11f9 <writeAppList+0x89>
add    $0x8,%r12
nopl   (%rax)
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <strcat@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
movw   $0xa,(%r14,%rax,1)
mov    (%r12),%r15
add    $0x8,%r12
test   %r15,%r15
jne    11c0 <writeAppList+0x50>
jmp    11f9 <writeAppList+0x89>
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
movb   $0x0,(%rax)
xor    %eax,%eax
call   1060 <getAppListPath@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1070 <writeTextFile@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1223 <writeAppList+0xb3>
lea    0xde5(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str.1 = [20 x i8] c"Failed to write %s\0A\00"
define i32 @writeAppList(i8** nocapture %0) {
%2 = load i8*, i8** %0
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
store i8 0, i8* %5
br label %31
6: ; preds = %1, %6
%7 = phi i64 [ %14, %6 ], [ 0, %1 ]
%8 = phi i8* [ %16, %6 ], [ %2, %1 ]
%9 = phi i32 [ %13, %6 ], [ 1, %1 ]
%10 = tail call i64 @strlen(i8* %8)
%11 = trunc i64 %10 to i32
%12 = add i32 %9, 1
%13 = add i32 %12, %11
%14 = add nuw nsw i64 %7, 1
%15 = getelementptr inbounds i8*, i8** %0, i64 %14
%16 = load i8*, i8** %15
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %6
18: ; preds = %6
%19 = tail call i8* @malloc(i32 %13)
store i8 0, i8* %19
br i1 %3, label %31, label %20
20: ; preds = %18, %20
%21 = phi i64 [ %27, %20 ], [ 0, %18 ]
%22 = phi i8* [ %29, %20 ], [ %2, %18 ]
%23 = tail call i8* @strcat(i8* %19, i8* %22)
%24 = tail call i64 @strlen(i8* %19)
%25 = getelementptr i8, i8* %19, i64 %24
%26 = bitcast i8* %25 to i16*
store i16 10, i16* %26
%27 = add nuw nsw i64 %21, 1
%28 = getelementptr inbounds i8*, i8** %0, i64 %27
%29 = load i8*, i8** %28
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %20
31: ; preds = %20, %4, %18
%32 = phi i8* [ %5, %4 ], [ %19, %18 ], [ %19, %20 ]
%33 = tail call i8* (...) @getAppListPath()
%34 = tail call i32 @writeTextFile(i8* %33, i8* %32)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %33)
br label %38
38: ; preds = %36, %31
%39 = tail call i32 @free(i8* %33)
%40 = tail call i32 @free(i8* %32)
ret i32 %34
}