#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*/ fwrite(unsigned int *, unsigned int, int,
                          int /*<<< orphan*/ *);
int read_char(int);
void decode_profile(int fd, FILE *out) {
  unsigned int event_id;
  unsigned int size = 0;
  unsigned int data[10];
  int i;
  while (1) {
    event_id = read_char(fd);
    size = read_char(fd);
    for (i = 0; i < size; i++) {
      data[i] = read_char(fd);
      data[i] += read_char(fd) << 8;
      data[i] += read_char(fd) << 16;
      data[i] += read_char(fd) << 24;
    }
    fwrite(&event_id, 1, 2, out);
    fwrite(&size, 1, 2, out);
    fwrite(data, size, 4, out);
    return;
  }
}


x86 assembly (clang, -O0):
<decode_profile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <read_char@plt>
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <read_char@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x18(%rbp),%eax
jae    11ca <decode_profile+0xaa>
mov    -0x4(%rbp),%edi
call   1030 <read_char@plt>
mov    %eax,%ecx
movslq -0x44(%rbp),%rax
mov    %ecx,-0x40(%rbp,%rax,4)
mov    -0x4(%rbp),%edi
call   1030 <read_char@plt>
mov    %eax,%ecx
shl    $0x8,%ecx
movslq -0x44(%rbp),%rax
add    -0x40(%rbp,%rax,4),%ecx
mov    %ecx,-0x40(%rbp,%rax,4)
mov    -0x4(%rbp),%edi
call   1030 <read_char@plt>
mov    %eax,%ecx
shl    $0x10,%ecx
movslq -0x44(%rbp),%rax
add    -0x40(%rbp,%rax,4),%ecx
mov    %ecx,-0x40(%rbp,%rax,4)
mov    -0x4(%rbp),%edi
call   1030 <read_char@plt>
mov    %eax,%ecx
shl    $0x18,%ecx
movslq -0x44(%rbp),%rax
add    -0x40(%rbp,%rax,4),%ecx
mov    %ecx,-0x40(%rbp,%rax,4)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1153 <decode_profile+0x33>
mov    -0x10(%rbp),%rcx
lea    -0x14(%rbp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
call   1040 <fwrite@plt>
mov    -0x10(%rbp),%rcx
lea    -0x18(%rbp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
call   1040 <fwrite@plt>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x4,%edx
call   1040 <fwrite@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_profile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
call   1030 <read_char@plt>
mov    %eax,0xc(%rsp)
mov    %r15d,%edi
call   1030 <read_char@plt>
mov    %eax,0x8(%rsp)
test   %eax,%eax
je     11a3 <decode_profile+0x83>
mov    %eax,%r12d
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
call   1030 <read_char@plt>
mov    %eax,%ebx
mov    %r15d,%edi
call   1030 <read_char@plt>
mov    %eax,%ebp
shl    $0x8,%ebp
add    %ebx,%ebp
mov    %r15d,%edi
call   1030 <read_char@plt>
mov    %eax,%ebx
shl    $0x10,%ebx
add    %ebp,%ebx
mov    %r15d,%edi
call   1030 <read_char@plt>
shl    $0x18,%eax
add    %ebx,%eax
mov    %eax,0x10(%rsp,%r13,4)
add    $0x1,%r13
cmp    %r13,%r12
jne    1160 <decode_profile+0x40>
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %r14,%rcx
call   1040 <fwrite@plt>
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %r14,%rcx
call   1040 <fwrite@plt>
mov    0x8(%rsp),%esi
lea    0x10(%rsp),%rdi
mov    $0x4,%edx
mov    %r14,%rcx
call   1040 <fwrite@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @decode_profile(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [10 x i32]
%8 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
store i32 0, i32* %6
br label %9
9: ; preds = %2
%10 = load i32, i32* %3
%11 = call i32 @read_char(i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %3
%13 = call i32 @read_char(i32 %12)
store i32 %13, i32* %6
store i32 0, i32* %8
br label %14
14: ; preds = %48, %9
%15 = load i32, i32* %8
%16 = load i32, i32* %6
%17 = icmp ult i32 %15, %16
br i1 %17, label %18, label %51
18: ; preds = %14
%19 = load i32, i32* %3
%20 = call i32 @read_char(i32 %19)
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 %22
store i32 %20, i32* %23
%24 = load i32, i32* %3
%25 = call i32 @read_char(i32 %24)
%26 = shl i32 %25, 8
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 %28
%30 = load i32, i32* %29
%31 = add i32 %30, %26
store i32 %31, i32* %29
%32 = load i32, i32* %3
%33 = call i32 @read_char(i32 %32)
%34 = shl i32 %33, 16
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 %36
%38 = load i32, i32* %37
%39 = add i32 %38, %34
store i32 %39, i32* %37
%40 = load i32, i32* %3
%41 = call i32 @read_char(i32 %40)
%42 = shl i32 %41, 24
%43 = load i32, i32* %8
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 %44
%46 = load i32, i32* %45
%47 = add i32 %46, %42
store i32 %47, i32* %45
br label %48
48: ; preds = %18
%49 = load i32, i32* %8
%50 = add nsw i32 %49, 1
store i32 %50, i32* %8
br label %14
51: ; preds = %14
%52 = load i32*, i32** %4
%53 = call i32 @fwrite(i32* %5, i32 1, i32 2, i32* %52)
%54 = load i32*, i32** %4
%55 = call i32 @fwrite(i32* %6, i32 1, i32 2, i32* %54)
%56 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 0
%57 = load i32, i32* %6
%58 = load i32*, i32** %4
%59 = call i32 @fwrite(i32* %56, i32 %57, i32 4, i32* %58)
ret void
}

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