#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*/ fscanf(int /*<<< orphan*/ *, char *, unsigned long *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
void read_word(FILE *in, FILE *out, unsigned w) {
  unsigned i;
  unsigned long x;
  fscanf(in, "%lx", &x);
  for (i = 0; i < w; ++i) {
    char b = (x >> (8 * i)) & 0xff;
    fwrite(&b, 1, 1, out);
  }
}


x86 assembly (clang, -O0):
<read_word>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xec2(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <fscanf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1194 <read_word+0x74>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
shl    $0x3,%ecx
mov    %ecx,%ecx
shr    %cl,%rax
and    $0xff,%rax
mov    %al,-0x21(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x21(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fwrite@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    114e <read_word+0x2e>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_word>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
lea    0xecc(%rip),%rsi
lea    0x8(%rsp),%rdx
call   1030 <fscanf@plt>
test   %ebp,%ebp
je     1171 <read_word+0x51>
mov    %ebp,%ebp
shl    $0x3,%rbp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rax
mov    %ebx,%ecx
and    $0xf8,%cl
shr    %cl,%rax
movsbl %al,%edi
mov    %r14,%rsi
call   1040 <fputc@plt>
add    $0x8,%rbx
cmp    %rbx,%rbp
jne    1150 <read_word+0x30>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%lx\00"
define void @read_word(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%10 = load i32*, i32** %4
%11 = call i32 @fscanf(i32* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %8)
store i32 0, i32* %7
br label %12
12: ; preds = %26, %3
%13 = load i32, i32* %7
%14 = load i32, i32* %6
%15 = icmp ult i32 %13, %14
br i1 %15, label %16, label %29
16: ; preds = %12
%17 = load i64, i64* %8
%18 = load i32, i32* %7
%19 = mul i32 8, %18
%20 = zext i32 %19 to i64
%21 = lshr i64 %17, %20
%22 = and i64 %21, 255
%23 = trunc i64 %22 to i8
store i8 %23, i8* %9
%24 = load i32*, i32** %5
%25 = call i32 @fwrite(i8* %9, i32 1, i32 1, i32* %24)
br label %26
26: ; preds = %16
%27 = load i32, i32* %7
%28 = add i32 %27, 1
store i32 %28, i32* %7
br label %12
29: ; preds = %12
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"%lx\00"
define void @read_word(i32* nocapture %0, i32* nocapture %1, i32 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
%6 = call i32 @fscanf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64* %4)
%7 = icmp eq i32 %2, 0
br i1 %7, label %22, label %8
8: ; preds = %3
%9 = zext i32 %2 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %20, %10 ]
%12 = load i64, i64* %4
%13 = shl i64 %11, 3
%14 = and i64 %13, 4294967288
%15 = lshr i64 %12, %14
%16 = trunc i64 %15 to i32
%17 = shl i32 %16, 24
%18 = ashr exact i32 %17, 24
%19 = call i32 @fputc(i32 %18, i32* %1)
%20 = add nuw nsw i64 %11, 1
%21 = icmp eq i64 %20, %9
br i1 %21, label %22, label %10
22: ; preds = %10, %3
ret void
}