#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*/ printf(char *, ...);
void write_as_text(char *message, int bytes_read) {
  int i;
  int j;
  int n_stations;
  char *ptr;
  printf("Message bytes=%d type=%d length=%d\n", bytes_read, message[0],
         message[1]);
  ptr = &message[2];
  n_stations = *ptr++;
  for (i = 0; i < n_stations; i++) {
    int station_id;
    int n_sensors;
    int sensor_type;
    int sensor_value;
    int age;
    station_id = *ptr++;
    n_sensors = *ptr++;
    printf("Station [%d]\n", station_id);
    for (j = 0; j < n_sensors; j++) {
      sensor_type = ptr[0];
      sensor_value = (ptr[2] << 8) + ptr[1];
      ptr += 3;
      printf("    sensor type %d = %d\n", sensor_type, sensor_value);
    }
    age = (ptr[1] << 8) + ptr[0];
    ptr += 2;
    printf("  [last message: %d secs ago]\n", age);
    printf("\n");
  }
  for (i = 0; i < message[1]; i++) {
    printf("%02x ", message[i + 2]);
    if (i % 16 == 15)
      printf("\n");
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<write_as_text>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%rax
movsbl (%rax),%edx
mov    -0x8(%rbp),%rax
movsbl 0x1(%rax),%ecx
lea    0xec8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movsbl (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1261 <write_as_text+0x151>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movsbl (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movsbl (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1211 <write_as_text+0x101>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
movsbl 0x2(%rax),%eax
shl    $0x8,%eax
mov    -0x20(%rbp),%rcx
movsbl 0x1(%rcx),%ecx
add    %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x20(%rbp)
mov    -0x2c(%rbp),%esi
mov    -0x30(%rbp),%edx
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b5 <write_as_text+0xa5>
mov    -0x20(%rbp),%rax
movsbl 0x1(%rax),%eax
shl    $0x8,%eax
mov    -0x20(%rbp),%rcx
movsbl (%rcx),%ecx
add    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x34(%rbp),%esi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1167 <write_as_text+0x57>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x8(%rbp),%rcx
movsbl 0x1(%rcx),%ecx
cmp    %ecx,%eax
jge    12cf <write_as_text+0x1bf>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0xf,%edx
jne    12bc <write_as_text+0x1ac>
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c1 <write_as_text+0x1b1>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1268 <write_as_text+0x158>
lea    0xd5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_as_text>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
movsbl (%rdi),%edx
movsbl 0x1(%rdi),%ecx
lea    0xec4(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movsbl 0x2(%r15),%r13d
test   %r13d,%r13d
jle    11fa <write_as_text+0xda>
lea    0x3(%r15),%rbp
lea    0xed6(%rip),%rbx
xor    %r14d,%r14d
jmp    11a2 <write_as_text+0x82>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl 0x1(%rbp),%eax
shl    $0x8,%eax
movsbl 0x0(%rbp),%esi
add    %eax,%esi
add    $0x2,%rbp
lea    0xec3(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%r14d
cmp    %r13d,%r14d
je     11fa <write_as_text+0xda>
movsbl 0x0(%rbp),%esi
movsbl 0x1(%rbp),%r12d
add    $0x2,%rbp
lea    0xe6e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
test   %r12d,%r12d
jle    1170 <write_as_text+0x50>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl 0x0(%rbp),%esi
movsbl 0x2(%rbp),%eax
shl    $0x8,%eax
movsbl 0x1(%rbp),%edx
add    %eax,%edx
add    $0x3,%rbp
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffff,%r12d
jne    11d0 <write_as_text+0xb0>
jmp    1170 <write_as_text+0x50>
cmpb   $0x0,0x1(%r15)
jle    1242 <write_as_text+0x122>
lea    0xe62(%rip),%rbx
xor    %ebp,%ebp
jmp    121e <write_as_text+0xfe>
nopl   0x0(%rax)
add    $0x1,%rbp
movsbq 0x1(%r15),%rax
cmp    %rax,%rbp
jge    1242 <write_as_text+0x122>
movsbl 0x2(%r15,%rbp,1),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
not    %eax
test   $0xf,%al
jne    1210 <write_as_text+0xf0>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1210 <write_as_text+0xf0>
mov    $0xa,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"Message bytes=%d type=%d length=%d\0A\00"
@.str.1 = [14 x i8] c"Station [%d]\0A\00"
@.str.2 = [25 x i8] c" sensor type %d = %d\0A\00"
@.str.3 = [31 x i8] c" [last message: %d secs ago]\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [6 x i8] c"%02x \00"
define void @write_as_text(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%14 = load i32, i32* %4
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = load i8*, i8** %3
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %18, i32 %22)
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 2
store i8* %25, i8** %8
%26 = load i8*, i8** %8
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %8
%28 = load i8, i8* %26
%29 = sext i8 %28 to i32
store i32 %29, i32* %7
store i32 0, i32* %5
br label %30
30: ; preds = %88, %2
%31 = load i32, i32* %5
%32 = load i32, i32* %7
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %91
34: ; preds = %30
%35 = load i8*, i8** %8
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %8
%37 = load i8, i8* %35
%38 = sext i8 %37 to i32
store i32 %38, i32* %9
%39 = load i8*, i8** %8
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %8
%41 = load i8, i8* %39
%42 = sext i8 %41 to i32
store i32 %42, i32* %10
%43 = load i32, i32* %9
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %43)
store i32 0, i32* %6
br label %45
45: ; preds = %69, %34
%46 = load i32, i32* %6
%47 = load i32, i32* %10
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %72
49: ; preds = %45
%50 = load i8*, i8** %8
%51 = getelementptr inbounds i8, i8* %50, i64 0
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
store i32 %53, i32* %11
%54 = load i8*, i8** %8
%55 = getelementptr inbounds i8, i8* %54, i64 2
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = shl i32 %57, 8
%59 = load i8*, i8** %8
%60 = getelementptr inbounds i8, i8* %59, i64 1
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = add nsw i32 %58, %62
store i32 %63, i32* %12
%64 = load i8*, i8** %8
%65 = getelementptr inbounds i8, i8* %64, i64 3
store i8* %65, i8** %8
%66 = load i32, i32* %11
%67 = load i32, i32* %12
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %66, i32 %67)
br label %69
69: ; preds = %49
%70 = load i32, i32* %6
%71 = add nsw i32 %70, 1
store i32 %71, i32* %6
br label %45
72: ; preds = %45
%73 = load i8*, i8** %8
%74 = getelementptr inbounds i8, i8* %73, i64 1
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = shl i32 %76, 8
%78 = load i8*, i8** %8
%79 = getelementptr inbounds i8, i8* %78, i64 0
%80 = load i8, i8* %79
%81 = sext i8 %80 to i32
%82 = add nsw i32 %77, %81
store i32 %82, i32* %13
%83 = load i8*, i8** %8
%84 = getelementptr inbounds i8, i8* %83, i64 2
store i8* %84, i8** %8
%85 = load i32, i32* %13
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %85)
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %88
88: ; preds = %72
%89 = load i32, i32* %5
%90 = add nsw i32 %89, 1
store i32 %90, i32* %5
br label %30
91: ; preds = %30
store i32 0, i32* %5
br label %92
92: ; preds = %114, %91
%93 = load i32, i32* %5
%94 = load i8*, i8** %3
%95 = getelementptr inbounds i8, i8* %94, i64 1
%96 = load i8, i8* %95
%97 = sext i8 %96 to i32
%98 = icmp slt i32 %93, %97
br i1 %98, label %99, label %117
99: ; preds = %92
%100 = load i8*, i8** %3
%101 = load i32, i32* %5
%102 = add nsw i32 %101, 2
%103 = sext i32 %102 to i64
%104 = getelementptr inbounds i8, i8* %100, i64 %103
%105 = load i8, i8* %104
%106 = sext i8 %105 to i32
%107 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %106)
%108 = load i32, i32* %5
%109 = srem i32 %108, 16
%110 = icmp eq i32 %109, 15
br i1 %110, label %111, label %113
111: ; preds = %99
%112 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %113
113: ; preds = %111, %99
br label %114
114: ; preds = %113
%115 = load i32, i32* %5
%116 = add nsw i32 %115, 1
store i32 %116, i32* %5
br label %92
117: ; preds = %92
%118 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [36 x i8] c"Message bytes=%d type=%d length=%d\0A\00"
@.str.1 = [14 x i8] c"Station [%d]\0A\00"
@.str.2 = [25 x i8] c" sensor type %d = %d\0A\00"
@.str.3 = [31 x i8] c" [last message: %d secs ago]\0A\00"
@.str.5 = [6 x i8] c"%02x \00"
define void @write_as_text(i8* nocapture %0, i32 %1) {
%3 = load i8, i8* %0
%4 = sext i8 %3 to i32
%5 = getelementptr inbounds i8, i8* %0, i64 1
%6 = load i8, i8* %5
%7 = sext i8 %6 to i32
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %4, i32 %7)
%9 = getelementptr inbounds i8, i8* %0, i64 2
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp sgt i8 %10, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = getelementptr inbounds i8, i8* %0, i64 3
br label %18
15: ; preds = %46, %2
%16 = load i8, i8* %5
%17 = icmp sgt i8 %16, 0
br i1 %17, label %60, label %76
18: ; preds = %13, %46
%19 = phi i32 [ %58, %46 ], [ 0, %13 ]
%20 = phi i8* [ %55, %46 ], [ %14, %13 ]
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = load i8, i8* %20
%23 = sext i8 %22 to i32
%24 = getelementptr inbounds i8, i8* %20, i64 2
%25 = load i8, i8* %21
%26 = sext i8 %25 to i32
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%28 = icmp sgt i8 %25, 0
br i1 %28, label %29, label %46
29: ; preds = %18, %29
%30 = phi i32 [ %44, %29 ], [ 0, %18 ]
%31 = phi i8* [ %42, %29 ], [ %24, %18 ]
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = getelementptr inbounds i8, i8* %31, i64 2
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = shl nsw i32 %36, 8
%38 = getelementptr inbounds i8, i8* %31, i64 1
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = add nsw i32 %37, %40
%42 = getelementptr inbounds i8, i8* %31, i64 3
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %33, i32 %41)
%44 = add nuw nsw i32 %30, 1
%45 = icmp eq i32 %44, %26
br i1 %45, label %46, label %29
46: ; preds = %29, %18
%47 = phi i8* [ %24, %18 ], [ %42, %29 ]
%48 = getelementptr inbounds i8, i8* %47, i64 1
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = shl nsw i32 %50, 8
%52 = load i8, i8* %47
%53 = sext i8 %52 to i32
%54 = add nsw i32 %51, %53
%55 = getelementptr inbounds i8, i8* %47, i64 2
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %54)
%57 = tail call i32 @putchar(i32 10)
%58 = add nuw nsw i32 %19, 1
%59 = icmp eq i32 %58, %11
br i1 %59, label %15, label %18
60: ; preds = %15, %71
%61 = phi i64 [ %72, %71 ], [ 0, %15 ]
%62 = add nuw nsw i64 %61, 2
%63 = getelementptr inbounds i8, i8* %0, i64 %62
%64 = load i8, i8* %63
%65 = sext i8 %64 to i32
%66 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %65)
%67 = and i64 %61, 15
%68 = icmp eq i64 %67, 15
br i1 %68, label %69, label %71
69: ; preds = %60
%70 = tail call i32 @putchar(i32 10)
br label %71
71: ; preds = %60, %69
%72 = add nuw nsw i64 %61, 1
%73 = load i8, i8* %5
%74 = sext i8 %73 to i64
%75 = icmp slt i64 %72, %74
br i1 %75, label %60, label %76
76: ; preds = %71, %15
%77 = tail call i32 @putchar(i32 10)
ret void
}