#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 */
typedef int /*<<< orphan*/ uint32_t;
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ FindIconType(char *);
int /*<<< orphan*/ *OpenIcnsFileForIconset(char const *);
int /*<<< orphan*/ PrintSystemError();
int /*<<< orphan*/ WriteIcnsFileMetadata(int /*<<< orphan*/ *);
int /*<<< orphan*/ WriteIconToFile(char const *, char *, int /*<<< orphan*/,
                                   int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
bool CreateIcnsFromIconset(const char *iconset_path) {
  DIR *iconset = opendir(iconset_path);
  if (!iconset) {
    PrintSystemError();
    return false;
  }
  FILE *icns = OpenIcnsFileForIconset(iconset_path);
  if (!icns)
    return false;
  for (struct dirent *entry = readdir(iconset); entry;
       entry = readdir(iconset)) {
    if (entry->d_name[0] == '.')
      continue;
    uint32_t icon_type = FindIconType(entry->d_name);
    if (!icon_type) {
      fprintf(stderr, "Warning: Don't know icon type for %s, skipping\n",
              entry->d_name);
      continue;
    }
    if (!WriteIconToFile(iconset_path, entry->d_name, icon_type, icns)) {
      fclose(icns);
      return false;
    }
  }
  if (!WriteIcnsFileMetadata(icns)) {
    fclose(icns);
    return false;
  }
  fclose(icns);
  return true;
}


x86 assembly (clang, -O0):
<CreateIcnsFromIconset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c7 <CreateIcnsFromIconset+0x37>
mov    $0x0,%al
call   1030 <PrintSystemError@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d8 <CreateIcnsFromIconset+0x148>
mov    -0x10(%rbp),%rdi
call   10b0 <OpenIcnsFileForIconset@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11eb <CreateIcnsFromIconset+0x5b>
movl   $0x0,-0x4(%rbp)
jmp    12d8 <CreateIcnsFromIconset+0x148>
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12a1 <CreateIcnsFromIconset+0x111>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    121b <CreateIcnsFromIconset+0x8b>
jmp    128f <CreateIcnsFromIconset+0xff>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1040 <FindIconType@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1255 <CreateIcnsFromIconset+0xc5>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%rdx
lea    0xdb5(%rip),%rsi
call   1070 <fprintf@plt>
jmp    128f <CreateIcnsFromIconset+0xff>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   10a0 <WriteIconToFile@plt>
cmp    $0x0,%eax
jne    128a <CreateIcnsFromIconset+0xfa>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d8 <CreateIcnsFromIconset+0x148>
jmp    128f <CreateIcnsFromIconset+0xff>
mov    -0x18(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x28(%rbp)
jmp    11f8 <CreateIcnsFromIconset+0x68>
mov    -0x20(%rbp),%rdi
call   1080 <WriteIcnsFileMetadata@plt>
cmp    $0x0,%eax
jne    12c8 <CreateIcnsFromIconset+0x138>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d8 <CreateIcnsFromIconset+0x148>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CreateIcnsFromIconset>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
call   1060 <opendir@plt>
test   %rax,%rax
je     1227 <CreateIcnsFromIconset+0x97>
mov    %rax,%rbp
mov    %r13,%rdi
call   10b0 <OpenIcnsFileForIconset@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     124c <CreateIcnsFromIconset+0xbc>
mov    %rax,%r15
mov    %rbp,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1233 <CreateIcnsFromIconset+0xa3>
mov    %rax,%rbx
lea    0xe27(%rip),%r12
jmp    1200 <CreateIcnsFromIconset+0x70>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
mov    %r12,%rsi
call   1070 <fprintf@plt>
nop
mov    %rbp,%rdi
call   1090 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1233 <CreateIcnsFromIconset+0xa3>
mov    (%rbx),%rdi
cmpb   $0x2e,(%rdi)
je     11f0 <CreateIcnsFromIconset+0x60>
call   1040 <FindIconType@plt>
test   %eax,%eax
je     11db <CreateIcnsFromIconset+0x4b>
mov    (%rbx),%rsi
mov    %r13,%rdi
mov    %eax,%edx
mov    %r15,%rcx
call   10a0 <WriteIconToFile@plt>
test   %eax,%eax
jne    11f0 <CreateIcnsFromIconset+0x60>
jmp    1244 <CreateIcnsFromIconset+0xb4>
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <PrintSystemError@plt>
jmp    124c <CreateIcnsFromIconset+0xbc>
mov    %r15,%rdi
call   1080 <WriteIcnsFileMetadata@plt>
xor    %r14d,%r14d
test   %eax,%eax
setne  %r14b
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [48 x i8] c"Warning: Don't know icon type for %s, skipping\0A\00"
define i32 @CreateIcnsFromIconset(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %14, label %12
12: ; preds = %1
%13 = call i32 (...) @PrintSystemError()
store i32 0, i32* %2
br label %74
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i32* @OpenIcnsFileForIconset(i8* %15)
store i32* %16, i32** %5
%17 = load i32*, i32** %5
%18 = icmp ne i32* %17, null
br i1 %18, label %20, label %19
19: ; preds = %14
store i32 0, i32* %2
br label %74
20: ; preds = %14
%21 = load i32*, i32** %4
%22 = call %struct.dirent* @readdir(i32* %21)
store %struct.dirent* %22, %struct.dirent** %6
br label %23
23: ; preds = %61, %20
%24 = load %struct.dirent*, %struct.dirent** %6
%25 = icmp ne %struct.dirent* %24, null
br i1 %25, label %26, label %64
26: ; preds = %23
%27 = load %struct.dirent*, %struct.dirent** %6
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = getelementptr inbounds i8, i8* %29, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 46
br i1 %33, label %34, label %35
34: ; preds = %26
br label %61
35: ; preds = %26
%36 = load %struct.dirent*, %struct.dirent** %6
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i32 0, i32 0
%38 = load i8*, i8** %37
%39 = call i32 @FindIconType(i8* %38)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = icmp ne i32 %40, 0
br i1 %41, label %48, label %42
42: ; preds = %35
%43 = load i32, i32* @stderr
%44 = load %struct.dirent*, %struct.dirent** %6
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %44, i32 0, i32 0
%46 = load i8*, i8** %45
%47 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %46)
br label %61
48: ; preds = %35
%49 = load i8*, i8** %3
%50 = load %struct.dirent*, %struct.dirent** %6
%51 = getelementptr inbounds %struct.dirent, %struct.dirent* %50, i32 0, i32 0
%52 = load i8*, i8** %51
%53 = load i32, i32* %7
%54 = load i32*, i32** %5
%55 = call i32 @WriteIconToFile(i8* %49, i8* %52, i32 %53, i32* %54)
%56 = icmp ne i32 %55, 0
br i1 %56, label %60, label %57
57: ; preds = %48
%58 = load i32*, i32** %5
%59 = call i32 @fclose(i32* %58)
store i32 0, i32* %2
br label %74
60: ; preds = %48
br label %61
61: ; preds = %60, %42, %34
%62 = load i32*, i32** %4
%63 = call %struct.dirent* @readdir(i32* %62)
store %struct.dirent* %63, %struct.dirent** %6
br label %23
64: ; preds = %23
%65 = load i32*, i32** %5
%66 = call i32 @WriteIcnsFileMetadata(i32* %65)
%67 = icmp ne i32 %66, 0
br i1 %67, label %71, label %68
68: ; preds = %64
%69 = load i32*, i32** %5
%70 = call i32 @fclose(i32* %69)
store i32 0, i32* %2
br label %74
71: ; preds = %64
%72 = load i32*, i32** %5
%73 = call i32 @fclose(i32* %72)
store i32 1, i32* %2
br label %74
74: ; preds = %71, %68, %57, %19, %12
%75 = load i32, i32* %2
ret i32 %75
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [48 x i8] c"Warning: Don't know icon type for %s, skipping\0A\00"
define i32 @CreateIcnsFromIconset(i8* %0) {
%2 = tail call i32* @opendir(i8* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 (...) @PrintSystemError()
br label %39
6: ; preds = %1
%7 = tail call i32* @OpenIcnsFileForIconset(i8* %0)
%8 = icmp eq i32* %7, null
br i1 %8, label %39, label %9
9: ; preds = %6
%10 = tail call %struct.dirent* @readdir(i32* %2)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %34, label %12
12: ; preds = %9, %29
%13 = phi %struct.dirent* [ %30, %29 ], [ %10, %9 ]
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 46
br i1 %17, label %29, label %18
18: ; preds = %12
%19 = tail call i32 @FindIconType(i8* %15)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %14
%24 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %29
25: ; preds = %18
%26 = load i8*, i8** %14
%27 = tail call i32 @WriteIconToFile(i8* %0, i8* %26, i32 %19, i32* %7)
%28 = icmp eq i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %25, %21, %12
%30 = tail call %struct.dirent* @readdir(i32* %2)
%31 = icmp eq %struct.dirent* %30, null
br i1 %31, label %34, label %12
32: ; preds = %25
%33 = tail call i32 @fclose(i32* %7)
br label %39
34: ; preds = %29, %9
%35 = tail call i32 @WriteIcnsFileMetadata(i32* %7)
%36 = icmp ne i32 %35, 0
%37 = tail call i32 @fclose(i32* %7)
%38 = zext i1 %36 to i32
br label %39
39: ; preds = %34, %32, %6, %4
%40 = phi i32 [ 0, %4 ], [ 0, %32 ], [ 0, %6 ], [ %38, %34 ]
ret i32 %40
}