#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*/ B115200;
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
char *calloc(int, int);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ set_blocking(int, int /*<<< orphan*/);
int /*<<< orphan*/ set_interface_attribs(int, int /*<<< orphan*/,
                                         int /*<<< orphan*/);
int sprintf(char *, char *, unsigned int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int /*<<< orphan*/ write(int, char *, int);
char *cubeInfo(char *port, unsigned int packetSize) {
  int fd = open(port, O_RDWR | O_NOCTTY | O_SYNC);
  if (fd < 0) {
    fprintf(stderr, "error %d opening %s: %s\n", errno, port, strerror(errno));
    exit(1);
  }
  set_interface_attribs(fd, B115200, 0);
  set_blocking(fd, 0);
  char sizeBuffer[16];
  int numChars = sprintf(sizeBuffer, "%d", packetSize);
  write(fd, sizeBuffer, numChars);
  write(fd, "\r", 1);
  char c;
  char *addressBuffer = calloc(15, sizeof(char));
  int rxCount = 0;
  for (;;) {
    int len = read(fd, &c, 1);
    if (c == '\n' || c == '\r')
      break;
    if (len > 0) {
      if (rxCount < 15) {
        addressBuffer[rxCount] = c;
      } else {
        if (rxCount >= sizeof addressBuffer + 1) {
          fprintf(stderr, "Corrupted serial message? \"%s\"", addressBuffer);
          exit(1);
        }
      }
      rxCount++;
    }
  }
  close(fd);
  return addressBuffer;
}


x86 assembly (clang, -O0):
<cubeInfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    0x2dfd(%rip),%rax
or     (%rax),%esi
mov    0x2e04(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1243 <cubeInfo+0x93>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    -0x44(%rbp),%edx
mov    -0x40(%rbp),%rcx
mov    %rax,%r8
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%edi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <set_interface_attribs@plt>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1050 <set_blocking@plt>
lea    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xdab(%rip),%rsi
call   10b0 <sprintf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%edi
lea    0xd8d(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0xf,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x25(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,-0x38(%rbp)
movsbl -0x25(%rbp),%eax
cmp    $0xa,%eax
je     12e1 <cubeInfo+0x131>
movsbl -0x25(%rbp),%eax
cmp    $0xd,%eax
jne    12e6 <cubeInfo+0x136>
jmp    1353 <cubeInfo+0x1a3>
cmpl   $0x0,-0x38(%rbp)
jle    134e <cubeInfo+0x19e>
cmpl   $0xf,-0x34(%rbp)
jge    130d <cubeInfo+0x15d>
mov    -0x25(%rbp),%dl
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1345 <cubeInfo+0x195>
movslq -0x34(%rbp),%rax
cmp    $0x9,%rax
jb     1340 <cubeInfo+0x190>
mov    0x2ca6(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xcef(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1345 <cubeInfo+0x195>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12b3 <cubeInfo+0x103>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0x30(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cubeInfo>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    0x2e25(%rip),%rcx
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e05(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   10a0 <open@plt>
test   %eax,%eax
js     12ac <cubeInfo+0xfc>
mov    %eax,%ebx
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
xor    %r12d,%r12d
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <set_interface_attribs@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <set_blocking@plt>
lea    0xe06(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r14d,%edx
call   10b0 <sprintf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xde6(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0xf,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%r14
lea    0xf(%rsp),%r15
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movzbl 0xf(%rsp),%ecx
cmp    $0xa,%cl
je     1295 <cubeInfo+0xe5>
cmp    $0xd,%cl
je     1295 <cubeInfo+0xe5>
test   %eax,%eax
jle    1260 <cubeInfo+0xb0>
cmp    $0xe,%r12d
ja     12e9 <cubeInfo+0x139>
mov    %r12d,%eax
mov    %cl,(%r14,%rax,1)
add    $0x1,%r12d
jmp    1260 <cubeInfo+0xb0>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d15(%rip),%rax
mov    (%rax),%r14d
mov    0x2d03(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10d0 <strerror@plt>
lea    0xd33(%rip),%rsi
mov    %r14d,%edi
mov    %ebx,%edx
mov    %rbp,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
lea    0xd25(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"error %d opening %s: %s\0A\00"
@errno = global i32 0
@B115200 = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [2 x i8] c"\0D\00"
@.str.3 = [31 x i8] c"Corrupted serial message? \22%s\22\00"
define i8* @cubeInfo(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [16 x i8]
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%12 = load i8*, i8** %3
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_NOCTTY
%15 = or i32 %13, %14
%16 = load i32, i32* @O_SYNC
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %12, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = load i8*, i8** %3
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %23, i8* %24, i8* %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %2
%30 = load i32, i32* %5
%31 = load i32, i32* @B115200
%32 = call i32 @set_interface_attribs(i32 %30, i32 %31, i32 0)
%33 = load i32, i32* %5
%34 = call i32 @set_blocking(i32 %33, i32 0)
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%36 = load i32, i32* %4
%37 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %36)
store i32 %37, i32* %7
%38 = load i32, i32* %5
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%40 = load i32, i32* %7
%41 = call i32 @write(i32 %38, i8* %39, i32 %40)
%42 = load i32, i32* %5
%43 = call i32 @write(i32 %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%44 = call i8* @calloc(i32 15, i32 1)
store i8* %44, i8** %9
store i32 0, i32* %10
br label %45
45: ; preds = %81, %29
%46 = load i32, i32* %5
%47 = call i32 @read(i32 %46, i8* %8, i32 1)
store i32 %47, i32* %11
%48 = load i8, i8* %8
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 10
br i1 %50, label %55, label %51
51: ; preds = %45
%52 = load i8, i8* %8
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 13
br i1 %54, label %55, label %56
55: ; preds = %51, %45
br label %82
56: ; preds = %51
%57 = load i32, i32* %11
%58 = icmp sgt i32 %57, 0
br i1 %58, label %59, label %81
59: ; preds = %56
%60 = load i32, i32* %10
%61 = icmp slt i32 %60, 15
br i1 %61, label %62, label %68
62: ; preds = %59
%63 = load i8, i8* %8
%64 = load i8*, i8** %9
%65 = load i32, i32* %10
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
store i8 %63, i8* %67
br label %78
68: ; preds = %59
%69 = load i32, i32* %10
%70 = sext i32 %69 to i64
%71 = icmp uge i64 %70, 9
br i1 %71, label %72, label %77
72: ; preds = %68
%73 = load i32, i32* @stderr
%74 = load i8*, i8** %9
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %74)
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %68
br label %78
78: ; preds = %77, %62
%79 = load i32, i32* %10
%80 = add nsw i32 %79, 1
store i32 %80, i32* %10
br label %81
81: ; preds = %78, %56
br label %45
82: ; preds = %55
%83 = load i32, i32* %5
%84 = call i32 @close(i32 %83)
%85 = load i8*, i8** %9
ret i8* %85
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@stderr = global i32 0
@.str = [25 x i8] c"error %d opening %s: %s\0A\00"
@errno = global i32 0
@B115200 = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [2 x i8] c"\0D\00"
@.str.3 = [31 x i8] c"Corrupted serial message? \22%s\22\00"
define i8* @cubeInfo(i8* %0, i32 %1) {
%3 = alloca [16 x i8]
%4 = alloca i8
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = load i32, i32* @O_SYNC
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %14, i8* %0, i8* %15)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @B115200
%20 = tail call i32 @set_interface_attribs(i32 %10, i32 %19, i32 0)
%21 = tail call i32 @set_blocking(i32 %10, i32 0)
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%23 = call i32 @sprintf(i8* %22, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%24 = call i32 @write(i32 %10, i8* %22, i32 %23)
%25 = tail call i32 @write(i32 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%26 = tail call dereferenceable_or_null(15) i8* @calloc(i32 15, i32 1)
br label %27
27: ; preds = %36, %18
%28 = phi i32 [ %39, %36 ], [ 0, %18 ]
br label %29
29: ; preds = %27, %32
%30 = call i32 @read(i32 %10, i8* %4, i32 1)
%31 = load i8, i8* %4
switch i8 %31, label %32 [
i8 13, label %44
i8 10, label %44
]
32: ; preds = %29
%33 = icmp sgt i32 %30, 0
br i1 %33, label %34, label %29
34: ; preds = %32
%35 = icmp ult i32 %28, 15
br i1 %35, label %36, label %40
36: ; preds = %34
%37 = zext i32 %28 to i64
%38 = getelementptr inbounds i8, i8* %26, i64 %37
store i8 %31, i8* %38
%39 = add nuw nsw i32 %28, 1
br label %27
40: ; preds = %34
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%43 = tail call i32 @exit(i32 1)
unreachable
44: ; preds = %29, %29
%45 = tail call i32 @close(i32 %10)
ret i8* %26
}