#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 */

/* Variables and functions */
int creat(char *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int);
char linear2ulaw(int);
short *map(char *, int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wr(int, char *, int);
main(int argc, char *argv[]) {
  int fd;
  short *sp;
  int size;
  int i;
  struct au_header {
    int magic;
    int hdr_size;
    int data_size;
    int encoding;
    int sample_rate;
    int channels;
  } au_header = {0x2e736e64, 24, 0, 1, 8000, 1};
  argc--;
  argv++;
  if (argc != 2) {
    printf("usage: <linear input file> <ulaw output file>\n");
    exit(1);
  }
  sp = map(argv[0], &size);
  sp = (short *)((int)sp + sizeof(struct au_header));
  size -= sizeof(struct au_header);
  if ((fd = creat(argv[1], 0666)) < 0)
    error("creat");
  au_header.data_size = size / 2;
  wr(fd, (char *)&au_header, sizeof(struct au_header));
  for (i = 0; i < size; i += 2) {
    char c;
    c = linear2ulaw((int)(*sp++ & 0xffff));
    wr(fd, (char *)&c, 1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe73(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe70(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe6d(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11dc <main+0x6c>
lea    0xe4b(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x24(%rbp),%rsi
call   1050 <map@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cltq
add    $0x18,%rax
mov    %rax,-0x20(%rbp)
movslq -0x24(%rbp),%rax
sub    $0x18,%rax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x1b6,%esi
call   1080 <creat@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1233 <main+0xc3>
lea    0xe19(%rip),%rdi
call   1070 <error@plt>
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x18,%edx
call   1060 <wr@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12a4 <main+0x134>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x2,%rcx
mov    %rcx,-0x20(%rbp)
movswl (%rax),%edi
and    $0xffff,%edi
call   1040 <linear2ulaw@plt>
mov    %al,-0x41(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x41(%rbp),%rsi
mov    $0x1,%edx
call   1060 <wr@plt>
mov    -0x28(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x28(%rbp)
jmp    1259 <main+0xe9>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0xe8f(%rip),%rax
mov    %rax,0x20(%rsp)
movups 0xe73(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
cmp    $0x3,%edi
jne    1245 <main+0xd5>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xc(%rsp),%rsi
call   1050 <map@plt>
mov    %rax,%r14
addl   $0xffffffe8,0xc(%rsp)
mov    0x10(%rbx),%rdi
mov    $0x1b6,%esi
call   1080 <creat@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11d5 <main+0x65>
lea    0xe48(%rip),%rdi
call   1070 <error@plt>
mov    0xc(%rsp),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
mov    %ecx,0x18(%rsp)
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x18,%edx
call   1060 <wr@plt>
cmpl   $0x0,0xc(%rsp)
jle    1238 <main+0xc8>
movslq %r14d,%rbx
xor    %ebp,%ebp
lea    0xb(%rsp),%r14
nopl   0x0(%rax)
movzwl 0x18(%rbx,%rbp,1),%edi
call   1040 <linear2ulaw@plt>
mov    %al,0xb(%rsp)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1060 <wr@plt>
add    $0x2,%rbp
cmp    0xc(%rsp),%ebp
jl     1210 <main+0xa0>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd2(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.au_header = type { i32, i32, i32, i32, i32, i32 }
@__const.main.au_header = %struct.au_header { i32 779316836, i32 24, i32 0, i32 1, i32 8000, i32 1 }
@.str = [47 x i8] c"usage: <linear input file> <ulaw output file>\0A\00"
@.str.1 = [6 x i8] c"creat\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i16*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.au_header
%11 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast %struct.au_header* %10 to i8*
%13 = load i32, i32* %4
%14 = add nsw i32 %13, -1
store i32 %14, i32* %4
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i32 1
store i8** %16, i8*** %5
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 2
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 0
%25 = load i8*, i8** %24
%26 = call i16* @map(i8* %25, i32* %8)
store i16* %26, i16** %7
%27 = load i16*, i16** %7
%28 = ptrtoint i16* %27 to i32
%29 = sext i32 %28 to i64
%30 = add i64 %29, 24
%31 = inttoptr i64 %30 to i16*
store i16* %31, i16** %7
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = sub i64 %33, 24
%35 = trunc i64 %34 to i32
store i32 %35, i32* %8
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 @creat(i8* %38, i32 438)
store i32 %39, i32* %6
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %22
%42 = call i32 @error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %41, %22
%44 = load i32, i32* %8
%45 = sdiv i32 %44, 2
%46 = getelementptr inbounds %struct.au_header, %struct.au_header* %10, i32 0, i32 2
store i32 %45, i32* %46
%47 = load i32, i32* %6
%48 = bitcast %struct.au_header* %10 to i8*
%49 = call i32 @wr(i32 %47, i8* %48, i32 24)
store i32 0, i32* %9
br label %50
50: ; preds = %63, %43
%51 = load i32, i32* %9
%52 = load i32, i32* %8
%53 = icmp slt i32 %51, %52
br i1 %53, label %54, label %66
54: ; preds = %50
%55 = load i16*, i16** %7
%56 = getelementptr inbounds i16, i16* %55, i32 1
store i16* %56, i16** %7
%57 = load i16, i16* %55
%58 = sext i16 %57 to i32
%59 = and i32 %58, 65535
%60 = call signext i8 @linear2ulaw(i32 %59)
store i8 %60, i8* %11
%61 = load i32, i32* %6
%62 = call i32 @wr(i32 %61, i8* %11, i32 1)
br label %63
63: ; preds = %54
%64 = load i32, i32* %9
%65 = add nsw i32 %64, 2
store i32 %65, i32* %9
br label %50
66: ; preds = %50
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.au_header = type { i32, i32, i32, i32, i32, i32 }
@__const.main.au_header = %struct.au_header { i32 779316836, i32 24, i32 0, i32 1, i32 8000, i32 1 }
@.str.1 = [6 x i8] c"creat\00"
@str = [46 x i8] c"usage: <linear input file> <ulaw output file>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.au_header
%5 = alloca i8
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.au_header* %4 to i8*
%8 = icmp eq i32 %0, 3
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i16* @map(i8* %14, i32* %3)
%16 = ptrtoint i16* %15 to i64
%17 = shl i64 %16, 32
%18 = ashr exact i64 %17, 32
%19 = add nsw i64 %18, 24
%20 = inttoptr i64 %19 to i16*
%21 = load i32, i32* %3
%22 = add i32 %21, -24
store i32 %22, i32* %3
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @creat(i8* %24, i32 438)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %12
%28 = call i32 @error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %12
%30 = load i32, i32* %3
%31 = sdiv i32 %30, 2
%32 = getelementptr inbounds %struct.au_header, %struct.au_header* %4, i64 0, i32 2
store i32 %31, i32* %32
%33 = call i32 @wr(i32 %25, i8* %7, i32 24)
%34 = load i32, i32* %3
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %47
36: ; preds = %29, %36
%37 = phi i32 [ %44, %36 ], [ 0, %29 ]
%38 = phi i16* [ %39, %36 ], [ %20, %29 ]
%39 = getelementptr inbounds i16, i16* %38, i64 1
%40 = load i16, i16* %38
%41 = zext i16 %40 to i32
%42 = call signext i8 @linear2ulaw(i32 %41)
store i8 %42, i8* %5
%43 = call i32 @wr(i32 %25, i8* %5, i32 1)
%44 = add nuw nsw i32 %37, 2
%45 = load i32, i32* %3
%46 = icmp slt i32 %44, %45
br i1 %46, label %36, label %47
47: ; preds = %36, %29
ret i32 0
}