#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 */
int /*<<< orphan*/ errorcheck(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned short *, int, int, int /*<<< orphan*/ *);
unsigned short strtoul(char *, int /*<<< orphan*/ *, int);
int writewave(FILE *wavefp, FILE *samplefp) {
  unsigned int samples = 0;
  unsigned short val;
  char buf[32];
  while (fgets(buf, 30, samplefp)) {
    val = strtoul(buf, NULL, 16);
    errorcheck(fwrite(&val, 2, 1, wavefp));
    samples += 1;
  }
  fflush(wavefp);
  return samples;
}


x86 assembly (clang, -O0):
<writewave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1e,%esi
call   1030 <fgets@plt>
cmp    $0x0,%rax
je     11c5 <writewave+0x75>
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
mov    %ax,-0x16(%rbp)
mov    -0x8(%rbp),%rcx
lea    -0x16(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    %eax,%edi
call   1060 <errorcheck@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1167 <writewave+0x17>
mov    -0x8(%rbp),%rdi
call   1040 <fflush@plt>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writewave>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x10(%rsp),%rdi
mov    $0x1e,%esi
mov    %r15,%rdx
call   1030 <fgets@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11d8 <writewave+0x88>
lea    0x10(%rsp),%r12
lea    0xe(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
mov    %ax,0xe(%rsp)
mov    %r13,%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
mov    %eax,%edi
call   1060 <errorcheck@plt>
add    $0x1,%ebx
mov    %r12,%rdi
mov    $0x1e,%esi
mov    %r15,%rdx
call   1030 <fgets@plt>
test   %rax,%rax
jne    1190 <writewave+0x40>
mov    %r14,%rdi
call   1040 <fflush@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @writewave(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i16
%7 = alloca [32 x i8]
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32 0, i32* %5
br label %8
8: ; preds = %13, %2
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%10 = load i32*, i32** %4
%11 = call i64 @fgets(i8* %9, i32 30, i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %8
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%15 = call zeroext i16 @strtoul(i8* %14, i32* null, i32 16)
store i16 %15, i16* %6
%16 = load i32*, i32** %3
%17 = call i32 @fwrite(i16* %6, i32 2, i32 1, i32* %16)
%18 = call i32 @errorcheck(i32 %17)
%19 = load i32, i32* %5
%20 = add i32 %19, 1
store i32 %20, i32* %5
br label %8
21: ; preds = %8
%22 = load i32*, i32** %3
%23 = call i32 @fflush(i32* %22)
%24 = load i32, i32* %5
ret i32 %24
}

LLVM IR (-O2):
define i32 @writewave(i32* nocapture %0, i32* nocapture %1) {
%3 = alloca i16
%4 = alloca [32 x i8]
%5 = bitcast i16* %3 to i8*
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = call i64 @fgets(i8* %6, i32 30, i32* %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %17, label %9
9: ; preds = %2, %9
%10 = phi i32 [ %14, %9 ], [ 0, %2 ]
%11 = call zeroext i16 @strtoul(i8* nocapture %6, i32* null, i32 16)
store i16 %11, i16* %3
%12 = call i32 @fwrite(i16* %3, i32 2, i32 1, i32* %0)
%13 = call i32 @errorcheck(i32 %12)
%14 = add i32 %10, 1
%15 = call i64 @fgets(i8* %6, i32 30, i32* %1)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %9
17: ; preds = %9, %2
%18 = phi i32 [ 0, %2 ], [ %14, %9 ]
%19 = call i32 @fflush(i32* %0)
ret i32 %18
}