#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*/ queueClose(int);
int queueGetNumMessages(int);
int queueOpen(int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
char *serializeQueues(int len, char *buf) {
  buf[0] = '\0';
  strcat(buf, "name,num");
  char *queues[5] = {"none", "control interface", "secure joiner", "zcb-in",
                     "dbp"};
  int i, num, queue;
  for (i = 1; i < 5; i++) {
    if ((queue = queueOpen(i, 0)) != -1) {
      num = queueGetNumMessages(queue);
      queueClose(queue);
    }
    char line[50];
    sprintf(line, ";%s,%d", queues[i], num);
    int l = strlen(line);
    if (len > l) {
      strcat(buf, line);
      len -= l;
    }
  }
  return ((len > 0) ? buf : NULL);
}


x86 assembly (clang, -O0):
<serializeQueues>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe6c(%rip),%rsi
call   1080 <strcat@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
lea    0xe59(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xe53(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xe5a(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe5d(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe59(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x44(%rbp)
cmpl   $0x5,-0x44(%rbp)
jge    1282 <serializeQueues+0x112>
mov    -0x44(%rbp),%edi
xor    %esi,%esi
call   1040 <queueOpen@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0xffffffff,%eax
je     121a <serializeQueues+0xaa>
mov    -0x4c(%rbp),%edi
call   1070 <queueGetNumMessages@plt>
mov    %eax,-0x48(%rbp)
mov    -0x4c(%rbp),%edi
call   1030 <queueClose@plt>
lea    -0x80(%rbp),%rdi
movslq -0x44(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdx
mov    -0x48(%rbp),%ecx
lea    0xe08(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x80(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x84(%rbp)
mov    -0x4(%rbp),%eax
cmp    -0x84(%rbp),%eax
jle    126f <serializeQueues+0xff>
mov    -0x10(%rbp),%rdi
lea    -0x80(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x84(%rbp),%ecx
mov    -0x4(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1274 <serializeQueues+0x104>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11e7 <serializeQueues+0x77>
cmpl   $0x0,-0x4(%rbp)
jle    129c <serializeQueues+0x12c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x90(%rbp)
jmp    12aa <serializeQueues+0x13a>
xor    %eax,%eax
mov    %rax,-0x90(%rbp)
jmp    12aa <serializeQueues+0x13a>
mov    -0x90(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serializeQueues>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
movb   $0x0,(%rsi)
lea    0xe87(%rip),%rsi
mov    %r14,%rdi
call   1070 <strcat@plt>
mov    $0x1,%edi
xor    %esi,%esi
call   1040 <queueOpen@plt>
cmp    $0xffffffff,%eax
je     11a7 <serializeQueues+0x47>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <queueGetNumMessages@plt>
mov    %eax,%r15d
mov    %ebx,%edi
call   1030 <queueClose@plt>
jmp    11a7 <serializeQueues+0x47>
lea    0xe86(%rip),%rsi
lea    0xe54(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15d,%ecx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %ebp,%eax
jge    11de <serializeQueues+0x7e>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %r14,%rdi
call   1070 <strcat@plt>
sub    %ebx,%ebp
mov    $0x2,%edi
xor    %esi,%esi
call   1040 <queueOpen@plt>
cmp    $0xffffffff,%eax
je     1202 <serializeQueues+0xa2>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <queueGetNumMessages@plt>
mov    %eax,%r15d
mov    %ebx,%edi
call   1030 <queueClose@plt>
lea    0xe2b(%rip),%rsi
lea    0xe0b(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15d,%ecx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%ebx
sub    %eax,%ebx
jle    1239 <serializeQueues+0xd9>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1070 <strcat@plt>
mov    %ebx,%ebp
mov    $0x3,%edi
xor    %esi,%esi
call   1040 <queueOpen@plt>
cmp    $0xffffffff,%eax
je     125d <serializeQueues+0xfd>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <queueGetNumMessages@plt>
mov    %eax,%r15d
mov    %ebx,%edi
call   1030 <queueClose@plt>
lea    0xdd0(%rip),%rsi
lea    0xdbe(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15d,%ecx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%ebx
sub    %eax,%ebx
jle    1294 <serializeQueues+0x134>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1070 <strcat@plt>
mov    %ebx,%ebp
mov    $0x4,%edi
xor    %esi,%esi
call   1040 <queueOpen@plt>
cmp    $0xffffffff,%eax
je     12b8 <serializeQueues+0x158>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <queueGetNumMessages@plt>
mov    %eax,%r15d
mov    %ebx,%edi
call   1030 <queueClose@plt>
lea    0xd75(%rip),%rsi
lea    0xd6a(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15d,%ecx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%ebx
sub    %eax,%ebx
jle    12ef <serializeQueues+0x18f>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1070 <strcat@plt>
mov    %ebx,%ebp
xor    %eax,%eax
test   %ebp,%ebp
cmovg  %r14,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"name,num\00"
@.str.1 = [5 x i8] c"none\00"
@.str.2 = [18 x i8] c"control interface\00"
@.str.3 = [14 x i8] c"secure joiner\00"
@.str.4 = [7 x i8] c"zcb-in\00"
@.str.5 = [4 x i8] c"dbp\00"
@.str.6 = [7 x i8] c";%s,%d\00"
define i8* @serializeQueues(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [5 x i8*]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [50 x i8]
%10 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i64 0
store i8 0, i8* %12
%13 = load i8*, i8** %4
%14 = call i32 @strcat(i8* %13, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%15 = bitcast [5 x i8*]* %5 to i8*
%16 = bitcast i8* %15 to [5 x i8*]*
%17 = getelementptr inbounds [5 x i8*], [5 x i8*]* %16, i32 0, i32 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i32 0, i32 0), i8** %17
%18 = getelementptr inbounds [5 x i8*], [5 x i8*]* %16, i32 0, i32 1
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i32 0, i32 0), i8** %18
%19 = getelementptr inbounds [5 x i8*], [5 x i8*]* %16, i32 0, i32 2
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i32 0, i32 0), i8** %19
%20 = getelementptr inbounds [5 x i8*], [5 x i8*]* %16, i32 0, i32 3
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i32 0, i32 0), i8** %20
%21 = getelementptr inbounds [5 x i8*], [5 x i8*]* %16, i32 0, i32 4
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i32 0, i32 0), i8** %21
store i32 1, i32* %6
br label %22
22: ; preds = %55, %2
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 5
br i1 %24, label %25, label %58
25: ; preds = %22
%26 = load i32, i32* %6
%27 = call i32 @queueOpen(i32 %26, i32 0)
store i32 %27, i32* %8
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i32, i32* %8
%31 = call i32 @queueGetNumMessages(i32 %30)
store i32 %31, i32* %7
%32 = load i32, i32* %8
%33 = call i32 @queueClose(i32 %32)
br label %34
34: ; preds = %29, %25
%35 = getelementptr inbounds [50 x i8], [50 x i8]* %9, i64 0, i64 0
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [5 x i8*], [5 x i8*]* %5, i64 0, i64 %37
%39 = load i8*, i8** %38
%40 = load i32, i32* %7
%41 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* %39, i32 %40)
%42 = getelementptr inbounds [50 x i8], [50 x i8]* %9, i64 0, i64 0
%43 = call i32 @strlen(i8* %42)
store i32 %43, i32* %10
%44 = load i32, i32* %3
%45 = load i32, i32* %10
%46 = icmp sgt i32 %44, %45
br i1 %46, label %47, label %54
47: ; preds = %34
%48 = load i8*, i8** %4
%49 = getelementptr inbounds [50 x i8], [50 x i8]* %9, i64 0, i64 0
%50 = call i32 @strcat(i8* %48, i8* %49)
%51 = load i32, i32* %10
%52 = load i32, i32* %3
%53 = sub nsw i32 %52, %51
store i32 %53, i32* %3
br label %54
54: ; preds = %47, %34
br label %55
55: ; preds = %54
%56 = load i32, i32* %6
%57 = add nsw i32 %56, 1
store i32 %57, i32* %6
br label %22
58: ; preds = %22
%59 = load i32, i32* %3
%60 = icmp sgt i32 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %58
%62 = load i8*, i8** %4
br label %64
63: ; preds = %58
br label %64
64: ; preds = %63, %61
%65 = phi i8* [ %62, %61 ], [ null, %63 ]
ret i8* %65
}

LLVM IR (-O2):
@.str = [9 x i8] c"name,num\00"
@.str.2 = [18 x i8] c"control interface\00"
@.str.3 = [14 x i8] c"secure joiner\00"
@.str.4 = [7 x i8] c"zcb-in\00"
@.str.5 = [4 x i8] c"dbp\00"
@.str.6 = [7 x i8] c";%s,%d\00"
define i8* @serializeQueues(i32 %0, i8* %1) {
%3 = alloca [50 x i8]
store i8 0, i8* %1
%4 = tail call i32 @strcat(i8* %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%5 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%6 = call i32 @queueOpen(i32 1, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = call i32 @queueGetNumMessages(i32 %6)
%10 = call i32 @queueClose(i32 %6)
br label %11
11: ; preds = %8, %2
%12 = phi i32 [ %9, %8 ], [ undef, %2 ]
%13 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = call i32 @strlen(i8* %5)
%15 = icmp slt i32 %14, %0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = call i32 @strcat(i8* %1, i8* %5)
%18 = sub nsw i32 %0, %14
br label %19
19: ; preds = %16, %11
%20 = phi i32 [ %18, %16 ], [ %0, %11 ]
%21 = call i32 @queueOpen(i32 2, i32 0)
%22 = icmp eq i32 %21, -1
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = call i32 @queueGetNumMessages(i32 %21)
%25 = call i32 @queueClose(i32 %21)
br label %26
26: ; preds = %23, %19
%27 = phi i32 [ %24, %23 ], [ %12, %19 ]
%28 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = call i32 @strlen(i8* %5)
%30 = icmp sgt i32 %20, %29
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = call i32 @strcat(i8* %1, i8* %5)
%33 = sub nsw i32 %20, %29
br label %34
34: ; preds = %31, %26
%35 = phi i32 [ %33, %31 ], [ %20, %26 ]
%36 = call i32 @queueOpen(i32 3, i32 0)
%37 = icmp eq i32 %36, -1
br i1 %37, label %41, label %38
38: ; preds = %34
%39 = call i32 @queueGetNumMessages(i32 %36)
%40 = call i32 @queueClose(i32 %36)
br label %41
41: ; preds = %38, %34
%42 = phi i32 [ %39, %38 ], [ %27, %34 ]
%43 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %42)
%44 = call i32 @strlen(i8* %5)
%45 = icmp sgt i32 %35, %44
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = call i32 @strcat(i8* %1, i8* %5)
%48 = sub nsw i32 %35, %44
br label %49
49: ; preds = %46, %41
%50 = phi i32 [ %48, %46 ], [ %35, %41 ]
%51 = call i32 @queueOpen(i32 4, i32 0)
%52 = icmp eq i32 %51, -1
br i1 %52, label %56, label %53
53: ; preds = %49
%54 = call i32 @queueGetNumMessages(i32 %51)
%55 = call i32 @queueClose(i32 %51)
br label %56
56: ; preds = %53, %49
%57 = phi i32 [ %54, %53 ], [ %42, %49 ]
%58 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %57)
%59 = call i32 @strlen(i8* %5)
%60 = icmp sgt i32 %50, %59
br i1 %60, label %61, label %64
61: ; preds = %56
%62 = call i32 @strcat(i8* %1, i8* %5)
%63 = sub nsw i32 %50, %59
br label %64
64: ; preds = %61, %56
%65 = phi i32 [ %63, %61 ], [ %50, %56 ]
%66 = icmp sgt i32 %65, 0
%67 = select i1 %66, i8* %1, i8* null
ret i8* %67
}