#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*/ file_path;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *basename(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char *);
char *write_data_until_next_section(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
void extract_rich_core(FILE *input_file, const char *output_dir) {
  FILE *output_file = fopen("/dev/null", "w");
  char *next_section;
  while (
      (next_section = write_data_until_next_section(input_file, output_file))) {
    fclose(output_file);
    char *file_name = basename(next_section);
    char file_path[128];
    snprintf(file_path, sizeof(file_path), "%s/%s", output_dir, file_name);
    output_file = fopen(file_path, "w");
    free(next_section);
  }
  fclose(output_file);
}


x86 assembly (clang, -O0):
<extract_rich_core>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe86(%rip),%rdi
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <write_data_until_next_section@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1200 <extract_rich_core+0xa0>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1060 <basename@plt>
mov    %rax,-0x28(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    $0x80,%esi
lea    0xe36(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0xb0(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    118a <extract_rich_core+0x2a>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_rich_core>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xe82(%rip),%rdi
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %rax,%rsi
call   1080 <write_data_until_next_section@plt>
test   %rax,%rax
je     1203 <extract_rich_core+0xa3>
mov    %rax,%r15
mov    %rsp,%r13
lea    0xe60(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1060 <basename@plt>
mov    %r13,%rdi
mov    $0x80,%esi
lea    0xe3d(%rip),%rdx
mov    %r14,%rcx
mov    %rax,%r8
call   1050 <snprintf@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1080 <write_data_until_next_section@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11b0 <extract_rich_core+0x50>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define void @extract_rich_core(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [128 x i8]
store i32* %0, i32** %3
store i8* %1, i8** %4
%9 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %5
br label %10
10: ; preds = %15, %2
%11 = load i32*, i32** %3
%12 = load i32*, i32** %5
%13 = call i8* @write_data_until_next_section(i32* %11, i32* %12)
store i8* %13, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %28
15: ; preds = %10
%16 = load i32*, i32** %5
%17 = call i32 @fclose(i32* %16)
%18 = load i8*, i8** %6
%19 = call i8* @basename(i8* %18)
store i8* %19, i8** %7
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%21 = load i8*, i8** %4
%22 = load i8*, i8** %7
%23 = call i32 @snprintf(i8* %20, i32 128, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %21, i8* %22)
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %5
%26 = load i8*, i8** %6
%27 = call i32 @free(i8* %26)
br label %10
28: ; preds = %10
%29 = load i32*, i32** %5
%30 = call i32 @fclose(i32* %29)
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"%s/%s\00"
define void @extract_rich_core(i32* %0, i8* %1) {
%3 = alloca [128 x i8]
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = call i8* @write_data_until_next_section(i32* %0, i32* %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %19, label %7
7: ; preds = %2
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
br label %9
9: ; preds = %7, %9
%10 = phi i8* [ %5, %7 ], [ %17, %9 ]
%11 = phi i32* [ %4, %7 ], [ %15, %9 ]
%12 = call i32 @fclose(i32* %11)
%13 = call i8* @basename(i8* %10)
%14 = call i32 @snprintf(i8* %8, i32 128, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %13)
%15 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @free(i8* %10)
%17 = call i8* @write_data_until_next_section(i32* %0, i32* %15)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %9
19: ; preds = %9, %2
%20 = phi i32* [ %4, %2 ], [ %15, %9 ]
%21 = call i32 @fclose(i32* %20)
ret void
}