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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
int fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
void wav_close(FILE **wav) {
  int rc = 0, file_length = (int)ftell(*wav), data_length = file_length - 44;
  int riff_length = file_length - 8;
  if (fseek(*wav, 44 - sizeof(int), SEEK_SET)) {
    printf("Could not seek\n");
    rc = 1;
    goto cleanup;
  }
  if (fwrite(&data_length, sizeof(data_length), 1, *wav) != 1) {
    printf("Could not write data length\n");
    rc = 1;
    goto cleanup;
  }
  if (fseek(*wav, 4, SEEK_SET)) {
    printf("Could not seek\n");
    rc = 1;
    goto cleanup;
  }
  if (fwrite(&riff_length, sizeof(riff_length), 1, *wav) != 1) {
    printf("Could not write RIFF length\n");
    rc = 1;
    goto cleanup;
  }
  if (fclose(*wav)) {
    printf("Could not close WAV\n");
    rc = 1;
    goto cleanup;
  }
cleanup:
  if (rc) {
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<wav_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sub    $0x2c,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
sub    $0x8,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    $0x28,%esi
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     11d0 <wav_close+0x70>
lea    0xe41(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0xc(%rbp)
jmp    12b5 <wav_close+0x155>
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
lea    -0x14(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
cmp    $0x1,%eax
je     120b <wav_close+0xab>
lea    0xe16(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0xc(%rbp)
jmp    12b5 <wav_close+0x155>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     1247 <wav_close+0xe7>
lea    0xdca(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0xc(%rbp)
jmp    12b5 <wav_close+0x155>
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
lea    -0x18(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
cmp    $0x1,%eax
je     1282 <wav_close+0x122>
lea    0xdbc(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0xc(%rbp)
jmp    12b5 <wav_close+0x155>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     12b0 <wav_close+0x150>
lea    0xdab(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0xc(%rbp)
jmp    12b5 <wav_close+0x155>
jmp    12b5 <wav_close+0x155>
cmpl   $0x0,-0xc(%rbp)
je     12cd <wav_close+0x16d>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wav_close>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    (%rdi),%rdi
call   1050 <ftell@plt>
lea    -0x2c(%rax),%ecx
mov    %ecx,0xc(%rsp)
add    $0xfffffff8,%eax
mov    %eax,0x8(%rsp)
mov    (%rbx),%rdi
mov    0x2e54(%rip),%r15
mov    (%r15),%edx
mov    $0x28,%esi
call   1060 <fseek@plt>
lea    0xeac(%rip),%r14
test   %rax,%rax
jne    1222 <wav_close+0xc2>
mov    (%rbx),%rcx
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
cmp    $0x1,%eax
jne    1209 <wav_close+0xa9>
mov    (%rbx),%rdi
mov    (%r15),%edx
mov    $0x4,%esi
call   1060 <fseek@plt>
test   %rax,%rax
jne    1222 <wav_close+0xc2>
mov    (%rbx),%rcx
lea    0x8(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
cmp    $0x1,%eax
jne    1212 <wav_close+0xb2>
mov    (%rbx),%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    121b <wav_close+0xbb>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe20(%rip),%r14
jmp    1222 <wav_close+0xc2>
lea    0xdfb(%rip),%r14
jmp    1222 <wav_close+0xc2>
lea    0xdde(%rip),%r14
mov    %r14,%rdi
call   1030 <puts@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [16 x i8] c"Could not seek\0A\00"
@.str.1 = [29 x i8] c"Could not write data length\0A\00"
@.str.2 = [29 x i8] c"Could not write RIFF length\0A\00"
@.str.3 = [21 x i8] c"Could not close WAV\0A\00"
@EXIT_FAILURE = global i32 0
define void @wav_close(i32** %0) {
%2 = alloca i32**
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32** %0, i32*** %2
store i32 0, i32* %3
%7 = load i32**, i32*** %2
%8 = load i32*, i32** %7
%9 = call i64 @ftell(i32* %8)
%10 = trunc i64 %9 to i32
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = sub nsw i32 %11, 44
store i32 %12, i32* %5
%13 = load i32, i32* %4
%14 = sub nsw i32 %13, 8
store i32 %14, i32* %6
%15 = load i32**, i32*** %2
%16 = load i32*, i32** %15
%17 = load i32, i32* @SEEK_SET
%18 = call i64 @fseek(i32* %16, i32 40, i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %52
22: ; preds = %1
%23 = load i32**, i32*** %2
%24 = load i32*, i32** %23
%25 = call i32 @fwrite(i32* %5, i32 4, i32 1, i32* %24)
%26 = icmp ne i32 %25, 1
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %52
29: ; preds = %22
%30 = load i32**, i32*** %2
%31 = load i32*, i32** %30
%32 = load i32, i32* @SEEK_SET
%33 = call i64 @fseek(i32* %31, i32 4, i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %52
37: ; preds = %29
%38 = load i32**, i32*** %2
%39 = load i32*, i32** %38
%40 = call i32 @fwrite(i32* %6, i32 4, i32 1, i32* %39)
%41 = icmp ne i32 %40, 1
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %52
44: ; preds = %37
%45 = load i32**, i32*** %2
%46 = load i32*, i32** %45
%47 = call i64 @fclose(i32* %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %52
51: ; preds = %44
br label %52
52: ; preds = %51, %49, %42, %35, %27, %20
%53 = load i32, i32* %3
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %52
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@EXIT_FAILURE = global i32 0
@str = [20 x i8] c"Could not close WAV\00"
@str.4 = [28 x i8] c"Could not write RIFF length\00"
@str.6 = [28 x i8] c"Could not write data length\00"
@str.7 = [15 x i8] c"Could not seek\00"
define void @wav_close(i32** nocapture %0) {
%2 = alloca i32
%3 = alloca i32
%4 = load i32*, i32** %0
%5 = tail call i64 @ftell(i32* %4)
%6 = trunc i64 %5 to i32
%7 = bitcast i32* %2 to i8*
%8 = add nsw i32 %6, -44
store i32 %8, i32* %2
%9 = bitcast i32* %3 to i8*
%10 = add nsw i32 %6, -8
store i32 %10, i32* %3
%11 = load i32*, i32** %0
%12 = load i32, i32* @SEEK_SET
%13 = tail call i64 @fseek(i32* %11, i32 40, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %32
15: ; preds = %1
%16 = load i32*, i32** %0
%17 = call i32 @fwrite(i32* %2, i32 4, i32 1, i32* %16)
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %32
19: ; preds = %15
%20 = load i32*, i32** %0
%21 = load i32, i32* @SEEK_SET
%22 = tail call i64 @fseek(i32* %20, i32 4, i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %19
%25 = load i32*, i32** %0
%26 = call i32 @fwrite(i32* %3, i32 4, i32 1, i32* %25)
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32*, i32** %0
%30 = tail call i64 @fclose(i32* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %28, %24, %19, %15, %1
%33 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @str.7, i64 0, i64 0), %1 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @str.6, i64 0, i64 0), %15 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @str.7, i64 0, i64 0), %19 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @str.4, i64 0, i64 0), %24 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0), %28 ]
%34 = tail call i32 @puts(i8* dereferenceable(1) %33)
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %28
ret void
}