#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*/ tracks;
typedef int /*<<< orphan*/ length;
typedef int /*<<< orphan*/ format;
typedef int /*<<< orphan*/ divisions;

/* Variables and functions */
unsigned int htonl(int);
unsigned short htons(int);
int write(int, char *, int);
int write_bytes(int, ...);
int write_MThd(int fd) {
  unsigned int length;
  int rc;
  unsigned short format = htons(0);
  unsigned short tracks = htons(1);
  unsigned short divisions = htons(0xE728);
  length = htonl(6);
  rc = write(fd, "MThd", 4);
  if (rc != 4)
    return -1;
  rc = write_bytes(fd, &length, sizeof(length));
  if (rc != sizeof(length))
    return -1;
  rc = write_bytes(fd, &format, sizeof(format));
  if (rc != sizeof(format))
    return -1;
  rc = write_bytes(fd, &tracks, sizeof(tracks));
  if (rc != sizeof(tracks))
    return -1;
  rc = write_bytes(fd, &divisions, sizeof(divisions));
  if (rc != sizeof(divisions))
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<write_MThd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
xor    %edi,%edi
call   1040 <htons@plt>
mov    %ax,-0x12(%rbp)
mov    $0x1,%edi
call   1040 <htons@plt>
mov    %ax,-0x14(%rbp)
mov    $0xe728,%edi
call   1040 <htons@plt>
mov    %ax,-0x16(%rbp)
mov    $0x6,%edi
call   1050 <htonl@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    0xe77(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x4,-0x10(%rbp)
je     11ac <write_MThd+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <write_MThd+0x133>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1060 <write_bytes@plt>
mov    %eax,-0x10(%rbp)
movslq -0x10(%rbp),%rax
cmp    $0x4,%rax
je     11dc <write_MThd+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <write_MThd+0x133>
mov    -0x8(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1060 <write_bytes@plt>
mov    %eax,-0x10(%rbp)
movslq -0x10(%rbp),%rax
cmp    $0x2,%rax
je     120c <write_MThd+0xcc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <write_MThd+0x133>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1060 <write_bytes@plt>
mov    %eax,-0x10(%rbp)
movslq -0x10(%rbp),%rax
cmp    $0x2,%rax
je     123c <write_MThd+0xfc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <write_MThd+0x133>
mov    -0x8(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1060 <write_bytes@plt>
mov    %eax,-0x10(%rbp)
movslq -0x10(%rbp),%rax
cmp    $0x2,%rax
je     126c <write_MThd+0x12c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <write_MThd+0x133>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_MThd>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
xor    %edi,%edi
call   1040 <htons@plt>
mov    %ax,0x12(%rsp)
mov    $0x1,%edi
call   1040 <htons@plt>
mov    %ax,0x10(%rsp)
mov    $0xe728,%edi
call   1040 <htons@plt>
mov    %ax,0xe(%rsp)
mov    $0x6,%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
lea    0xe79(%rip),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
mov    $0xffffffff,%ebp
cmp    $0x4,%eax
jne    1203 <write_MThd+0xc3>
lea    0x14(%rsp),%rsi
mov    $0x4,%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <write_bytes@plt>
cmp    $0x4,%eax
jne    1203 <write_MThd+0xc3>
lea    0x12(%rsp),%rsi
mov    $0x2,%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <write_bytes@plt>
cmp    $0x2,%eax
jne    1203 <write_MThd+0xc3>
lea    0x10(%rsp),%rsi
mov    $0x2,%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <write_bytes@plt>
cmp    $0x2,%eax
jne    1203 <write_MThd+0xc3>
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1060 <write_bytes@plt>
xor    %ebp,%ebp
cmp    $0x2,%eax
setne  %bpl
neg    %ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"MThd\00"
define i32 @write_MThd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i16
%7 = alloca i16
%8 = alloca i16
store i32 %0, i32* %3
%9 = call zeroext i16 @htons(i32 0)
store i16 %9, i16* %6
%10 = call zeroext i16 @htons(i32 1)
store i16 %10, i16* %7
%11 = call zeroext i16 @htons(i32 59176)
store i16 %11, i16* %8
%12 = call i32 @htonl(i32 6)
store i32 %12, i32* %4
%13 = load i32, i32* %3
%14 = call i32 @write(i32 %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 4
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %47
18: ; preds = %1
%19 = load i32, i32* %3
%20 = call i32 (i32, ...) @write_bytes(i32 %19, i32* %4, i64 4)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = icmp ne i64 %22, 4
br i1 %23, label %24, label %25
24: ; preds = %18
store i32 -1, i32* %2
br label %47
25: ; preds = %18
%26 = load i32, i32* %3
%27 = call i32 (i32, ...) @write_bytes(i32 %26, i16* %6, i64 2)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = icmp ne i64 %29, 2
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 -1, i32* %2
br label %47
32: ; preds = %25
%33 = load i32, i32* %3
%34 = call i32 (i32, ...) @write_bytes(i32 %33, i16* %7, i64 2)
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = icmp ne i64 %36, 2
br i1 %37, label %38, label %39
38: ; preds = %32
store i32 -1, i32* %2
br label %47
39: ; preds = %32
%40 = load i32, i32* %3
%41 = call i32 (i32, ...) @write_bytes(i32 %40, i16* %8, i64 2)
store i32 %41, i32* %5
%42 = load i32, i32* %5
%43 = sext i32 %42 to i64
%44 = icmp ne i64 %43, 2
br i1 %44, label %45, label %46
45: ; preds = %39
store i32 -1, i32* %2
br label %47
46: ; preds = %39
store i32 0, i32* %2
br label %47
47: ; preds = %46, %45, %38, %31, %24, %17
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@.str = [5 x i8] c"MThd\00"
define i32 @write_MThd(i32 %0) {
%2 = alloca i32
%3 = alloca i16
%4 = alloca i16
%5 = alloca i16
%6 = bitcast i32* %2 to i8*
%7 = bitcast i16* %3 to i8*
%8 = tail call zeroext i16 @htons(i32 0)
store i16 %8, i16* %3
%9 = bitcast i16* %4 to i8*
%10 = tail call zeroext i16 @htons(i32 1)
store i16 %10, i16* %4
%11 = bitcast i16* %5 to i8*
%12 = tail call zeroext i16 @htons(i32 59176)
store i16 %12, i16* %5
%13 = tail call i32 @htonl(i32 6)
store i32 %13, i32* %2
%14 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%15 = icmp eq i32 %14, 4
br i1 %15, label %16, label %29
16: ; preds = %1
%17 = call i32 (i32, ...) @write_bytes(i32 %0, i32* %2, i64 4)
%18 = icmp eq i32 %17, 4
br i1 %18, label %19, label %29
19: ; preds = %16
%20 = call i32 (i32, ...) @write_bytes(i32 %0, i16* %3, i64 2)
%21 = icmp eq i32 %20, 2
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = call i32 (i32, ...) @write_bytes(i32 %0, i16* %4, i64 2)
%24 = icmp eq i32 %23, 2
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = call i32 (i32, ...) @write_bytes(i32 %0, i16* %5, i64 2)
%27 = icmp ne i32 %26, 2
%28 = sext i1 %27 to i32
br label %29
29: ; preds = %25, %22, %19, %16, %1
%30 = phi i32 [ -1, %1 ], [ -1, %16 ], [ -1, %19 ], [ -1, %22 ], [ %28, %25 ]
ret i32 %30
}