#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 */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ rbuf;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ cat_file(char *);
int /*<<< orphan*/ config_get(char *);
int /*<<< orphan*/ daemon(int, int);
int fill_payload(char *);
scalar_t__ memcmp(char *, char *, int);
int open_telnet(char *);
int /*<<< orphan*/ printf(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
scalar_t__ strncmp(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ system(char *);
int main(int argc, char *argv[]) {
  int fd;
  socklen_t slen;
  struct sockaddr_in from;
  fd_set readable;
  int telnet_enabled = 0;
  int r, datasize;
  char rbuf[512], output_buf[512];
  char ack[] = "ACK";
  fd = open_telnet("br0");
  if (fd < 0) {
    printf("Can't open the socket!\n");
    return -1;
  }
  daemon(1, 1);
  printf("The telnetenable is running ...\n");
  for (;;) {
    FD_ZERO(&readable);
    FD_SET(fd, &readable);
    if (select(fd + 1, &readable, NULL, NULL, NULL) < 1)
      continue;
    slen = sizeof(struct sockaddr_in);
    r = recvfrom(fd, rbuf, sizeof(rbuf), 0, (struct sockaddr *)&from, &slen);
    if (r < 1)
      continue;
    datasize = fill_payload(output_buf);
    if (r == datasize && memcmp(rbuf, output_buf, r) == 0) {
      if (telnet_enabled == 0 &&
          ((strncmp(cat_file("/tmp/firmware_region"), "PR", 2) != 0) &&
           !((strncmp(cat_file("/tmp/firmware_region"), "WW", 2) == 0) &&
             (strcmp(config_get("GUI_Region"), "Chinese") == 0)))) {
        printf("The telnet server is enabled now!!!\n");
        system("/usr/sbin/utelnetd -d -i "
               "br0");
        telnet_enabled = 1;
      }
      sendto(fd, ack, 3, 0, (struct sockaddr *)&from, slen);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
mov    0xdea(%rip),%eax
mov    %eax,-0x434(%rbp)
lea    0xe86(%rip),%rdi
call   10d0 <open_telnet@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124f <main+0x5f>
lea    0xdc8(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%eax
add    $0x440,%rsp
pop    %rbp
ret
mov    $0x1,%esi
mov    %esi,%edi
call   10f0 <daemon@plt>
lea    0xdba(%rip),%rdi
call   1060 <printf@plt>
lea    -0x24(%rbp),%rdi
call   10b0 <FD_ZERO@plt>
mov    -0x14(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1110 <FD_SET@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10c0 <select@plt>
cmp    $0x1,%eax
jge    12a4 <main+0xb4>
jmp    1267 <main+0x77>
movl   $0x4,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    $0x200,%edx
xor    %ecx,%ecx
lea    -0x18(%rbp),%r9
call   10e0 <recvfrom@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jge    12db <main+0xeb>
jmp    1267 <main+0x77>
lea    -0x430(%rbp),%rdi
call   1030 <fill_payload@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jne    13d3 <main+0x1e3>
lea    -0x230(%rbp),%rdi
lea    -0x430(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
jne    13d3 <main+0x1e3>
cmpl   $0x0,-0x28(%rbp)
jne    13b5 <main+0x1c5>
lea    0xd16(%rip),%rdi
call   1100 <cat_file@plt>
mov    %eax,%edi
lea    0xd1d(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     13b5 <main+0x1c5>
lea    0xced(%rip),%rdi
call   1100 <cat_file@plt>
mov    %eax,%edi
lea    0xcf7(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1396 <main+0x1a6>
lea    0xcdf(%rip),%rdi
call   1090 <config_get@plt>
mov    %eax,%edi
lea    0xcdc(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     13b5 <main+0x1c5>
lea    0xcce(%rip),%rdi
call   1060 <printf@plt>
lea    0xce7(%rip),%rdi
call   1050 <system@plt>
movl   $0x1,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x434(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    -0x18(%rbp),%r9d
mov    $0x3,%edx
xor    %ecx,%ecx
call   1070 <sendto@plt>
jmp    1267 <main+0x77>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
movl   $0x4b4341,0xc(%rsp)
lea    0xe37(%rip),%rdi
call   10d0 <open_telnet@plt>
test   %eax,%eax
js     13b0 <main+0x1c0>
mov    %eax,%ebx
mov    $0x1,%edi
mov    $0x1,%esi
call   10f0 <daemon@plt>
lea    0xe16(%rip),%rdi
call   1050 <puts@plt>
mov    %ebx,%r14d
add    $0x1,%r14d
xor    %eax,%eax
lea    0x1c(%rsp),%rbp
lea    0x220(%rsp),%r15
lea    0x4(%rsp),%r13
jmp    129e <main+0xae>
nopw   0x0(%rax,%rax,1)
lea    0xe04(%rip),%rdi
call   1050 <puts@plt>
lea    0xdbb(%rip),%rdi
call   1060 <system@plt>
mov    $0x1,%r12d
mov    0x4(%rsp),%r9d
mov    %ebx,%edi
lea    0xc(%rsp),%rsi
mov    $0x3,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
call   1070 <sendto@plt>
mov    %r12d,%eax
mov    %rax,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
call   10b0 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1110 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10c0 <select@plt>
test   %eax,%eax
jle    12b0 <main+0xc0>
movl   $0x4,0x4(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
lea    0x8(%rsp),%r8
mov    %r13,%r9
call   10e0 <recvfrom@plt>
test   %eax,%eax
jle    12b0 <main+0xc0>
mov    %eax,%r12d
lea    0x20(%rsp),%rdi
call   1030 <fill_payload@plt>
cmp    %eax,%r12d
jne    12b0 <main+0xc0>
mov    %r15,%rdi
lea    0x20(%rsp),%rsi
mov    %r12d,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
jne    12b0 <main+0xc0>
mov    $0x1,%r12d
cmpl   $0x0,0x10(%rsp)
jne    127e <main+0x8e>
lea    0xcc4(%rip),%rdi
call   1100 <cat_file@plt>
mov    %eax,%edi
lea    0xccb(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
xor    %r12d,%r12d
test   %rax,%rax
je     127e <main+0x8e>
lea    0xc99(%rip),%rdi
call   1100 <cat_file@plt>
mov    %eax,%edi
lea    0xca3(%rip),%rsi
mov    $0x2,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    1260 <main+0x70>
lea    0xc8c(%rip),%rdi
call   1090 <config_get@plt>
mov    %eax,%edi
lea    0xc89(%rip),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
jne    1260 <main+0x70>
jmp    127e <main+0x8e>
lea    0xcd8(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.ack = [4 x i8] c"ACK\00"
@.str = [4 x i8] c"br0\00"
@.str.1 = [24 x i8] c"Can't open the socket!\0A\00"
@.str.2 = [33 x i8] c"The telnetenable is running ...\0A\00"
@.str.3 = [21 x i8] c"/tmp/firmware_region\00"
@.str.4 = [3 x i8] c"PR\00"
@.str.5 = [3 x i8] c"WW\00"
@.str.6 = [11 x i8] c"GUI_Region\00"
@.str.7 = [8 x i8] c"Chinese\00"
@.str.8 = [37 x i8] c"The telnet server is enabled now!!!\0A\00"
@.str.9 = [29 x i8] c"/usr/sbin/utelnetd -d -i br0\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [512 x i8]
%14 = alloca [512 x i8]
%15 = alloca [4 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %10
%16 = bitcast [4 x i8]* %15 to i8*
%17 = call i32 @open_telnet(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
ret i32 -1
22: ; preds = %2
%23 = call i32 @daemon(i32 1, i32 1)
%24 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %78, %41, %33, %22
%26 = call i32 @FD_ZERO(i32* %9)
%27 = load i32, i32* %6
%28 = call i32 @FD_SET(i32 %27, i32* %9)
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
%31 = call i32 @select(i32 %30, i32* %9, i32* null, i32* null, i32* null)
%32 = icmp slt i32 %31, 1
br i1 %32, label %33, label %34
33: ; preds = %25
br label %25
34: ; preds = %25
store i32 4, i32* %7
%35 = load i32, i32* %6
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%37 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%38 = call i32 @recvfrom(i32 %35, i8* %36, i32 512, i32 0, %struct.sockaddr* %37, i32* %7)
store i32 %38, i32* %11
%39 = load i32, i32* %11
%40 = icmp slt i32 %39, 1
br i1 %40, label %41, label %42
41: ; preds = %34
br label %25
42: ; preds = %34
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 0
%44 = call i32 @fill_payload(i8* %43)
store i32 %44, i32* %12
%45 = load i32, i32* %11
%46 = load i32, i32* %12
%47 = icmp eq i32 %45, %46
br i1 %47, label %48, label %78
48: ; preds = %42
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %14, i64 0, i64 0
%51 = load i32, i32* %11
%52 = call i64 @memcmp(i8* %49, i8* %50, i32 %51)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %78
54: ; preds = %48
%55 = load i32, i32* %10
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %72
57: ; preds = %54
%58 = call i32 @cat_file(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%59 = call i64 @strncmp(i32 %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 2)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %72
61: ; preds = %57
%62 = call i32 @cat_file(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%63 = call i64 @strncmp(i32 %62, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2)
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %61
%66 = call i32 @config_get(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%67 = call i64 @strcmp(i32 %66, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
%68 = icmp eq i64 %67, 0
br i1 %68, label %72, label %69
69: ; preds = %65, %61
%70 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0))
%71 = call i32 @system(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %10
br label %72
72: ; preds = %69, %65, %57, %54
%73 = load i32, i32* %6
%74 = getelementptr inbounds [4 x i8], [4 x i8]* %15, i64 0, i64 0
%75 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%76 = load i32, i32* %7
%77 = call i32 @sendto(i32 %73, i8* %74, i32 3, i32 0, %struct.sockaddr* %75, i32 %76)
br label %78
78: ; preds = %72, %48, %42
br label %25
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [4 x i8] c"br0\00"
@.str.3 = [21 x i8] c"/tmp/firmware_region\00"
@.str.4 = [3 x i8] c"PR\00"
@.str.5 = [3 x i8] c"WW\00"
@.str.6 = [11 x i8] c"GUI_Region\00"
@.str.7 = [8 x i8] c"Chinese\00"
@.str.9 = [29 x i8] c"/usr/sbin/utelnetd -d -i br0\00"
@str = [32 x i8] c"The telnetenable is running ...\00"
@str.10 = [36 x i8] c"The telnet server is enabled now!!!\00"
@str.11 = [23 x i8] c"Can't open the socket!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = alloca [512 x i8]
%8 = alloca i32
%9 = bitcast i32* %3 to i8*
%10 = bitcast %struct.sockaddr_in* %4 to i8*
%11 = bitcast i32* %5 to i8*
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%14 = bitcast i32* %8 to i8*
store i32 4932417, i32* %8
%15 = tail call i32 @open_telnet(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.11, i64 0, i64 0))
ret i32 -1
19: ; preds = %2
%20 = tail call i32 @daemon(i32 1, i32 1)
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%22 = add nuw nsw i32 %15, 1
%23 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %24
24: ; preds = %60, %19
%25 = phi i32 [ %61, %60 ], [ 0, %19 ]
br label %26
26: ; preds = %42, %24
br label %27
27: ; preds = %32, %26
%28 = call i32 @FD_ZERO(i32* %5)
%29 = call i32 @FD_SET(i32 %15, i32* %5)
%30 = call i32 @select(i32 %22, i32* %5, i32* null, i32* null, i32* null)
%31 = icmp slt i32 %30, 1
br i1 %31, label %32, label %33
32: ; preds = %27, %33
br label %27
33: ; preds = %27
store i32 4, i32* %3
%34 = call i32 @recvfrom(i32 %15, i8* %12, i32 512, i32 0, %struct.sockaddr* %23, i32* %3)
%35 = icmp slt i32 %34, 1
br i1 %35, label %32, label %36
36: ; preds = %33
%37 = call i32 @fill_payload(i8* %13)
%38 = icmp eq i32 %34, %37
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = call i64 @memcmp(i8* %12, i8* %13, i32 %34)
%41 = icmp eq i64 %40, 0
br i1 %41, label %43, label %42
42: ; preds = %39, %36
br label %26
43: ; preds = %39
%44 = icmp eq i32 %25, 0
br i1 %44, label %45, label %60
45: ; preds = %43
%46 = call i32 @cat_file(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%47 = call i64 @strncmp(i32 %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 2)
%48 = icmp eq i64 %47, 0
br i1 %48, label %60, label %49
49: ; preds = %45
%50 = call i32 @cat_file(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%51 = call i64 @strncmp(i32 %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %49
%54 = call i32 @config_get(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%55 = call i64 @strcmp(i32 %54, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
%56 = icmp eq i64 %55, 0
br i1 %56, label %60, label %57
57: ; preds = %53, %49
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.10, i64 0, i64 0))
%59 = call i32 @system(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0))
br label %60
60: ; preds = %57, %53, %45, %43
%61 = phi i32 [ 0, %53 ], [ 1, %57 ], [ 0, %45 ], [ 1, %43 ]
%62 = load i32, i32* %3
%63 = call i32 @sendto(i32 %15, i8* %14, i32 3, i32 0, %struct.sockaddr* %23, i32 %62)
br label %24
}