#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 */

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pabort(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ setConfig01(int);
int read_func(int fd, int len) {
  char *rx, *bp;
  int ret = 0;
  rx = malloc(len);
  memset(rx, 0, len);
  setConfig01(fd);
  ret = read(fd, rx, len);
  if (ret == -1)
    pabort("can't read");
  printf("response(%2d, %2d): ", len, ret);
  for (bp = rx; len; len--)
    printf(" %02x", *bp++);
  printf("\n");
  free(rx);
  return ret;
}


x86 assembly (clang, -O0):
<read_func>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x4(%rbp),%edi
call   1040 <setConfig01@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1070 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11cf <read_func+0x5f>
lea    0xe36(%rip),%rdi
call   1090 <pabort@plt>
mov    -0x8(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1223 <read_func+0xb3>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movsbl (%rax),%esi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    11eb <read_func+0x7b>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_func>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebp
mov    %edi,%r14d
mov    %esi,%edi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1070 <memset@plt>
mov    %r14d,%edi
call   1050 <setConfig01@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1080 <read@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    11cc <read_func+0x4c>
lea    0xe39(%rip),%rdi
call   10a0 <pabort@plt>
lea    0xe38(%rip),%rdi
mov    %ebp,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebp,%ebp
je     1207 <read_func+0x87>
mov    %ebp,%ebp
lea    0xe34(%rip),%r15
xor    %ebx,%ebx
xchg   %ax,%ax
movsbl (%r12,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    %ebx,%ebp
jne    11f0 <read_func+0x70>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"can't read\00"
@.str.1 = [21 x i8] c"response(%2d, %2d): \00"
@.str.2 = [6 x i8] c" %02x\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @read_func(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %7
%8 = load i32, i32* %4
%9 = call i8* @malloc(i32 %8)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = load i32, i32* %4
%12 = call i32 @memset(i8* %10, i32 0, i32 %11)
%13 = load i32, i32* %3
%14 = call i32 @setConfig01(i32 %13)
%15 = load i32, i32* %3
%16 = load i8*, i8** %5
%17 = load i32, i32* %4
%18 = call i32 @read(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 @pabort(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %2
%24 = load i32, i32* %4
%25 = load i32, i32* %7
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i8*, i8** %5
store i8* %27, i8** %6
br label %28
28: ; preds = %37, %23
%29 = load i32, i32* %4
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %40
31: ; preds = %28
%32 = load i8*, i8** %6
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %6
%34 = load i8, i8* %32
%35 = sext i8 %34 to i32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %31
%38 = load i32, i32* %4
%39 = add nsw i32 %38, -1
store i32 %39, i32* %4
br label %28
40: ; preds = %28
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%42 = load i8*, i8** %5
%43 = call i32 @free(i8* %42)
%44 = load i32, i32* %7
ret i32 %44
}

LLVM IR (-O2):
@.str = [11 x i8] c"can't read\00"
@.str.1 = [21 x i8] c"response(%2d, %2d): \00"
@.str.2 = [6 x i8] c" %02x\00"
define i32 @read_func(i32 %0, i32 %1) {
%3 = tail call i8* @malloc(i32 %1)
%4 = tail call i32 @memset(i8* %3, i32 0, i32 %1)
%5 = tail call i32 @setConfig01(i32 %0)
%6 = tail call i32 @read(i32 %0, i8* %3, i32 %1)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @pabort(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %6)
%12 = icmp eq i32 %1, 0
br i1 %12, label %22, label %13
13: ; preds = %10, %13
%14 = phi i8* [ %16, %13 ], [ %3, %10 ]
%15 = phi i32 [ %20, %13 ], [ %1, %10 ]
%16 = getelementptr inbounds i8, i8* %14, i64 1
%17 = load i8, i8* %14
%18 = sext i8 %17 to i32
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = add nsw i32 %15, -1
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %13
22: ; preds = %13, %10
%23 = tail call i32 @putchar(i32 10)
%24 = tail call i32 @free(i8* %3)
ret i32 %6
}