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

/* Variables and functions */
int /*<<< orphan*/ fail(char *);
int /*<<< orphan*/ printf(char *, char);
int /*<<< orphan*/ puts(char *);
scalar_t__ read(int, void *, int);
void from_fd(int fd) {
  char buf[128];
  ssize_t len;
  len = read(fd, (void *)&buf, 128);
  if (len <= 0)
    fail("read");
  for (int i = 0; i < len; i++) {
    printf("%.2hhx ", buf[i]);
  }
  puts("");
}


x86 assembly (clang, -O0):
<from_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jg     1183 <from_fd+0x43>
lea    0xe82(%rip),%rdi
call   1040 <fail@plt>
movl   $0x0,-0x9c(%rbp)
movslq -0x9c(%rbp),%rax
cmp    -0x98(%rbp),%rax
jge    11d0 <from_fd+0x90>
movslq -0x9c(%rbp),%rax
lea    0xe56(%rip),%rdi
movsbl -0x90(%rbp,%rax,1),%esi
ff
call   1050 <printf@plt>
mov    -0x9c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x9c(%rbp)
jmp    118d <from_fd+0x4d>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<from_fd>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %rsp,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    1187 <from_fd+0x47>
mov    %rax,%r15
xor    %ebx,%ebx
lea    0xe9b(%rip),%r14
nopw   0x0(%rax,%rax,1)
movsbl (%rsp,%rbx,1),%esi
mov    %r14,%rdi
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1170 <from_fd+0x30>
jmp    1193 <from_fd+0x53>
lea    0xe72(%rip),%rdi
call   1040 <fail@plt>
mov    $0xa,%edi
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <putchar@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@.str.1 = [8 x i8] c"%.2hhx \00"
@.str.2 = [1 x i8] zeroinitializer
define void @from_fd(i32 %0) {
%2 = alloca i32
%3 = alloca [128 x i8]
%4 = alloca i64
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = bitcast [128 x i8]* %3 to i8*
%8 = call i64 @read(i32 %6, i8* %7, i32 128)
store i64 %8, i64* %4
%9 = load i64, i64* %4
%10 = icmp sle i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @fail(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
store i32 0, i32* %5
br label %14
14: ; preds = %25, %13
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = load i64, i64* %4
%18 = icmp slt i64 %16, %17
br i1 %18, label %19, label %28
19: ; preds = %14
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8 signext %23)
br label %25
25: ; preds = %19
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
br label %14
28: ; preds = %14
%29 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@.str.1 = [8 x i8] c"%.2hhx \00"
define void @from_fd(i32 %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = call i64 @read(i32 %0, i8* %3, i32 128)
%5 = icmp slt i64 %4, 1
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = tail call i32 @fail(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %10, %6
%9 = tail call i32 @putchar(i32 10)
ret void
10: ; preds = %1, %10
%11 = phi i64 [ %15, %10 ], [ 0, %1 ]
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 %11
%13 = load i8, i8* %12
%14 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8 signext %13)
%15 = add nuw nsw i64 %11, 1
%16 = icmp eq i64 %15, %4
br i1 %16, label %8, label %10
}