#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__ uint16_t;

/* Variables and functions */
int read(int const, char *, int);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int);
int get_count(const int fd, uint16_t *count) {
  char buf[sizeof(*count) + 1];
  if (read(fd, buf, sizeof(*count)) != sizeof(*count))
    return 1;
  buf[sizeof(*count)] = '\0';
  unsigned long val = strtoul(buf, NULL, 16);
  if (val > 0xFF)
    return 1;
  *count = (uint16_t)val;
  return 0;
}


x86 assembly (clang, -O0):
<get_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x8,%edx
call   1030 <read@plt>
cltq
cmp    $0x8,%rax
je     1158 <get_count+0x38>
movl   $0x1,-0x4(%rbp)
jmp    119e <get_count+0x7e>
movb   $0x0,-0x11(%rbp)
lea    -0x19(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1040 <strtoul@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xff,-0x28(%rbp)
jbe    118c <get_count+0x6c>
movl   $0x1,-0x4(%rbp)
jmp    119e <get_count+0x7e>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_count>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
lea    0xf(%rsp),%rsi
mov    $0x8,%edx
call   1030 <read@plt>
mov    $0x1,%ebp
cmp    $0x8,%eax
jne    1165 <get_count+0x45>
movb   $0x0,0x17(%rsp)
lea    0xf(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <strtoul@plt>
cmp    $0xff,%rax
ja     1165 <get_count+0x45>
mov    %rax,(%rbx)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @get_count(i32 %0, i64* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64*
%6 = alloca [9 x i8]
%7 = alloca i64
store i32 %0, i32* %4
store i64* %1, i64** %5
%8 = load i32, i32* %4
%9 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 8)
%11 = sext i32 %10 to i64
%12 = icmp ne i64 %11, 8
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 1, i32* %3
br label %24
14: ; preds = %2
%15 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 8
store i8 0, i8* %15
%16 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%17 = call i64 @strtoul(i8* %16, i32* null, i32 16)
store i64 %17, i64* %7
%18 = load i64, i64* %7
%19 = icmp ugt i64 %18, 255
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 1, i32* %3
br label %24
21: ; preds = %14
%22 = load i64, i64* %7
%23 = load i64*, i64** %5
store i64 %22, i64* %23
store i32 0, i32* %3
br label %24
24: ; preds = %21, %20, %13
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
define i32 @get_count(i32 %0, i64* nocapture writeonly %1) {
%3 = alloca [9 x i8]
%4 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%5 = call i32 @read(i32 %0, i8* %4, i32 8)
%6 = icmp eq i32 %5, 8
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 8
store i8 0, i8* %8
%9 = call i64 @strtoul(i8* nocapture %4, i32* null, i32 16)
%10 = icmp ugt i64 %9, 255
br i1 %10, label %12, label %11
11: ; preds = %7
store i64 %9, i64* %1
br label %12
12: ; preds = %11, %7, %2
%13 = phi i32 [ 1, %2 ], [ 0, %11 ], [ 1, %7 ]
ret i32 %13
}