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

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ respond(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *, ...);
int vul(void) {
  char tmp[1024];
  char buf[1024];
  int len = 0;
  syslog(LOG_ERR, "vul() -> tmp = 0x%x buf = 0x%x\n", tmp, buf);
  while (1) {
    memset(buf, 0, sizeof(buf));
    memset(tmp, 0, sizeof(tmp));
    if ((len = read(0, buf, sizeof(buf))) <= 0) {
      syslog(LOG_ERR, "vul() -> error while reading input buf [%s] (%d)", buf,
             len);
      exit(-1);
    }
    if (!strncmp(buf, "quit", 4)) {
      respond("bye bye ...\n");
      return 0;
    }
    snprintf(tmp, sizeof(tmp) - 1, buf);
    respond("%s", tmp);
  }
}


x86 assembly (clang, -O0):
<vul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x810,%rsp
movl   $0x0,-0x804(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    -0x400(%rbp),%rdx
lea    -0x800(%rbp),%rcx
lea    0xe5d(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
lea    -0x800(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x400(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x800(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x804(%rbp)
cmp    $0x0,%eax
jg     1220 <vul+0xb0>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    -0x800(%rbp),%rdx
mov    -0x804(%rbp),%ecx
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   1080 <syslog@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    -0x800(%rbp),%rdi
lea    0xe23(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    125a <vul+0xea>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <respond@plt>
xor    %eax,%eax
add    $0x810,%rsp
pop    %rbp
ret
lea    -0x400(%rbp),%rdi
lea    -0x800(%rbp),%rdx
mov    $0x3ff,%esi
call   1050 <snprintf@plt>
lea    -0x400(%rbp),%rsi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1040 <respond@plt>
jmp    11aa <vul+0x3a>

x86 assembly (clang, -O2):
<vul>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    0x2e51(%rip),%r13
mov    0x0(%r13),%edi
lea    0xe6e(%rip),%rsi
lea    0x400(%rsp),%r14
mov    %rsp,%rbx
mov    %r14,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1080 <syslog@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    1262 <vul+0xf2>
lea    0xe6b(%rip),%r14
mov    %rsp,%rbx
lea    0x400(%rsp),%r15
lea    0xe6b(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     1283 <vul+0x113>
mov    %r15,%rdi
mov    $0x3ff,%esi
mov    %rbx,%rdx
call   1050 <snprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <respond@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
jg     1200 <vul+0x90>
mov    0x0(%r13),%edi
lea    0xdb3(%rip),%rsi
mov    %rsp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1080 <syslog@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xdcc(%rip),%rdi
xor    %eax,%eax
call   1040 <respond@plt>
xor    %eax,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@LOG_ERR = global i32 0
@.str = [32 x i8] c"vul() -> tmp = 0x%x buf = 0x%x\0A\00"
@.str.1 = [49 x i8] c"vul() -> error while reading input buf [%s] (%d)\00"
@.str.2 = [5 x i8] c"quit\00"
@.str.3 = [13 x i8] c"bye bye ...\0A\00"
@.str.4 = [3 x i8] c"%s\00"
define i32 @vul() {
%1 = alloca [1024 x i8]
%2 = alloca [1024 x i8]
%3 = alloca i32
store i32 0, i32* %3
%4 = load i32, i32* @LOG_ERR
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%7 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %4, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %5, i8* %6)
br label %8
8: ; preds = %0, %28
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 0, i32 1024)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 1024)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%14 = call i32 @read(i32 0, i8* %13, i32 1024)
store i32 %14, i32* %3
%15 = icmp sle i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %8
%17 = load i32, i32* @LOG_ERR
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%19 = load i32, i32* %3
%20 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %17, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %18, i32 %19)
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %8
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%24 = call i32 @strncmp(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%25 = icmp ne i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = call i32 (i8*, ...) @respond(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
28: ; preds = %22
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%31 = call i32 @snprintf(i8* %29, i32 1023, i8* %30)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%33 = call i32 (i8*, ...) @respond(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %32)
br label %8
}

LLVM IR (-O2):
@LOG_ERR = global i32 0
@.str = [32 x i8] c"vul() -> tmp = 0x%x buf = 0x%x\0A\00"
@.str.1 = [49 x i8] c"vul() -> error while reading input buf [%s] (%d)\00"
@.str.2 = [5 x i8] c"quit\00"
@.str.3 = [13 x i8] c"bye bye ...\0A\00"
@.str.4 = [3 x i8] c"%s\00"
define i32 @vul() {
%1 = alloca [1024 x i8]
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @LOG_ERR
%6 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %5, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %3, i8* %4)
%7 = call i32 @memset(i8* %4, i32 0, i32 1024)
%8 = call i32 @memset(i8* %3, i32 0, i32 1024)
%9 = call i32 @read(i32 0, i8* %4, i32 1024)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %16
11: ; preds = %21, %0
%12 = phi i32 [ %9, %0 ], [ %26, %21 ]
%13 = load i32, i32* @LOG_ERR
%14 = call i32 (i32, i8*, i8*, ...) @syslog(i32 %13, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i8* %4, i32 %12)
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %0, %21
%17 = call i32 @strncmp(i8* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 (i8*, ...) @respond(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
21: ; preds = %16
%22 = call i32 @snprintf(i8* %3, i32 1023, i8* %4)
%23 = call i32 (i8*, ...) @respond(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %3)
%24 = call i32 @memset(i8* %4, i32 0, i32 1024)
%25 = call i32 @memset(i8* %3, i32 0, i32 1024)
%26 = call i32 @read(i32 0, i8* %4, i32 1024)
%27 = icmp slt i32 %26, 1
br i1 %27, label %11, label %16
}