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

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int read(int, char *, int);
int serialport_read_some(int fd, char *buf, int max_size) {
  char b[1];
  memset(buf, 0, sizeof(buf));
  int num_read;
  int stop_at = sizeof(buf);
  for (num_read = 0; num_read < stop_at; num_read++) {
    int n = read(fd, b, 1);
    if (n == -1) {
      num_read = 0;
      break;
    } else if (n == 0) {
      if (num_read > 0)
        num_read = num_read - 1;
      break;
    } else
      buf[num_read] = b[0];
  }
  buf[num_read + 1] = 0;
  return num_read;
}


x86 assembly (clang, -O0):
<serialport_read_some>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
movl   $0x8,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11ce <serialport_read_some+0xae>
mov    -0x4(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1186 <serialport_read_some+0x66>
movl   $0x0,-0x1c(%rbp)
jmp    11ce <serialport_read_some+0xae>
cmpl   $0x0,-0x24(%rbp)
jne    11a8 <serialport_read_some+0x88>
cmpl   $0x0,-0x1c(%rbp)
jle    11a3 <serialport_read_some+0x83>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ce <serialport_read_some+0xae>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11bb <serialport_read_some+0x9b>
jmp    11c0 <serialport_read_some+0xa0>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1150 <serialport_read_some+0x30>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serialport_read_some>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r14d
xor    %r15d,%r15d
mov    %rsi,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
lea    0x7(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     129e <serialport_read_some+0x17e>
mov    %eax,%ebp
test   %eax,%eax
je     12a1 <serialport_read_some+0x181>
mov    0x7(%rsp),%al
mov    %al,(%rbx)
lea    0x7(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     129e <serialport_read_some+0x17e>
mov    %eax,%ebp
test   %eax,%eax
je     12a1 <serialport_read_some+0x181>
mov    0x7(%rsp),%al
mov    %al,0x1(%rbx)
lea    0x7(%rsp),%rsi
mov    $0x1,%ebp
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     129e <serialport_read_some+0x17e>
test   %eax,%eax
je     12a1 <serialport_read_some+0x181>
mov    0x7(%rsp),%al
mov    %al,0x2(%rbx)
lea    0x7(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     129e <serialport_read_some+0x17e>
mov    $0x2,%ebp
test   %eax,%eax
je     12a1 <serialport_read_some+0x181>
mov    0x7(%rsp),%al
mov    %al,0x3(%rbx)
lea    0x7(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     129e <serialport_read_some+0x17e>
mov    $0x3,%ebp
test   %eax,%eax
je     12a1 <serialport_read_some+0x181>
mov    0x7(%rsp),%al
mov    %al,0x4(%rbx)
lea    0x7(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     129e <serialport_read_some+0x17e>
mov    $0x4,%ebp
test   %eax,%eax
je     12a1 <serialport_read_some+0x181>
mov    0x7(%rsp),%al
mov    %al,0x5(%rbx)
lea    0x7(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     129e <serialport_read_some+0x17e>
mov    $0x5,%ebp
test   %eax,%eax
je     12a1 <serialport_read_some+0x181>
mov    0x7(%rsp),%al
mov    %al,0x6(%rbx)
lea    0x7(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     129e <serialport_read_some+0x17e>
mov    $0x6,%ebp
test   %eax,%eax
je     12a1 <serialport_read_some+0x181>
mov    0x7(%rsp),%al
mov    %al,0x7(%rbx)
mov    $0x8,%r15d
mov    %r15d,%ebp
mov    %ebp,%eax
movb   $0x0,0x1(%rax,%rbx,1)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @serialport_read_some(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [1 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i8*, i8** %5
%12 = call i32 @memset(i8* %11, i32 0, i32 8)
store i32 8, i32* %9
store i32 0, i32* %8
br label %13
13: ; preds = %43, %3
%14 = load i32, i32* %8
%15 = load i32, i32* %9
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %46
17: ; preds = %13
%18 = load i32, i32* %4
%19 = getelementptr inbounds [1 x i8], [1 x i8]* %7, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 1)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %17
store i32 0, i32* %8
br label %46
24: ; preds = %17
%25 = load i32, i32* %10
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = load i32, i32* %8
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* %8
%32 = sub nsw i32 %31, 1
store i32 %32, i32* %8
br label %33
33: ; preds = %30, %27
br label %46
34: ; preds = %24
%35 = getelementptr inbounds [1 x i8], [1 x i8]* %7, i64 0, i64 0
%36 = load i8, i8* %35
%37 = load i8*, i8** %5
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 %36, i8* %40
br label %41
41: ; preds = %34
br label %42
42: ; preds = %41
br label %43
43: ; preds = %42
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
store i32 %45, i32* %8
br label %13
46: ; preds = %33, %23, %13
%47 = load i8*, i8** %5
%48 = load i32, i32* %8
%49 = add nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %47, i64 %50
store i8 0, i8* %51
%52 = load i32, i32* %8
ret i32 %52
}

LLVM IR (-O2):
define i32 @serialport_read_some(i32 %0, i8* %1, i32 %2) {
%4 = alloca [1 x i8]
%5 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @memset(i8* %1, i32 0, i32 8)
%7 = call i32 @read(i32 %0, i8* %5, i32 1)
switch i32 %7, label %8 [
i32 -1, label %38
i32 0, label %40
]
8: ; preds = %3
%9 = load i8, i8* %5
store i8 %9, i8* %1
%10 = call i32 @read(i32 %0, i8* %5, i32 1)
switch i32 %10, label %11 [
i32 -1, label %38
i32 0, label %40
]
11: ; preds = %8
%12 = load i8, i8* %5
%13 = getelementptr inbounds i8, i8* %1, i64 1
store i8 %12, i8* %13
%14 = call i32 @read(i32 %0, i8* %5, i32 1)
switch i32 %14, label %15 [
i32 -1, label %38
i32 0, label %40
]
15: ; preds = %11
%16 = load i8, i8* %5
%17 = getelementptr inbounds i8, i8* %1, i64 2
store i8 %16, i8* %17
%18 = call i32 @read(i32 %0, i8* %5, i32 1)
switch i32 %18, label %19 [
i32 -1, label %38
i32 0, label %40
]
19: ; preds = %15
%20 = load i8, i8* %5
%21 = getelementptr inbounds i8, i8* %1, i64 3
store i8 %20, i8* %21
%22 = call i32 @read(i32 %0, i8* %5, i32 1)
switch i32 %22, label %23 [
i32 -1, label %38
i32 0, label %40
]
23: ; preds = %19
%24 = load i8, i8* %5
%25 = getelementptr inbounds i8, i8* %1, i64 4
store i8 %24, i8* %25
%26 = call i32 @read(i32 %0, i8* %5, i32 1)
switch i32 %26, label %27 [
i32 -1, label %38
i32 0, label %40
]
27: ; preds = %23
%28 = load i8, i8* %5
%29 = getelementptr inbounds i8, i8* %1, i64 5
store i8 %28, i8* %29
%30 = call i32 @read(i32 %0, i8* %5, i32 1)
switch i32 %30, label %31 [
i32 -1, label %38
i32 0, label %40
]
31: ; preds = %27
%32 = load i8, i8* %5
%33 = getelementptr inbounds i8, i8* %1, i64 6
store i8 %32, i8* %33
%34 = call i32 @read(i32 %0, i8* %5, i32 1)
switch i32 %34, label %35 [
i32 -1, label %38
i32 0, label %40
]
35: ; preds = %31
%36 = load i8, i8* %5
%37 = getelementptr inbounds i8, i8* %1, i64 7
store i8 %36, i8* %37
br label %38
38: ; preds = %35, %31, %27, %23, %19, %15, %11, %8, %3
%39 = phi i32 [ 0, %3 ], [ 0, %8 ], [ 0, %11 ], [ 0, %15 ], [ 0, %19 ], [ 0, %23 ], [ 0, %27 ], [ 0, %31 ], [ 8, %35 ]
br label %40
40: ; preds = %3, %8, %11, %15, %19, %23, %27, %31, %38
%41 = phi i32 [ %39, %38 ], [ 6, %31 ], [ 5, %27 ], [ 4, %23 ], [ 3, %19 ], [ 2, %15 ], [ 1, %11 ], [ %10, %8 ], [ %7, %3 ]
%42 = add nuw nsw i32 %41, 1
%43 = zext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %1, i64 %43
store i8 0, i8* %44
ret i32 %41
}