#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 */
char *calloc(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *memchr(char *, char, int);
int /*<<< orphan*/ memmove(char *, char *, int);
int /*<<< orphan*/ notify(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char *, int);
int read_notifications(char *buf, int len) {
  while (1) {
    char *msg;
    char *p = memchr(buf, '\n', len);
    if (p == NULL) {
      if (len >= 1024) {
        fprintf(stderr, "Notification too long!\n");
        return -1;
      }
      return len;
    }
    switch (buf[0]) {
    case 'C':
      msg = calloc(sizeof(*msg), p - buf);
      strncpy(msg, buf + 1, p - buf - 1);
      notify("Dazibao changed", msg);
      free(msg);
      break;
    case 'E':
      msg = calloc(sizeof(*msg), p - buf);
      strncpy(msg, buf + 1, p - buf - 1);
      notify("Error", msg);
      free(msg);
      break;
    default:
      fprintf(stderr, "Unknown notification type: %c.\n", buf[0]);
    }
    if (p + 1 >= buf + len) {
      return 0;
    } else {
      len -= p + 1 - buf;
      memmove(buf, p + 1, len);
    }
  }
  return len;
}


x86 assembly (clang, -O0):
<read_notifications>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    $0xa,%esi
call   1060 <memchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11da <read_notifications+0x6a>
cmpl   $0x400,-0x14(%rbp)
jl     11cf <read_notifications+0x5f>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1331 <read_notifications+0x1c1>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1331 <read_notifications+0x1c1>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x43,%eax
je     1203 <read_notifications+0x93>
jmp    11f2 <read_notifications+0x82>
mov    -0x2c(%rbp),%eax
sub    $0x45,%eax
je     125e <read_notifications+0xee>
jmp    12b9 <read_notifications+0x149>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    $0x1,%edi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rsi
lea    0xdcd(%rip),%rdi
call   1050 <notify@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    12d7 <read_notifications+0x167>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    $0x1,%edi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rsi
lea    0xd82(%rip),%rdi
call   1050 <notify@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    12d7 <read_notifications+0x167>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movsbl (%rax),%edx
lea    0xd5e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    -0x10(%rbp),%rcx
movslq -0x14(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jb     12ff <read_notifications+0x18f>
movl   $0x0,-0x4(%rbp)
jmp    1331 <read_notifications+0x1c1>
mov    -0x28(%rbp),%rcx
add    $0x1,%rcx
mov    -0x10(%rbp),%rax
sub    %rax,%rcx
movslq -0x14(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
mov    -0x14(%rbp),%edx
call   1090 <memmove@plt>
jmp    117f <read_notifications+0xf>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_notifications>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
mov    $0xa,%esi
mov    %r15d,%edx
call   1060 <memchr@plt>
test   %rax,%rax
je     1288 <read_notifications+0x118>
mov    %rax,%rbp
lea    0x1(%r14),%rax
mov    %rax,(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl (%r14),%edx
cmp    $0x45,%edx
je     11f0 <read_notifications+0x80>
cmp    $0x43,%edx
jne    1230 <read_notifications+0xc0>
mov    %ebp,%r13d
sub    %r14d,%r13d
mov    $0x1,%edi
mov    %r13d,%esi
call   1070 <calloc@plt>
mov    %rax,%r12
add    $0xffffffff,%r13d
mov    %rax,%rdi
mov    (%rsp),%rsi
mov    %r13d,%edx
call   1040 <strncpy@plt>
lea    0xe2a(%rip),%rdi
jmp    121c <read_notifications+0xac>
mov    %ebp,%ebx
sub    %r14d,%ebx
mov    $0x1,%edi
mov    %ebx,%esi
call   1070 <calloc@plt>
mov    %rax,%r12
add    $0xffffffff,%ebx
mov    %rax,%rdi
mov    (%rsp),%rsi
mov    %ebx,%edx
call   1040 <strncpy@plt>
lea    0xe0c(%rip),%rdi
mov    %r12,%rsi
call   1050 <notify@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1247 <read_notifications+0xd7>
xchg   %ax,%ax
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
add    $0x1,%rbp
movslq %r15d,%rax
add    %r14,%rax
cmp    %rax,%rbp
jae    12b0 <read_notifications+0x140>
mov    %r14d,%eax
sub    %ebp,%eax
add    %eax,%r15d
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1090 <memmove@plt>
mov    %r14,%rdi
mov    $0xa,%esi
mov    %r15d,%edx
call   1060 <memchr@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11b0 <read_notifications+0x40>
cmp    $0x400,%r15d
jl     12b3 <read_notifications+0x143>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xd5f(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%r15d
jmp    12b3 <read_notifications+0x143>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"Notification too long!\0A\00"
@.str.1 = [16 x i8] c"Dazibao changed\00"
@.str.2 = [6 x i8] c"Error\00"
@.str.3 = [32 x i8] c"Unknown notification type: %c.\0A\00"
define i32 @read_notifications(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
br label %8
8: ; preds = %2, %105
%9 = load i8*, i8** %4
%10 = load i32, i32* %5
%11 = call i8* @memchr(i8* %9, i8 signext 10, i32 %10)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %22
14: ; preds = %8
%15 = load i32, i32* %5
%16 = icmp sge i32 %15, 1024
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %106
20: ; preds = %14
%21 = load i32, i32* %5
store i32 %21, i32* %3
br label %106
22: ; preds = %8
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i64 0
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
switch i32 %26, label %73 [
i32 67, label %27
i32 69, label %50
]
27: ; preds = %22
%28 = load i8*, i8** %7
%29 = load i8*, i8** %4
%30 = ptrtoint i8* %28 to i64
%31 = ptrtoint i8* %29 to i64
%32 = sub i64 %30, %31
%33 = trunc i64 %32 to i32
%34 = call i8* @calloc(i32 1, i32 %33)
store i8* %34, i8** %6
%35 = load i8*, i8** %6
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i64 1
%38 = load i8*, i8** %7
%39 = load i8*, i8** %4
%40 = ptrtoint i8* %38 to i64
%41 = ptrtoint i8* %39 to i64
%42 = sub i64 %40, %41
%43 = sub nsw i64 %42, 1
%44 = trunc i64 %43 to i32
%45 = call i32 @strncpy(i8* %35, i8* %37, i32 %44)
%46 = load i8*, i8** %6
%47 = call i32 @notify(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %46)
%48 = load i8*, i8** %6
%49 = call i32 @free(i8* %48)
br label %80
50: ; preds = %22
%51 = load i8*, i8** %7
%52 = load i8*, i8** %4
%53 = ptrtoint i8* %51 to i64
%54 = ptrtoint i8* %52 to i64
%55 = sub i64 %53, %54
%56 = trunc i64 %55 to i32
%57 = call i8* @calloc(i32 1, i32 %56)
store i8* %57, i8** %6
%58 = load i8*, i8** %6
%59 = load i8*, i8** %4
%60 = getelementptr inbounds i8, i8* %59, i64 1
%61 = load i8*, i8** %7
%62 = load i8*, i8** %4
%63 = ptrtoint i8* %61 to i64
%64 = ptrtoint i8* %62 to i64
%65 = sub i64 %63, %64
%66 = sub nsw i64 %65, 1
%67 = trunc i64 %66 to i32
%68 = call i32 @strncpy(i8* %58, i8* %60, i32 %67)
%69 = load i8*, i8** %6
%70 = call i32 @notify(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %69)
%71 = load i8*, i8** %6
%72 = call i32 @free(i8* %71)
br label %80
73: ; preds = %22
%74 = load i32, i32* @stderr
%75 = load i8*, i8** %4
%76 = getelementptr inbounds i8, i8* %75, i64 0
%77 = load i8, i8* %76
%78 = sext i8 %77 to i32
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %78)
br label %80
80: ; preds = %73, %50, %27
%81 = load i8*, i8** %7
%82 = getelementptr inbounds i8, i8* %81, i64 1
%83 = load i8*, i8** %4
%84 = load i32, i32* %5
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8, i8* %83, i64 %85
%87 = icmp uge i8* %82, %86
br i1 %87, label %88, label %89
88: ; preds = %80
store i32 0, i32* %3
br label %106
89: ; preds = %80
%90 = load i8*, i8** %7
%91 = getelementptr inbounds i8, i8* %90, i64 1
%92 = load i8*, i8** %4
%93 = ptrtoint i8* %91 to i64
%94 = ptrtoint i8* %92 to i64
%95 = sub i64 %93, %94
%96 = load i32, i32* %5
%97 = sext i32 %96 to i64
%98 = sub nsw i64 %97, %95
%99 = trunc i64 %98 to i32
store i32 %99, i32* %5
%100 = load i8*, i8** %4
%101 = load i8*, i8** %7
%102 = getelementptr inbounds i8, i8* %101, i64 1
%103 = load i32, i32* %5
%104 = call i32 @memmove(i8* %100, i8* %102, i32 %103)
br label %105
105: ; preds = %89
br label %8
106: ; preds = %88, %20, %17
%107 = load i32, i32* %3
ret i32 %107
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"Notification too long!\0A\00"
@.str.1 = [16 x i8] c"Dazibao changed\00"
@.str.2 = [6 x i8] c"Error\00"
@.str.3 = [32 x i8] c"Unknown notification type: %c.\0A\00"
define i32 @read_notifications(i8* %0, i32 %1) {
%3 = tail call i8* @memchr(i8* %0, i8 signext 10, i32 %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = ptrtoint i8* %0 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 1
br label %14
8: ; preds = %45, %2
%9 = phi i32 [ %1, %2 ], [ %49, %45 ]
%10 = icmp sgt i32 %9, 1023
br i1 %10, label %11, label %53
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %53
14: ; preds = %5, %45
%15 = phi i8* [ %3, %5 ], [ %51, %45 ]
%16 = phi i32 [ %1, %5 ], [ %49, %45 ]
%17 = load i8, i8* %0
%18 = sext i8 %17 to i32
switch i32 %18, label %37 [
i32 67, label %19
i32 69, label %28
]
19: ; preds = %14
%20 = ptrtoint i8* %15 to i64
%21 = sub i64 %20, %6
%22 = trunc i64 %21 to i32
%23 = tail call i8* @calloc(i32 1, i32 %22)
%24 = add i32 %22, -1
%25 = tail call i32 @strncpy(i8* %23, i8* %7, i32 %24)
%26 = tail call i32 @notify(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%27 = tail call i32 @free(i8* %23)
br label %40
28: ; preds = %14
%29 = ptrtoint i8* %15 to i64
%30 = sub i64 %29, %6
%31 = trunc i64 %30 to i32
%32 = tail call i8* @calloc(i32 1, i32 %31)
%33 = add i32 %31, -1
%34 = tail call i32 @strncpy(i8* %32, i8* %7, i32 %33)
%35 = tail call i32 @notify(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%36 = tail call i32 @free(i8* %32)
br label %40
37: ; preds = %14
%38 = load i32, i32* @stderr
%39 = tail call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %18)
br label %40
40: ; preds = %37, %28, %19
%41 = getelementptr inbounds i8, i8* %15, i64 1
%42 = sext i32 %16 to i64
%43 = getelementptr inbounds i8, i8* %0, i64 %42
%44 = icmp ult i8* %41, %43
br i1 %44, label %45, label %53
45: ; preds = %40
%46 = ptrtoint i8* %41 to i64
%47 = sub i64 %6, %46
%48 = trunc i64 %47 to i32
%49 = add i32 %16, %48
%50 = tail call i32 @memmove(i8* %0, i8* %41, i32 %49)
%51 = tail call i8* @memchr(i8* %0, i8 signext 10, i32 %49)
%52 = icmp eq i8* %51, null
br i1 %52, label %8, label %14
53: ; preds = %40, %11, %8
%54 = phi i32 [ %9, %8 ], [ -1, %11 ], [ 0, %40 ]
ret i32 %54
}