#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 /*<<< orphan*/ write_bytes(int, unsigned char *, int);
void write_weird_midi_int(int fd, unsigned int value) {
  unsigned int buf;
  buf = value & 0x7F;
  unsigned char *x;
  int count;
  while ((value >>= 7)) {
    buf <<= 8;
    buf |= ((value & 0x7F) | 0x80);
  }
  count = 0;
  x = (unsigned char *)&buf;
  while (1) {
    write_bytes(fd, x, 1);
    if (*x & 0x80) {
      x++;
      count++;
      if (count >= 4)
        break;
    } else
      break;
  }
}


x86 assembly (clang, -O0):
<write_weird_midi_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
and    $0x7f,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
shr    $0x7,%eax
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     1158 <write_weird_midi_int+0x48>
mov    -0xc(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
and    $0x7f,%eax
or     $0x80,%eax
or     -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    1127 <write_weird_midi_int+0x17>
movl   $0x0,-0x1c(%rbp)
lea    -0xc(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write_bytes@plt>
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     11b6 <write_weird_midi_int+0xa6>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jl     11b1 <write_weird_midi_int+0xa1>
jmp    11c0 <write_weird_midi_int+0xb0>
jmp    11bb <write_weird_midi_int+0xab>
jmp    11c0 <write_weird_midi_int+0xb0>
jmp    1167 <write_weird_midi_int+0x57>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_weird_midi_int>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    %esi,%eax
and    $0x7f,%eax
cmp    $0x80,%esi
jae    1126 <write_weird_midi_int+0x16>
mov    %eax,%edx
jmp    114d <write_weird_midi_int+0x3d>
mov    %esi,%ecx
nopl   0x0(%rax,%rax,1)
shr    $0x7,%ecx
shl    $0x8,%eax
movzbl %cl,%edx
or     %eax,%edx
or     $0x80,%edx
mov    %edx,%eax
cmp    $0x4000,%esi
mov    %ecx,%esi
jae    1130 <write_weird_midi_int+0x20>
mov    %edx,0x4(%rsp)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    (%rsp,%rbx,1),%rsi
add    $0x4,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write_bytes@plt>
cmpb   $0x0,0x4(%rsp,%rbx,1)
jns    1187 <write_weird_midi_int+0x77>
lea    0x1(%rbx),%rax
cmp    $0x3,%ebx
mov    %rax,%rbx
jb     1160 <write_weird_midi_int+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @write_weird_midi_int(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = and i32 %8, 127
store i32 %9, i32* %5
br label %10
10: ; preds = %14, %2
%11 = load i32, i32* %4
%12 = lshr i32 %11, 7
store i32 %12, i32* %4
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %10
%15 = load i32, i32* %5
%16 = shl i32 %15, 8
store i32 %16, i32* %5
%17 = load i32, i32* %4
%18 = and i32 %17, 127
%19 = or i32 %18, 128
%20 = load i32, i32* %5
%21 = or i32 %20, %19
store i32 %21, i32* %5
br label %10
22: ; preds = %10
store i32 0, i32* %7
%23 = bitcast i32* %5 to i8*
store i8* %23, i8** %6
br label %24
24: ; preds = %22, %43
%25 = load i32, i32* %3
%26 = load i8*, i8** %6
%27 = call i32 @write_bytes(i32 %25, i8* %26, i32 1)
%28 = load i8*, i8** %6
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = and i32 %30, 128
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %42
33: ; preds = %24
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %6
%36 = load i32, i32* %7
%37 = add nsw i32 %36, 1
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = icmp sge i32 %38, 4
br i1 %39, label %40, label %41
40: ; preds = %33
br label %44
41: ; preds = %33
br label %43
42: ; preds = %24
br label %44
43: ; preds = %41
br label %24
44: ; preds = %42, %40
ret void
}

LLVM IR (-O2):
define void @write_weird_midi_int(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = and i32 %1, 127
%6 = icmp ult i32 %1, 128
br i1 %6, label %7, label %9
7: ; preds = %9, %2
%8 = phi i32 [ %5, %2 ], [ %16, %9 ]
store i32 %8, i32* %3
br label %18
9: ; preds = %2, %9
%10 = phi i32 [ %12, %9 ], [ %1, %2 ]
%11 = phi i32 [ %16, %9 ], [ %5, %2 ]
%12 = lshr i32 %10, 7
%13 = shl i32 %11, 8
%14 = and i32 %12, 127
%15 = or i32 %14, %13
%16 = or i32 %15, 128
%17 = icmp ult i32 %10, 16384
br i1 %17, label %7, label %9
18: ; preds = %7, %18
%19 = phi i8* [ %24, %18 ], [ %4, %7 ]
%20 = phi i32 [ %25, %18 ], [ 0, %7 ]
%21 = call i32 @write_bytes(i32 %0, i8* %19, i32 1)
%22 = load i8, i8* %19
%23 = icmp sgt i8 %22, -1
%24 = getelementptr inbounds i8, i8* %19, i64 1
%25 = add nuw nsw i32 %20, 1
%26 = icmp ugt i32 %20, 2
%27 = select i1 %23, i1 true, i1 %26
br i1 %27, label %28, label %18
28: ; preds = %18
ret void
}