#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 char uint8_t;
typedef int /*<<< orphan*/ uint16_t;
typedef int /*<<< orphan*/ time_t;
struct ts_eit {
  int dummy;
};

/* Variables and functions */
char *calloc(int, int);
int strlen(char *);
int /*<<< orphan*/ ts_LOGf(char *, ...);
int ts_eit_add_stream(struct ts_eit *, int /*<<< orphan*/, int,
                      int /*<<< orphan*/, int, char *, int);
int ts_eit_add_extended_event_descriptor(struct ts_eit *eit, uint16_t event_id,
                                         uint8_t running, time_t start_time,
                                         int duration_sec, char *text) {
  if (!text)
    return 0;
  char *txt;
  int desc_size = 2 + 3 + 1 + 1;
  int text_len = strlen(text);
  desc_size += 1 + text_len;
  if (desc_size > 257) {
    ts_LOGf("EIT extended event descriptor size > 255 is not supported (%d)!\n",
            desc_size);
    return 0;
  }
  if (text_len == 0) {
    ts_LOGf("EIT text_len == 0!\n");
    return 0;
  }
  uint8_t *desc = calloc(1, desc_size);
  int dpos = 0;
  desc[dpos + 0] = 0x4e;
  desc[dpos + 1] = desc_size - 2;
  desc[dpos + 2] = (0 << 4) | (0 & ~0xf0);
  desc[dpos + 3] = 'b';
  desc[dpos + 4] = 'u';
  desc[dpos + 5] = 'l';
  desc[dpos + 6] = 0;
  desc[dpos + 7] = text_len;
  dpos += 8;
  txt = text;
  while (txt[0]) {
    desc[dpos++] = txt[0];
    txt++;
  }
  return ts_eit_add_stream(eit, event_id, running ? 4 : 1, start_time,
                           duration_sec, desc, dpos);
}


x86 assembly (clang, -O0):
<ts_eit_add_extended_event_descriptor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %al,-0x15(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1176 <ts_eit_add_extended_event_descriptor+0x36>
movl   $0x0,-0x4(%rbp)
jmp    130c <ts_eit_add_extended_event_descriptor+0x1cc>
movl   $0x7,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x101,-0x34(%rbp)
jle    11bf <ts_eit_add_extended_event_descriptor+0x7f>
mov    -0x34(%rbp),%esi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1060 <ts_LOGf@plt>
movl   $0x0,-0x4(%rbp)
jmp    130c <ts_eit_add_extended_event_descriptor+0x1cc>
cmpl   $0x0,-0x38(%rbp)
jne    11e3 <ts_eit_add_extended_event_descriptor+0xa3>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1060 <ts_LOGf@plt>
movl   $0x0,-0x4(%rbp)
jmp    130c <ts_eit_add_extended_event_descriptor+0x1cc>
mov    -0x34(%rbp),%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    $0x0,%ecx
movslq %ecx,%rcx
movb   $0x4e,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
sub    $0x2,%eax
mov    %al,%dl
mov    -0x40(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
movb   $0x62,(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    $0x4,%ecx
movslq %ecx,%rcx
movb   $0x75,(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    $0x5,%ecx
movslq %ecx,%rcx
movb   $0x6c,(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    $0x6,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%eax
mov    %al,%dl
mov    -0x40(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    $0x7,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x44(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     12d8 <ts_eit_add_extended_event_descriptor+0x198>
mov    -0x30(%rbp),%rax
mov    (%rax),%dl
mov    -0x40(%rbp),%rax
mov    -0x44(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x44(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    129f <ts_eit_add_extended_event_descriptor+0x15f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
movsbl -0x15(%rbp),%ecx
mov    $0x1,%edx
mov    $0x4,%eax
cmp    $0x0,%ecx
cmovne %eax,%edx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x40(%rbp),%r9
mov    -0x44(%rbp),%eax
mov    %eax,(%rsp)
call   1050 <ts_eit_add_stream@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ts_eit_add_extended_event_descriptor>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %r9,%r9
je     11f0 <ts_eit_add_extended_event_descriptor+0xb0>
mov    %r9,%rbp
mov    %edx,%ebx
mov    %esi,%r12d
mov    %rdi,%r13
mov    %ecx,0x14(%rsp)
mov    %r8d,%r15d
mov    %r9,%rdi
call   1030 <strlen@plt>
mov    %eax,%r14d
lea    0x8(%r14),%esi
cmp    $0xfa,%eax
jl     1194 <ts_eit_add_extended_event_descriptor+0x54>
lea    0xe7a(%rip),%rdi
xor    %eax,%eax
call   1060 <ts_LOGf@plt>
xor    %eax,%eax
jmp    121e <ts_eit_add_extended_event_descriptor+0xde>
test   %r14d,%r14d
je     11e2 <ts_eit_add_extended_event_descriptor+0xa2>
mov    $0x1,%edi
call   1040 <calloc@plt>
movb   $0x4e,(%rax)
lea    0x6(%r14),%ecx
mov    %cl,0x1(%rax)
movl   $0x6c756200,0x2(%rax)
movb   $0x0,0x6(%rax)
mov    %r14b,0x7(%rax)
mov    0x0(%rbp),%dl
test   %dl,%dl
je     11f4 <ts_eit_add_extended_event_descriptor+0xb4>
mov    $0x8,%ecx
nopl   0x0(%rax,%rax,1)
mov    %dl,(%rax,%rcx,1)
movzbl -0x7(%rbp,%rcx,1),%edx
add    $0x1,%rcx
test   %dl,%dl
jne    11d0 <ts_eit_add_extended_event_descriptor+0x90>
jmp    11f9 <ts_eit_add_extended_event_descriptor+0xb9>
lea    0xe58(%rip),%rdi
xor    %eax,%eax
call   1060 <ts_LOGf@plt>
xor    %eax,%eax
jmp    121e <ts_eit_add_extended_event_descriptor+0xde>
mov    $0x8,%ecx
xor    %edx,%edx
test   %bl,%bl
setne  %dl
lea    (%rdx,%rdx,2),%edx
add    $0x1,%edx
mov    %ecx,(%rsp)
mov    %r13,%rdi
mov    %r12d,%esi
mov    0x14(%rsp),%ecx
mov    %r15d,%r8d
mov    %rax,%r9
call   1050 <ts_eit_add_stream@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ts_eit = type { i32 }
@.str = [65 x i8] c"EIT extended event descriptor size > 255 is not supported (%d)!\0A\00"
@.str.1 = [20 x i8] c"EIT text_len == 0!\0A\00"
define i32 @ts_eit_add_extended_event_descriptor(%struct.ts_eit* %0, i32 %1, i8 signext %2, i32 %3, i32 %4, i8* %5) {
%7 = alloca i32
%8 = alloca %struct.ts_eit*
%9 = alloca i32
%10 = alloca i8
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i8*
%18 = alloca i32
store %struct.ts_eit* %0, %struct.ts_eit** %8
store i32 %1, i32* %9
store i8 %2, i8* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i8* %5, i8** %13
%19 = load i8*, i8** %13
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %6
store i32 0, i32* %7
br label %119
22: ; preds = %6
store i32 7, i32* %15
%23 = load i8*, i8** %13
%24 = call i32 @strlen(i8* %23)
store i32 %24, i32* %16
%25 = load i32, i32* %16
%26 = add nsw i32 1, %25
%27 = load i32, i32* %15
%28 = add nsw i32 %27, %26
store i32 %28, i32* %15
%29 = load i32, i32* %15
%30 = icmp sgt i32 %29, 257
br i1 %30, label %31, label %34
31: ; preds = %22
%32 = load i32, i32* %15
%33 = call i32 (i8*, ...) @ts_LOGf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0), i32 %32)
store i32 0, i32* %7
br label %119
34: ; preds = %22
%35 = load i32, i32* %16
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 (i8*, ...) @ts_LOGf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %7
br label %119
39: ; preds = %34
%40 = load i32, i32* %15
%41 = call i8* @calloc(i32 1, i32 %40)
store i8* %41, i8** %17
store i32 0, i32* %18
%42 = load i8*, i8** %17
%43 = load i32, i32* %18
%44 = add nsw i32 %43, 0
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %42, i64 %45
store i8 78, i8* %46
%47 = load i32, i32* %15
%48 = sub nsw i32 %47, 2
%49 = trunc i32 %48 to i8
%50 = load i8*, i8** %17
%51 = load i32, i32* %18
%52 = add nsw i32 %51, 1
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %50, i64 %53
store i8 %49, i8* %54
%55 = load i8*, i8** %17
%56 = load i32, i32* %18
%57 = add nsw i32 %56, 2
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
store i8 0, i8* %59
%60 = load i8*, i8** %17
%61 = load i32, i32* %18
%62 = add nsw i32 %61, 3
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %60, i64 %63
store i8 98, i8* %64
%65 = load i8*, i8** %17
%66 = load i32, i32* %18
%67 = add nsw i32 %66, 4
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %65, i64 %68
store i8 117, i8* %69
%70 = load i8*, i8** %17
%71 = load i32, i32* %18
%72 = add nsw i32 %71, 5
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %70, i64 %73
store i8 108, i8* %74
%75 = load i8*, i8** %17
%76 = load i32, i32* %18
%77 = add nsw i32 %76, 6
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %75, i64 %78
store i8 0, i8* %79
%80 = load i32, i32* %16
%81 = trunc i32 %80 to i8
%82 = load i8*, i8** %17
%83 = load i32, i32* %18
%84 = add nsw i32 %83, 7
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8, i8* %82, i64 %85
store i8 %81, i8* %86
%87 = load i32, i32* %18
%88 = add nsw i32 %87, 8
store i32 %88, i32* %18
%89 = load i8*, i8** %13
store i8* %89, i8** %14
br label %90
90: ; preds = %95, %39
%91 = load i8*, i8** %14
%92 = getelementptr inbounds i8, i8* %91, i64 0
%93 = load i8, i8* %92
%94 = icmp ne i8 %93, 0
br i1 %94, label %95, label %106
95: ; preds = %90
%96 = load i8*, i8** %14
%97 = getelementptr inbounds i8, i8* %96, i64 0
%98 = load i8, i8* %97
%99 = load i8*, i8** %17
%100 = load i32, i32* %18
%101 = add nsw i32 %100, 1
store i32 %101, i32* %18
%102 = sext i32 %100 to i64
%103 = getelementptr inbounds i8, i8* %99, i64 %102
store i8 %98, i8* %103
%104 = load i8*, i8** %14
%105 = getelementptr inbounds i8, i8* %104, i32 1
store i8* %105, i8** %14
br label %90
106: ; preds = %90
%107 = load %struct.ts_eit*, %struct.ts_eit** %8
%108 = load i32, i32* %9
%109 = load i8, i8* %10
%110 = sext i8 %109 to i32
%111 = icmp ne i32 %110, 0
%112 = zext i1 %111 to i64
%113 = select i1 %111, i32 4, i32 1
%114 = load i32, i32* %11
%115 = load i32, i32* %12
%116 = load i8*, i8** %17
%117 = load i32, i32* %18
%118 = call i32 @ts_eit_add_stream(%struct.ts_eit* %107, i32 %108, i32 %113, i32 %114, i32 %115, i8* %116, i32 %117)
store i32 %118, i32* %7
br label %119
119: ; preds = %106, %37, %31, %21
%120 = load i32, i32* %7
ret i32 %120
}

LLVM IR (-O2):
%struct.ts_eit = type { i32 }
@.str = [65 x i8] c"EIT extended event descriptor size > 255 is not supported (%d)!\0A\00"
@.str.1 = [20 x i8] c"EIT text_len == 0!\0A\00"
define i32 @ts_eit_add_extended_event_descriptor(%struct.ts_eit* %0, i32 %1, i8 signext %2, i32 %3, i32 %4, i8* %5) {
%7 = icmp eq i8* %5, null
br i1 %7, label %47, label %8
8: ; preds = %6
%9 = tail call i32 @strlen(i8* %5)
%10 = add nsw i32 %9, 8
%11 = icmp sgt i32 %9, 249
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 (i8*, ...) @ts_LOGf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %47
14: ; preds = %8
%15 = icmp eq i32 %9, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = tail call i32 (i8*, ...) @ts_LOGf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %47
18: ; preds = %14
%19 = tail call i8* @calloc(i32 1, i32 %10)
store i8 78, i8* %19
%20 = trunc i32 %9 to i8
%21 = add i8 %20, 6
%22 = getelementptr inbounds i8, i8* %19, i64 1
store i8 %21, i8* %22
%23 = getelementptr inbounds i8, i8* %19, i64 2
store i8 0, i8* %23
%24 = getelementptr inbounds i8, i8* %19, i64 3
store i8 98, i8* %24
%25 = getelementptr inbounds i8, i8* %19, i64 4
store i8 117, i8* %25
%26 = getelementptr inbounds i8, i8* %19, i64 5
store i8 108, i8* %26
%27 = getelementptr inbounds i8, i8* %19, i64 6
store i8 0, i8* %27
%28 = getelementptr inbounds i8, i8* %19, i64 7
store i8 %20, i8* %28
%29 = load i8, i8* %5
%30 = icmp eq i8 %29, 0
br i1 %30, label %42, label %31
31: ; preds = %18, %31
%32 = phi i64 [ %35, %31 ], [ 8, %18 ]
%33 = phi i8 [ %38, %31 ], [ %29, %18 ]
%34 = phi i8* [ %37, %31 ], [ %5, %18 ]
%35 = add nuw i64 %32, 1
%36 = getelementptr inbounds i8, i8* %19, i64 %32
store i8 %33, i8* %36
%37 = getelementptr inbounds i8, i8* %34, i64 1
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 0
br i1 %39, label %40, label %31
40: ; preds = %31
%41 = trunc i64 %35 to i32
br label %42
42: ; preds = %40, %18
%43 = phi i32 [ 8, %18 ], [ %41, %40 ]
%44 = icmp eq i8 %2, 0
%45 = select i1 %44, i32 1, i32 4
%46 = tail call i32 @ts_eit_add_stream(%struct.ts_eit* %0, i32 %1, i32 %45, i32 %3, i32 %4, i8* %19, i32 %43)
br label %47
47: ; preds = %12, %16, %42, %6
%48 = phi i32 [ 0, %6 ], [ 0, %12 ], [ 0, %16 ], [ %46, %42 ]
ret i32 %48
}