#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;

/* Variables and functions */
scalar_t__ dump(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ stderr;
int create_and_dump(FILE *infile, const char *filename, int offset, int size) {
  FILE *outfile = fopen(filename, "wb");
  if (!outfile) {
    fprintf(stderr, "error opening %s for writing\n", filename);
    return 1;
  }
  if (dump(infile, outfile, offset, size)) {
    fprintf(stderr, "error dumping to %s\n", filename);
    fclose(outfile);
    return 1;
  }
  fclose(outfile);
  return 0;
}


x86 assembly (clang, -O0):
<create_and_dump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119a <create_and_dump+0x5a>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe7a(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f5 <create_and_dump+0xb5>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1040 <dump@plt>
cmp    $0x0,%rax
je     11e5 <create_and_dump+0xa5>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe56(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f5 <create_and_dump+0xb5>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_and_dump>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r15d
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0xea5(%rip),%rsi
mov    %r14,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a8 <create_and_dump+0x68>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %rax,%rsi
mov    %r12d,%edx
mov    %r15d,%ecx
call   1040 <dump@plt>
test   %rax,%rax
je     11c7 <create_and_dump+0x87>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0x1,%eax
jmp    11d1 <create_and_dump+0x91>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%eax
jmp    11d1 <create_and_dump+0x91>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"error opening %s for writing\0A\00"
@.str.2 = [21 x i8] c"error dumping to %s\0A\00"
define i32 @create_and_dump(i32* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
store i32* %0, i32** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i8*, i8** %7
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %10
%13 = load i32*, i32** %10
%14 = icmp ne i32* %13, null
br i1 %14, label %19, label %15
15: ; preds = %4
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %7
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i32 1, i32* %5
br label %35
19: ; preds = %4
%20 = load i32*, i32** %6
%21 = load i32*, i32** %10
%22 = load i32, i32* %8
%23 = load i32, i32* %9
%24 = call i64 @dump(i32* %20, i32* %21, i32 %22, i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = load i8*, i8** %7
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = load i32*, i32** %10
%31 = call i32 @fclose(i32* %30)
store i32 1, i32* %5
br label %35
32: ; preds = %19
%33 = load i32*, i32** %10
%34 = call i32 @fclose(i32* %33)
store i32 0, i32* %5
br label %35
35: ; preds = %32, %26, %15
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"error opening %s for writing\0A\00"
@.str.2 = [21 x i8] c"error dumping to %s\0A\00"
define i32 @create_and_dump(i32* %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %19
10: ; preds = %4
%11 = tail call i64 @dump(i32* %0, i32* %5, i32 %2, i32 %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%16 = tail call i32 @fclose(i32* %5)
br label %19
17: ; preds = %10
%18 = tail call i32 @fclose(i32* %5)
br label %19
19: ; preds = %17, %13, %7
%20 = phi i32 [ 1, %13 ], [ 0, %17 ], [ 1, %7 ]
ret i32 %20
}