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

/* Variables and functions */
int /*<<< orphan*/ errno;
char fread(char *, int, char, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int scan(FILE *fp, char *buf, char dum, int print) {
  if (fread(buf, 1, dum, fp) != dum) {
    printf("Read: %s\n", strerror(errno));
    return 1;
  }
  if (print) {
    printf("\t");
    for (int i = 0; i < dum; ++i) {
      printf("%02x.", (uint8_t)buf[i]);
    }
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<scan>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %al,-0x19(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x19(%rbp),%al
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
movsbl %al,%edx
call   1030 <fread@plt>
movsbl %al,%eax
movsbl -0x19(%rbp),%ecx
cmp    %ecx,%eax
je     119a <scan+0x6a>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %rax,%rsi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1205 <scan+0xd5>
cmpl   $0x0,-0x20(%rbp)
je     11fe <scan+0xce>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
movsbl -0x19(%rbp),%ecx
cmp    %ecx,%eax
jge    11f0 <scan+0xc0>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b9 <scan+0x89>
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r15d
mov    %edx,%ebp
mov    %rsi,%r12
mov    %rdi,%rcx
mov    $0x1,%r14d
mov    %rsi,%rdi
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    %bpl,%al
jne    11b4 <scan+0x74>
xor    %r14d,%r14d
test   %r15d,%r15d
je     11d3 <scan+0x93>
mov    $0x9,%edi
call   1030 <putchar@plt>
test   %bpl,%bpl
jle    11a8 <scan+0x68>
mov    %ebp,%ebp
lea    0xe7f(%rip),%r15
xor    %ebx,%ebx
nopl   (%rax)
movsbl (%r12,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1190 <scan+0x50>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    11d3 <scan+0x93>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe37(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"Read: %s\0A\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"\09\00"
@.str.2 = [6 x i8] c"%02x.\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @scan(i32* %0, i8* %1, i8 signext %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %6
store i8* %1, i8** %7
store i8 %2, i8* %8
store i32 %3, i32* %9
%11 = load i8*, i8** %7
%12 = load i8, i8* %8
%13 = load i32*, i32** %6
%14 = call signext i8 @fread(i8* %11, i32 1, i8 signext %12, i32* %13)
%15 = sext i8 %14 to i32
%16 = load i8, i8* %8
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %15, %17
br i1 %18, label %19, label %23
19: ; preds = %4
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 1, i32* %5
br label %47
23: ; preds = %4
%24 = load i32, i32* %9
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %46
26: ; preds = %23
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %10
br label %28
28: ; preds = %41, %26
%29 = load i32, i32* %10
%30 = load i8, i8* %8
%31 = sext i8 %30 to i32
%32 = icmp slt i32 %29, %31
br i1 %32, label %33, label %44
33: ; preds = %28
%34 = load i8*, i8** %7
%35 = load i32, i32* %10
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %33
%42 = load i32, i32* %10
%43 = add nsw i32 %42, 1
store i32 %43, i32* %10
br label %28
44: ; preds = %28
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %23
store i32 0, i32* %5
br label %47
47: ; preds = %46, %19
%48 = load i32, i32* %5
ret i32 %48
}

LLVM IR (-O2):
@.str = [10 x i8] c"Read: %s\0A\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"%02x.\00"
define i32 @scan(i32* nocapture %0, i8* nocapture %1, i8 signext %2, i32 %3) {
%5 = tail call signext i8 @fread(i8* %1, i32 1, i8 signext %2, i32* %0)
%6 = sext i8 %2 to i64
%7 = icmp eq i8 %5, %2
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %29
12: ; preds = %4
%13 = icmp eq i32 %3, 0
br i1 %13, label %29, label %14
14: ; preds = %12
%15 = tail call i32 @putchar(i32 9)
%16 = icmp sgt i8 %2, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = and i64 %6, 4294967295
br label %21
19: ; preds = %21, %14
%20 = tail call i32 @putchar(i32 10)
br label %29
21: ; preds = %17, %21
%22 = phi i64 [ 0, %17 ], [ %27, %21 ]
%23 = getelementptr inbounds i8, i8* %1, i64 %22
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = add nuw nsw i64 %22, 1
%28 = icmp eq i64 %27, %18
br i1 %28, label %19, label %21
29: ; preds = %12, %19, %8
%30 = phi i32 [ 1, %8 ], [ 0, %19 ], [ 0, %12 ]
ret i32 %30
}