#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 int /*<<< orphan*/ buf;

/* Variables and functions */
int LOG_DEBUG;
int LOG_ERR;
int LOG_INFO;
int /*<<< orphan*/ LOG_PID;
int /*<<< orphan*/ LOG_USER;
int /*<<< orphan*/ exit(int);
char *malloc(int);
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read_msg(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ syslog(int, char *, char *);
int /*<<< orphan*/ write_msg(char *, int);
int main() {
  char buf[1024];
  char *myname;
  int len;
  len = read_msg(buf, sizeof(buf));
  if (len < 3 || len >= sizeof(buf)) {
    write_msg("Could not get identification string", 35);
    exit(1);
  }
  buf[len] = 0;
  len = strlen(buf) + 4;
  if ((myname = malloc(len)) == NULL) {
    snprintf(
        buf, sizeof(buf),
        "Failed allocating %i bytes of memory for the identification string\n",
        len);
    buf[sizeof(buf) - 1] = 0;
    write_msg(buf, strlen(buf));
    exit(1);
  }
  strcpy(myname, "yxa/");
  strcpy(myname + 4, buf);
  openlog(myname, LOG_PID, LOG_USER);
  while ((len = read_msg(buf, sizeof(buf))) > 0) {
    int t = sizeof(buf);
    int prio = LOG_DEBUG;
    if (len < t)
      t = len;
    buf[t] = 0;
    switch (*buf) {
    case 'i':
      prio = LOG_INFO;
    case 'e':
      prio = LOG_ERR;
    }
    syslog(prio, "%s", buf + 1);
    buf[0] = 0;
    buf[1] = 0;
  }
  write_msg("Failed reading message", 22);
  printf("NOT REACHED\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   10c0 <read_msg@plt>
mov    %eax,-0x41c(%rbp)
cmpl   $0x3,-0x41c(%rbp)
jl     11e9 <main+0x49>
movslq -0x41c(%rbp),%rax
cmp    $0x400,%rax
jb     1204 <main+0x64>
lea    0xe10(%rip),%rdi
mov    $0x23,%esi
call   1090 <write_msg@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movslq -0x41c(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x4,%eax
mov    %eax,-0x41c(%rbp)
mov    -0x41c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
jne    1298 <main+0xf8>
lea    -0x410(%rbp),%rdi
mov    -0x41c(%rbp),%ecx
mov    $0x400,%esi
lea    0xdc7(%rip),%rdx
call   1060 <snprintf@plt>
movb   $0x0,-0x11(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x430(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x430(%rbp),%rdi
mov    %eax,%esi
call   1090 <write_msg@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x418(%rbp),%rdi
lea    0xdc2(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x418(%rbp),%rdi
add    $0x4,%rdi
lea    -0x410(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x418(%rbp),%rdi
mov    0x2d08(%rip),%rax
mov    (%rax),%esi
mov    0x2d0f(%rip),%rax
mov    (%rax),%edx
call   10a0 <openlog@plt>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   10c0 <read_msg@plt>
mov    %eax,-0x41c(%rbp)
cmp    $0x0,%eax
jle    13c3 <main+0x223>
movl   $0x400,-0x420(%rbp)
mov    0x2cbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x424(%rbp)
mov    -0x41c(%rbp),%eax
cmp    -0x420(%rbp),%eax
jge    1337 <main+0x197>
mov    -0x41c(%rbp),%eax
mov    %eax,-0x420(%rbp)
movslq -0x420(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
movsbl -0x410(%rbp),%eax
mov    %eax,-0x434(%rbp)
sub    $0x65,%eax
je     1384 <main+0x1e4>
jmp    1361 <main+0x1c1>
mov    -0x434(%rbp),%eax
sub    $0x69,%eax
jne    1393 <main+0x1f3>
jmp    1375 <main+0x1d5>
mov    0x2c4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x424(%rbp)
mov    0x2c2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x424(%rbp)
mov    -0x424(%rbp),%edi
lea    -0x410(%rbp),%rdx
add    $0x1,%rdx
lea    0xcc2(%rip),%rsi
call   1080 <syslog@plt>
movb   $0x0,-0x410(%rbp)
movb   $0x0,-0x40f(%rbp)
jmp    12e0 <main+0x140>
lea    0xca6(%rip),%rdi
mov    $0x16,%esi
call   1090 <write_msg@plt>
lea    0xcac(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsp,%rdi
mov    $0x400,%esi
call   10c0 <read_msg@plt>
lea    -0x400(%rax),%ecx
cmp    $0xfffffc02,%ecx
ja     11da <main+0x3a>
lea    0xe2d(%rip),%rdi
mov    $0x23,%esi
jmp    1227 <main+0x87>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
add    $0x4,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
test   %rax,%rax
jne    1236 <main+0x96>
lea    0xe24(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %ebp,%ecx
call   1060 <snprintf@plt>
movb   $0x0,0x3ff(%rsp)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1090 <write_msg@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %rax,%rbx
lea    0xe28(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
add    $0x4,%rdi
mov    %rsp,%r14
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   10a0 <openlog@plt>
mov    $0x400,%r12d
mov    %r14,%rdi
mov    $0x400,%esi
call   10c0 <read_msg@plt>
test   %eax,%eax
jle    12f6 <main+0x156>
lea    0x1(%rsp),%r14
mov    0x2d39(%rip),%rbp
mov    0x2d22(%rip),%r13
lea    0xdc8(%rip),%r15
mov    %rsp,%rbx
jmp    12d6 <main+0x136>
nopw   0x0(%rax,%rax,1)
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1080 <syslog@plt>
movw   $0x0,(%rsp)
mov    %rbx,%rdi
mov    $0x400,%esi
call   10c0 <read_msg@plt>
test   %eax,%eax
jle    12f6 <main+0x156>
cmp    $0x400,%eax
cmovae %r12d,%eax
mov    0x0(%rbp),%edi
movb   $0x0,(%rsp,%rax,1)
movsbl (%rsp),%eax
cmp    $0x69,%eax
je     12b0 <main+0x110>
cmp    $0x65,%eax
je     12b0 <main+0x110>
jmp    12b4 <main+0x114>
lea    0xd73(%rip),%rdi
mov    $0x16,%esi
call   1090 <write_msg@plt>
lea    0xd79(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"Could not get identification string\00"
@.str.1 = [68 x i8] c"Failed allocating %i bytes of memory for the identification string\0A\00"
@.str.2 = [5 x i8] c"yxa/\00"
@LOG_PID = global i32 0
@LOG_USER = global i32 0
@LOG_DEBUG = global i32 0
@LOG_INFO = global i32 0
@LOG_ERR = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [23 x i8] c"Failed reading message\00"
@.str.5 = [13 x i8] c"NOT REACHED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%8 = call i32 @read_msg(i8* %7, i32 1024)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %15, label %11
11: ; preds = %0
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = icmp uge i64 %13, 1024
br i1 %14, label %15, label %18
15: ; preds = %11, %0
%16 = call i32 @write_msg(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 35)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %20
store i8 0, i8* %21
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
%24 = add nsw i32 %23, 4
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %3
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %38
28: ; preds = %18
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%30 = load i32, i32* %4
%31 = call i32 @snprintf(i8* %29, i32 1024, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 1023
store i8 0, i8* %32
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%35 = call i32 @strlen(i8* %34)
%36 = call i32 @write_msg(i8* %33, i32 %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %18
%39 = load i8*, i8** %3
%40 = call i32 @strcpy(i8* %39, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%41 = load i8*, i8** %3
%42 = getelementptr inbounds i8, i8* %41, i64 4
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%44 = call i32 @strcpy(i8* %42, i8* %43)
%45 = load i8*, i8** %3
%46 = load i32, i32* @LOG_PID
%47 = load i32, i32* @LOG_USER
%48 = call i32 @openlog(i8* %45, i32 %46, i32 %47)
br label %49
49: ; preds = %71, %38
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%51 = call i32 @read_msg(i8* %50, i32 1024)
store i32 %51, i32* %4
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %78
53: ; preds = %49
store i32 1024, i32* %5
%54 = load i32, i32* @LOG_DEBUG
store i32 %54, i32* %6
%55 = load i32, i32* %4
%56 = load i32, i32* %5
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = load i32, i32* %4
store i32 %59, i32* %5
br label %60
60: ; preds = %58, %53
%61 = load i32, i32* %5
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %62
store i8 0, i8* %63
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
switch i32 %66, label %71 [
i32 105, label %67
i32 101, label %69
]
67: ; preds = %60
%68 = load i32, i32* @LOG_INFO
store i32 %68, i32* %6
br label %69
69: ; preds = %60, %67
%70 = load i32, i32* @LOG_ERR
store i32 %70, i32* %6
br label %71
71: ; preds = %69, %60
%72 = load i32, i32* %6
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%74 = getelementptr inbounds i8, i8* %73, i64 1
%75 = call i32 @syslog(i32 %72, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %74)
%76 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
store i8 0, i8* %76
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 1
store i8 0, i8* %77
br label %49
78: ; preds = %49
%79 = call i32 @write_msg(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 22)
%80 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%81 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [36 x i8] c"Could not get identification string\00"
@.str.1 = [68 x i8] c"Failed allocating %i bytes of memory for the identification string\0A\00"
@.str.2 = [5 x i8] c"yxa/\00"
@LOG_PID = global i32 0
@LOG_USER = global i32 0
@LOG_DEBUG = global i32 0
@LOG_INFO = global i32 0
@LOG_ERR = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [23 x i8] c"Failed reading message\00"
@str = [12 x i8] c"NOT REACHED\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = call i32 @read_msg(i8* %2, i32 1024)
%4 = add i32 %3, -1024
%5 = icmp ult i32 %4, -1021
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @write_msg(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 35)
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = zext i32 %3 to i64
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %10
store i8 0, i8* %11
%12 = call i32 @strlen(i8* %2)
%13 = add nsw i32 %12, 4
%14 = call i8* @malloc(i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %22
16: ; preds = %9
%17 = call i32 @snprintf(i8* %2, i32 1024, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 1023
store i8 0, i8* %18
%19 = call i32 @strlen(i8* %2)
%20 = call i32 @write_msg(i8* %2, i32 %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %9
%23 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%24 = getelementptr inbounds i8, i8* %14, i64 4
%25 = call i32 @strcpy(i8* %24, i8* %2)
%26 = load i32, i32* @LOG_PID
%27 = load i32, i32* @LOG_USER
%28 = call i32 @openlog(i8* %14, i32 %26, i32 %27)
%29 = call i32 @read_msg(i8* %2, i32 1024)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %49
31: ; preds = %22
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 1
br label %33
33: ; preds = %31, %44
%34 = phi i32 [ %29, %31 ], [ %47, %44 ]
%35 = load i32, i32* @LOG_DEBUG
%36 = icmp ult i32 %34, 1024
%37 = select i1 %36, i32 %34, i32 1024
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %38
store i8 0, i8* %39
%40 = load i8, i8* %2
%41 = sext i8 %40 to i32
switch i32 %41, label %44 [
i32 105, label %42
i32 101, label %42
]
42: ; preds = %33, %33
%43 = load i32, i32* @LOG_ERR
br label %44
44: ; preds = %42, %33
%45 = phi i32 [ %35, %33 ], [ %43, %42 ]
%46 = call i32 @syslog(i32 %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %32)
store i8 0, i8* %2
store i8 0, i8* %32
%47 = call i32 @read_msg(i8* %2, i32 1024)
%48 = icmp sgt i32 %47, 0
br i1 %48, label %33, label %49
49: ; preds = %44, %22
%50 = call i32 @write_msg(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 22)
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%52 = call i32 @exit(i32 0)
unreachable
}