#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 /*<<< orphan*/ buffer;

/* Variables and functions */
int getDebug();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int read(int, char *, int);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ strncat(char *, char *, int);
void read_port(int *fd, char **replay_msg) {
  char buffer[8];
  int nbytes;
  int i, n = 0;
  if (getDebug() > 1)
    printf("I: Read port:\n");
  while ((nbytes = read(*fd, buffer, buffer + sizeof(buffer) - buffer - 1)) >
         0) {
    if (getDebug() > 1)
      for (i = 0; i < nbytes; i++)
        printf("\t%02d: %#x\n", i, buffer[i]);
    *replay_msg = (char *)realloc(*replay_msg, (n + nbytes) * sizeof(char));
    strncat(*replay_msg, buffer, nbytes);
    n += nbytes;
  }
  if (getDebug() > 1)
    puts("OK");
}


x86 assembly (clang, -O0):
<read_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1070 <getDebug@plt>
cmp    $0x1,%eax
jle    1195 <read_port+0x35>
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    119a <read_port+0x3a>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
lea    -0x18(%rbp),%rax
add    $0x8,%rax
lea    -0x18(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    125f <read_port+0xff>
mov    $0x0,%al
call   1070 <getDebug@plt>
cmp    $0x1,%eax
jle    121a <read_port+0xba>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1215 <read_port+0xb5>
mov    -0x20(%rbp),%esi
movslq -0x20(%rbp),%rax
movsbl -0x18(%rbp,%rax,1),%edx
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11e1 <read_port+0x81>
jmp    121a <read_port+0xba>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%eax
add    -0x1c(%rbp),%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1080 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <strncat@plt>
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    119a <read_port+0x3a>
mov    $0x0,%al
call   1070 <getDebug@plt>
cmp    $0x1,%eax
jle    127b <read_port+0x11b>
lea    0xda5(%rip),%rdi
call   1030 <puts@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_port>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,0x8(%rsp)
xor    %eax,%eax
call   1070 <getDebug@plt>
cmp    $0x2,%eax
jl     118e <read_port+0x2e>
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
mov    $0x7,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    122e <read_port+0xce>
mov    %eax,%r13d
lea    0xe4a(%rip),%rbx
xor    %ebp,%ebp
jmp    11fe <read_port+0x9e>
nopw   0x0(%rax,%rax,1)
mov    (%r15),%rdi
add    %r13d,%ebp
mov    %ebp,%esi
call   1080 <realloc@plt>
mov    %rax,(%r15)
mov    %rax,%rdi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
mov    %r13d,%edx
call   1050 <strncat@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%edi
mov    %r14,%rsi
mov    $0x7,%edx
call   1060 <read@plt>
mov    %eax,%r13d
test   %eax,%eax
jle    122e <read_port+0xce>
xor    %eax,%eax
call   1070 <getDebug@plt>
cmp    $0x2,%eax
jl     11c0 <read_port+0x60>
mov    %r13d,%r12d
xor    %r14d,%r14d
movsbl 0x10(%rsp,%r14,1),%edx
mov    %rbx,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r14
cmp    %r14,%r12
jne    1210 <read_port+0xb0>
jmp    11c0 <read_port+0x60>
xor    %eax,%eax
call   1070 <getDebug@plt>
cmp    $0x2,%eax
jl     1246 <read_port+0xe6>
lea    0xdcb(%rip),%rdi
call   1030 <puts@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"I: Read port:\0A\00"
@.str.1 = [12 x i8] c"\09%02d: %#x\0A\00"
@.str.2 = [3 x i8] c"OK\00"
define void @read_port(i32* %0, i8** %1) {
%3 = alloca i32*
%4 = alloca i8**
%5 = alloca [8 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %3
store i8** %1, i8*** %4
store i32 0, i32* %8
%9 = call i32 (...) @getDebug()
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
br label %14
14: ; preds = %48, %13
%15 = load i32*, i32** %3
%16 = load i32, i32* %15
%17 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%18 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%19 = getelementptr inbounds i8, i8* %18, i64 8
%20 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%21 = ptrtoint i8* %19 to i64
%22 = ptrtoint i8* %20 to i64
%23 = sub i64 %21, %22
%24 = sub nsw i64 %23, 1
%25 = trunc i64 %24 to i32
%26 = call i32 @read(i32 %16, i8* %17, i32 %25)
store i32 %26, i32* %6
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %68
28: ; preds = %14
%29 = call i32 (...) @getDebug()
%30 = icmp sgt i32 %29, 1
br i1 %30, label %31, label %48
31: ; preds = %28
store i32 0, i32* %7
br label %32
32: ; preds = %44, %31
%33 = load i32, i32* %7
%34 = load i32, i32* %6
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %47
36: ; preds = %32
%37 = load i32, i32* %7
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %37, i32 %42)
br label %44
44: ; preds = %36
%45 = load i32, i32* %7
%46 = add nsw i32 %45, 1
store i32 %46, i32* %7
br label %32
47: ; preds = %32
br label %48
48: ; preds = %47, %28
%49 = load i8**, i8*** %4
%50 = load i8*, i8** %49
%51 = load i32, i32* %8
%52 = load i32, i32* %6
%53 = add nsw i32 %51, %52
%54 = sext i32 %53 to i64
%55 = mul i64 %54, 1
%56 = trunc i64 %55 to i32
%57 = call i64 @realloc(i8* %50, i32 %56)
%58 = inttoptr i64 %57 to i8*
%59 = load i8**, i8*** %4
store i8* %58, i8** %59
%60 = load i8**, i8*** %4
%61 = load i8*, i8** %60
%62 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
%63 = load i32, i32* %6
%64 = call i32 @strncat(i8* %61, i8* %62, i32 %63)
%65 = load i32, i32* %6
%66 = load i32, i32* %8
%67 = add nsw i32 %66, %65
store i32 %67, i32* %8
br label %14
68: ; preds = %14
%69 = call i32 (...) @getDebug()
%70 = icmp sgt i32 %69, 1
br i1 %70, label %71, label %73
71: ; preds = %68
%72 = call i32 @puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %73
73: ; preds = %71, %68
ret void
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"\09%02d: %#x\0A\00"
@.str.2 = [3 x i8] c"OK\00"
@str = [14 x i8] c"I: Read port:\00"
define void @read_port(i32* nocapture %0, i8** nocapture %1) {
%3 = alloca [8 x i8]
%4 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (...) @getDebug()
%6 = icmp sgt i32 %5, 1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = load i32, i32* %0
%11 = call i32 @read(i32 %10, i8* %4, i32 7)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %38
13: ; preds = %9, %29
%14 = phi i32 [ %36, %29 ], [ %11, %9 ]
%15 = phi i32 [ %31, %29 ], [ 0, %9 ]
%16 = call i32 (...) @getDebug()
%17 = icmp sgt i32 %16, 1
br i1 %17, label %18, label %29
18: ; preds = %13
%19 = zext i32 %14 to i64
br label %20
20: ; preds = %18, %20
%21 = phi i64 [ 0, %18 ], [ %27, %20 ]
%22 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = trunc i64 %21 to i32
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %25, i32 %24)
%27 = add nuw nsw i64 %21, 1
%28 = icmp eq i64 %27, %19
br i1 %28, label %29, label %20
29: ; preds = %20, %13
%30 = load i8*, i8** %1
%31 = add nsw i32 %14, %15
%32 = call i64 @realloc(i8* %30, i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %1
%34 = call i32 @strncat(i8* %33, i8* %4, i32 %14)
%35 = load i32, i32* %0
%36 = call i32 @read(i32 %35, i8* %4, i32 7)
%37 = icmp sgt i32 %36, 0
br i1 %37, label %13, label %38
38: ; preds = %29, %9
%39 = call i32 (...) @getDebug()
%40 = icmp sgt i32 %39, 1
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
ret void
}