#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 */
struct reader {
  int dummy;
};

/* Variables and functions */
int fullread(struct reader *, unsigned char *, int);
int stream_head_read(struct reader *rds, unsigned long *newhead) {
  unsigned char hbuf[4];
  if (fullread(rds, hbuf, 4) != 4)
    return 0;
  *newhead = ((unsigned long)hbuf[0] << 24) | ((unsigned long)hbuf[1] << 16) |
             ((unsigned long)hbuf[2] << 8) | (unsigned long)hbuf[3];
  return 1;
}


x86 assembly (clang, -O0):
<stream_head_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1030 <fullread@plt>
cmp    $0x4,%eax
je     1147 <stream_head_read+0x37>
movl   $0x0,-0x4(%rbp)
jmp    117c <stream_head_read+0x6c>
movzbl -0x1c(%rbp),%eax
mov    %eax,%ecx
shl    $0x18,%rcx
movzbl -0x1b(%rbp),%eax
shl    $0x10,%rax
or     %rax,%rcx
movzbl -0x1a(%rbp),%eax
shl    $0x8,%rax
or     %rax,%rcx
movzbl -0x19(%rbp),%eax
or     %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stream_head_read>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1030 <fullread@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x4,%ecx
jne    1143 <stream_head_read+0x33>
mov    0xc(%rsp),%eax
shl    $0x20,%rax
bswap  %rax
mov    %rax,(%rbx)
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.reader = type { i32 }
define i32 @stream_head_read(%struct.reader* %0, i64* %1) {
%3 = alloca i32
%4 = alloca %struct.reader*
%5 = alloca i64*
%6 = alloca [4 x i8]
store %struct.reader* %0, %struct.reader** %4
store i64* %1, i64** %5
%7 = load %struct.reader*, %struct.reader** %4
%8 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%9 = call i32 @fullread(%struct.reader* %7, i8* %8, i32 4)
%10 = icmp ne i32 %9, 4
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 0, i32* %3
br label %32
12: ; preds = %2
%13 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%14 = load i8, i8* %13
%15 = zext i8 %14 to i64
%16 = shl i64 %15, 24
%17 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 1
%18 = load i8, i8* %17
%19 = zext i8 %18 to i64
%20 = shl i64 %19, 16
%21 = or i64 %16, %20
%22 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 2
%23 = load i8, i8* %22
%24 = zext i8 %23 to i64
%25 = shl i64 %24, 8
%26 = or i64 %21, %25
%27 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
%28 = load i8, i8* %27
%29 = zext i8 %28 to i64
%30 = or i64 %26, %29
%31 = load i64*, i64** %5
store i64 %30, i64* %31
store i32 1, i32* %3
br label %32
32: ; preds = %12, %11
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.reader = type { i32 }
define i32 @stream_head_read(%struct.reader* %0, i64* nocapture writeonly %1) {
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%5 = call i32 @fullread(%struct.reader* %0, i8* %4, i32 4)
%6 = icmp eq i32 %5, 4
br i1 %6, label %7, label %25
7: ; preds = %2
%8 = load i8, i8* %4
%9 = zext i8 %8 to i64
%10 = shl nuw nsw i64 %9, 24
%11 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%12 = load i8, i8* %11
%13 = zext i8 %12 to i64
%14 = shl nuw nsw i64 %13, 16
%15 = or i64 %14, %10
%16 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%17 = load i8, i8* %16
%18 = zext i8 %17 to i64
%19 = shl nuw nsw i64 %18, 8
%20 = or i64 %15, %19
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
%22 = load i8, i8* %21
%23 = zext i8 %22 to i64
%24 = or i64 %20, %23
store i64 %24, i64* %1
br label %25
25: ; preds = %2, %7
%26 = phi i32 [ 1, %7 ], [ 0, %2 ]
ret i32 %26
}