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

/* Variables and functions */
int /*<<< orphan*/ fputc(scalar_t__, int /*<<< orphan*/ *);
size_t write_bin(uint8_t *code, FILE *stream) {
  size_t i;
  size_t last = 0;
  for (i = 0; i < 65536; i++) {
    if (code[i] != 0)
      last = i;
  }
  for (i = 0; i <= last; i++)
    fputc(code[i], stream);
  return i;
}


x86 assembly (clang, -O0):
<write_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x10000,-0x18(%rbp)
jae    116f <write_bin+0x5f>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1159 <write_bin+0x49>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    115e <write_bin+0x4e>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1130 <write_bin+0x20>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
ja     11ab <write_bin+0x9b>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <fputc@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1177 <write_bin+0x67>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_bin>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmpq   $0x0,(%r15,%rax,8)
cmovne %rax,%rbx
cmpq   $0x0,0x8(%r15,%rax,8)
lea    0x1(%rax),%rcx
cmove  %rbx,%rcx
lea    0x2(%rax),%rdx
cmpq   $0x0,0x10(%r15,%rax,8)
cmove  %rcx,%rdx
lea    0x3(%rax),%rbx
cmpq   $0x0,0x18(%r15,%rax,8)
cmove  %rdx,%rbx
add    $0x4,%rax
cmp    $0x10000,%rax
jne    1130 <write_bin+0x20>
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15,%r12,8),%rdi
mov    %r14,%rsi
call   1030 <fputc@plt>
add    $0x1,%r12
cmp    %rbx,%r12
jbe    1180 <write_bin+0x70>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @write_bin(i64* %0, i32* %1) {
%3 = alloca i64*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
store i64* %0, i64** %3
store i32* %1, i32** %4
store i64 0, i64* %6
store i64 0, i64* %5
br label %7
7: ; preds = %19, %2
%8 = load i64, i64* %5
%9 = icmp ult i64 %8, 65536
br i1 %9, label %10, label %22
10: ; preds = %7
%11 = load i64*, i64** %3
%12 = load i64, i64* %5
%13 = getelementptr inbounds i64, i64* %11, i64 %12
%14 = load i64, i64* %13
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = load i64, i64* %5
store i64 %17, i64* %6
br label %18
18: ; preds = %16, %10
br label %19
19: ; preds = %18
%20 = load i64, i64* %5
%21 = add i64 %20, 1
store i64 %21, i64* %5
br label %7
22: ; preds = %7
store i64 0, i64* %5
br label %23
23: ; preds = %34, %22
%24 = load i64, i64* %5
%25 = load i64, i64* %6
%26 = icmp ule i64 %24, %25
br i1 %26, label %27, label %37
27: ; preds = %23
%28 = load i64*, i64** %3
%29 = load i64, i64* %5
%30 = getelementptr inbounds i64, i64* %28, i64 %29
%31 = load i64, i64* %30
%32 = load i32*, i32** %4
%33 = call i32 @fputc(i64 %31, i32* %32)
br label %34
34: ; preds = %27
%35 = load i64, i64* %5
%36 = add i64 %35, 1
store i64 %36, i64* %5
br label %23
37: ; preds = %23
%38 = load i64, i64* %5
ret i64 %38
}

LLVM IR (-O2):
define i64 @write_bin(i64* nocapture %0, i32* nocapture %1) {
br label %3
3: ; preds = %3, %2
%4 = phi i64 [ 0, %2 ], [ %24, %3 ]
%5 = phi i64 [ 0, %2 ], [ %25, %3 ]
%6 = getelementptr inbounds i64, i64* %0, i64 %5
%7 = load i64, i64* %6
%8 = icmp eq i64 %7, 0
%9 = select i1 %8, i64 %4, i64 %5
%10 = or i64 %5, 1
%11 = getelementptr inbounds i64, i64* %0, i64 %10
%12 = load i64, i64* %11
%13 = icmp eq i64 %12, 0
%14 = select i1 %13, i64 %9, i64 %10
%15 = or i64 %5, 2
%16 = getelementptr inbounds i64, i64* %0, i64 %15
%17 = load i64, i64* %16
%18 = icmp eq i64 %17, 0
%19 = select i1 %18, i64 %14, i64 %15
%20 = or i64 %5, 3
%21 = getelementptr inbounds i64, i64* %0, i64 %20
%22 = load i64, i64* %21
%23 = icmp eq i64 %22, 0
%24 = select i1 %23, i64 %19, i64 %20
%25 = add nuw nsw i64 %5, 4
%26 = icmp eq i64 %25, 65536
br i1 %26, label %27, label %3
27: ; preds = %3, %27
%28 = phi i64 [ %32, %27 ], [ 0, %3 ]
%29 = getelementptr inbounds i64, i64* %0, i64 %28
%30 = load i64, i64* %29
%31 = tail call i32 @fputc(i64 %30, i32* %1)
%32 = add i64 %28, 1
%33 = icmp ugt i64 %32, %24
br i1 %33, label %34, label %27
34: ; preds = %27
ret i64 %32
}