#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 */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
void *atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
unsigned char *malloc(unsigned int);
int /*<<< orphan*/ memcpy(unsigned short *, unsigned char *, int);
unsigned short ntohs(unsigned short);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned char *, unsigned int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int /*<<< orphan*/, unsigned char *, int);
int main(int argc, char *argv[]) {
  int byte_read;
  int fd_ts;
  int ts_header_size;
  unsigned char temp;
  unsigned short pid;
  unsigned short payload_pid;
  unsigned int i;
  unsigned char *packet_buffer;
  unsigned char *current_packet;
  unsigned int buffer_size;
  buffer_size = 0;
  if (argc >= 3) {
    fd_ts = open(argv[1], O_RDONLY);
    payload_pid = atoi(argv[2]);
    if (argc > 3) {
      buffer_size = atoi(argv[3]);
    }
  } else {
    fprintf(
        stderr,
        "Usage: 'ts2pes filename.ts payload_pid [buffer_size_in_packets]'\n");
    return 2;
  }
  if (fd_ts < 0) {
    fprintf(stderr, "Can't find file %s\n", argv[1]);
    return 2;
  }
  if (payload_pid < 2 || payload_pid > 8192 - 2) {
    fprintf(stderr, "Invalid PID, range is [2..8190]\n");
  }
  if (buffer_size <= 0) {
    if (argv[3] != 0) {
      fprintf(stderr, "Buffer size is not valid\n");
      return 2;
    } else {
      buffer_size = 1;
    }
  }
  buffer_size *= 188;
  packet_buffer = malloc(buffer_size);
  if (packet_buffer == NULL) {
    fprintf(stderr, "Out of memory\n");
    return 2;
  }
  byte_read = 1;
  while (byte_read) {
    byte_read = read(fd_ts, packet_buffer, buffer_size);
    for (i = 0; i < buffer_size && byte_read; i += 188) {
      current_packet = packet_buffer + i;
      memcpy(&pid, current_packet + 1, 2);
      pid = ntohs(pid);
      pid = pid & 0x1fff;
      if (pid == payload_pid) {
        ts_header_size = 4;
        temp = (current_packet[3] >> 4) & 0x03;
        if (temp == 0) {
          ts_header_size = 188;
          fprintf(stderr, "invalid packet!\n");
        } else if (temp == 1) {
          ;
        } else if (temp == 2) {
          ts_header_size = 188;
        } else if (temp == 3) {
          ts_header_size += current_packet[4] + 1;
        }
        write(STDOUT_FILENO, current_packet + ts_header_size,
              188 - ts_header_size);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     11f0 <main+0x70>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %ax,-0x22(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    11eb <main+0x6b>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x3c(%rbp)
jmp    1213 <main+0x93>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    148c <main+0x30c>
cmpl   $0x0,-0x18(%rbp)
jge    1248 <main+0xc8>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    148c <main+0x30c>
movzwl -0x22(%rbp),%eax
cmp    $0x2,%eax
jl     1264 <main+0xe4>
movzwl -0x22(%rbp),%eax
cmp    $0x1ffe,%eax
jle    127b <main+0xfb>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpl   $0x0,-0x3c(%rbp)
ja     12c3 <main+0x143>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     12b7 <main+0x137>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    148c <main+0x30c>
movl   $0x1,-0x3c(%rbp)
jmp    12c3 <main+0x143>
imul   $0xbc,-0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1307 <main+0x187>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    148c <main+0x30c>
movl   $0x1,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1485 <main+0x305>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%ecx
xor    %eax,%eax
cmp    -0x3c(%rbp),%ecx
mov    %al,-0x3d(%rbp)
jae    134c <main+0x1cc>
cmpl   $0x0,-0x14(%rbp)
setne  %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    135c <main+0x1dc>
jmp    1480 <main+0x300>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
add    $0x1,%rsi
lea    -0x20(%rbp),%rdi
mov    $0x2,%edx
call   1060 <memcpy@plt>
movzwl -0x20(%rbp),%edi
call   1080 <ntohs@plt>
mov    %ax,-0x20(%rbp)
movzwl -0x20(%rbp),%eax
and    $0x1fff,%eax
mov    %ax,-0x20(%rbp)
movzwl -0x20(%rbp),%eax
movzwl -0x22(%rbp),%ecx
cmp    %ecx,%eax
jne    146b <main+0x2eb>
movl   $0x4,-0x1c(%rbp)
mov    -0x38(%rbp),%rax
movzbl 0x3(%rax),%eax
sar    $0x4,%eax
and    $0x3,%eax
mov    %al,-0x1d(%rbp)
movzbl -0x1d(%rbp),%eax
cmp    $0x0,%eax
jne    13f2 <main+0x272>
movl   $0xbc,-0x1c(%rbp)
mov    0x2bf3(%rip),%rax
mov    (%rax),%edi
lea    0xcba(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    144a <main+0x2ca>
movzbl -0x1d(%rbp),%eax
cmp    $0x1,%eax
jne    1404 <main+0x284>
jmp    1445 <main+0x2c5>
movzbl -0x1d(%rbp),%eax
cmp    $0x2,%eax
jne    141d <main+0x29d>
movl   $0xbc,-0x1c(%rbp)
jmp    1440 <main+0x2c0>
movzbl -0x1d(%rbp),%eax
cmp    $0x3,%eax
jne    143b <main+0x2bb>
mov    -0x38(%rbp),%rax
movzbl 0x4(%rax),%eax
add    $0x1,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1440 <main+0x2c0>
jmp    1445 <main+0x2c5>
jmp    144a <main+0x2ca>
mov    0x2b9f(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    $0xbc,%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
jmp    1470 <main+0x2f0>
mov    -0x28(%rbp),%eax
add    $0xbc,%eax
mov    %eax,-0x28(%rbp)
jmp    1331 <main+0x1b1>
jmp    130e <main+0x18e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x3,%edi
jl     1309 <main+0x189>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%r13d
mov    0x10(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %rax,%r15
xor    %r14d,%r14d
cmp    $0x3,%ebp
je     11d1 <main+0x51>
mov    0x18(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %rax,%r14
test   %r13d,%r13d
js     1322 <main+0x1a2>
movzwl %r15w,%eax
add    $0xffffe001,%eax
cmp    $0xffffe002,%eax
ja     1201 <main+0x81>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r14d,%r14d
je     1355 <main+0x1d5>
imul   $0xbc,%r14d,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     136e <main+0x1ee>
mov    %rax,%rbx
lea    0xe(%rsp),%r12
mov    %r13d,0x10(%rsp)
jmp    1240 <main+0xc0>
cmpl   $0x0,0x14(%rsp)
mov    0x10(%rsp),%r13d
je     1351 <main+0x1d1>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
mov    %eax,0x14(%rsp)
test   %eax,%eax
je     1230 <main+0xb0>
test   %ebp,%ebp
je     1230 <main+0xb0>
xor    %r14d,%r14d
jmp    12a3 <main+0x123>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xbc,%eax
mov    0x2d6f(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%ecx
add    %rcx,%r13
mov    $0xbc,%edx
sub    %eax,%edx
mov    %r13,%rsi
call   1030 <write@plt>
add    $0xbc,%r14d
cmp    %ebp,%r14d
jae    1230 <main+0xb0>
mov    %r14d,%r13d
lea    (%rbx,%r13,1),%rsi
add    $0x1,%rsi
mov    %r12,%rdi
mov    $0x2,%edx
call   1060 <memcpy@plt>
movzwl 0xe(%rsp),%edi
call   1080 <ntohs@plt>
and    $0x1fff,%eax
mov    %ax,0xe(%rsp)
cmp    %r15w,%ax
jne    1297 <main+0x117>
add    %rbx,%r13
movzbl 0x3(%r13),%eax
shr    $0x4,%al
and    $0x3,%al
cmp    $0x2,%al
je     1275 <main+0xf5>
cmp    $0x1,%al
je     12ff <main+0x17f>
test   %al,%al
je     125e <main+0xde>
movzbl 0x4(%r13),%eax
add    $0x5,%eax
jmp    127a <main+0xfa>
mov    $0x4,%eax
jmp    127a <main+0xfa>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
lea    0xce7(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    133d <main+0x1bd>
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xd0c(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    1342 <main+0x1c2>
cmpq   $0x0,0x18(%rbx)
je     1380 <main+0x200>
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
lea    0xd0b(%rip),%rsi
jmp    1319 <main+0x199>
mov    0x2c5b(%rip),%rax
mov    (%rax),%edi
lea    0xd13(%rip),%rsi
jmp    1319 <main+0x199>
mov    $0x1,%r14d
jmp    120a <main+0x8a>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [66 x i8] c"Usage: 'ts2pes filename.ts payload_pid [buffer_size_in_packets]'\0A\00"
@.str.1 = [20 x i8] c"Can't find file %s\0A\00"
@.str.2 = [33 x i8] c"Invalid PID, range is [2..8190]\0A\00"
@.str.3 = [26 x i8] c"Buffer size is not valid\0A\00"
@.str.4 = [15 x i8] c"Out of memory\0A\00"
@.str.5 = [17 x i8] c"invalid packet!\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i16
%11 = alloca i16
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %15
%16 = load i32, i32* %4
%17 = icmp sge i32 %16, 3
br i1 %17, label %18, label %38
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %7
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i8* @atoi(i8* %26)
%28 = ptrtoint i8* %27 to i16
store i16 %28, i16* %11
%29 = load i32, i32* %4
%30 = icmp sgt i32 %29, 3
br i1 %30, label %31, label %37
31: ; preds = %18
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 3
%34 = load i8*, i8** %33
%35 = call i8* @atoi(i8* %34)
%36 = ptrtoint i8* %35 to i32
store i32 %36, i32* %15
br label %37
37: ; preds = %31, %18
br label %41
38: ; preds = %2
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %3
br label %175
41: ; preds = %37
%42 = load i32, i32* %7
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %48)
store i32 2, i32* %3
br label %175
50: ; preds = %41
%51 = load i16, i16* %11
%52 = zext i16 %51 to i32
%53 = icmp slt i32 %52, 2
br i1 %53, label %58, label %54
54: ; preds = %50
%55 = load i16, i16* %11
%56 = zext i16 %55 to i32
%57 = icmp sgt i32 %56, 8190
br i1 %57, label %58, label %61
58: ; preds = %54, %50
%59 = load i32, i32* @stderr
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %61
61: ; preds = %58, %54
%62 = load i32, i32* %15
%63 = icmp ule i32 %62, 0
br i1 %63, label %64, label %74
64: ; preds = %61
%65 = load i8**, i8*** %5
%66 = getelementptr inbounds i8*, i8** %65, i64 3
%67 = load i8*, i8** %66
%68 = icmp ne i8* %67, null
br i1 %68, label %69, label %72
69: ; preds = %64
%70 = load i32, i32* @stderr
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %3
br label %175
72: ; preds = %64
store i32 1, i32* %15
br label %73
73: ; preds = %72
br label %74
74: ; preds = %73, %61
%75 = load i32, i32* %15
%76 = mul i32 %75, 188
store i32 %76, i32* %15
%77 = load i32, i32* %15
%78 = call i8* @malloc(i32 %77)
store i8* %78, i8** %13
%79 = load i8*, i8** %13
%80 = icmp eq i8* %79, null
br i1 %80, label %81, label %84
81: ; preds = %74
%82 = load i32, i32* @stderr
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %3
br label %175
84: ; preds = %74
store i32 1, i32* %6
br label %85
85: ; preds = %173, %84
%86 = load i32, i32* %6
%87 = icmp ne i32 %86, 0
br i1 %87, label %88, label %174
88: ; preds = %85
%89 = load i32, i32* %7
%90 = load i8*, i8** %13
%91 = load i32, i32* %15
%92 = call i32 @read(i32 %89, i8* %90, i32 %91)
store i32 %92, i32* %6
store i32 0, i32* %12
br label %93
93: ; preds = %170, %88
%94 = load i32, i32* %12
%95 = load i32, i32* %15
%96 = icmp ult i32 %94, %95
br i1 %96, label %97, label %100
97: ; preds = %93
%98 = load i32, i32* %6
%99 = icmp ne i32 %98, 0
br label %100
100: ; preds = %97, %93
%101 = phi i1 [ false, %93 ], [ %99, %97 ]
br i1 %101, label %102, label %173
102: ; preds = %100
%103 = load i8*, i8** %13
%104 = load i32, i32* %12
%105 = zext i32 %104 to i64
%106 = getelementptr inbounds i8, i8* %103, i64 %105
store i8* %106, i8** %14
%107 = load i8*, i8** %14
%108 = getelementptr inbounds i8, i8* %107, i64 1
%109 = call i32 @memcpy(i16* %10, i8* %108, i32 2)
%110 = load i16, i16* %10
%111 = call zeroext i16 @ntohs(i16 zeroext %110)
store i16 %111, i16* %10
%112 = load i16, i16* %10
%113 = zext i16 %112 to i32
%114 = and i32 %113, 8191
%115 = trunc i32 %114 to i16
store i16 %115, i16* %10
%116 = load i16, i16* %10
%117 = zext i16 %116 to i32
%118 = load i16, i16* %11
%119 = zext i16 %118 to i32
%120 = icmp eq i32 %117, %119
br i1 %120, label %121, label %169
121: ; preds = %102
store i32 4, i32* %8
%122 = load i8*, i8** %14
%123 = getelementptr inbounds i8, i8* %122, i64 3
%124 = load i8, i8* %123
%125 = zext i8 %124 to i32
%126 = ashr i32 %125, 4
%127 = and i32 %126, 3
%128 = trunc i32 %127 to i8
store i8 %128, i8* %9
%129 = load i8, i8* %9
%130 = zext i8 %129 to i32
%131 = icmp eq i32 %130, 0
br i1 %131, label %132, label %135
132: ; preds = %121
store i32 188, i32* %8
%133 = load i32, i32* @stderr
%134 = call i32 (i32, i8*, ...) @fprintf(i32 %133, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %160
135: ; preds = %121
%136 = load i8, i8* %9
%137 = zext i8 %136 to i32
%138 = icmp eq i32 %137, 1
br i1 %138, label %139, label %140
139: ; preds = %135
br label %159
140: ; preds = %135
%141 = load i8, i8* %9
%142 = zext i8 %141 to i32
%143 = icmp eq i32 %142, 2
br i1 %143, label %144, label %145
144: ; preds = %140
store i32 188, i32* %8
br label %158
145: ; preds = %140
%146 = load i8, i8* %9
%147 = zext i8 %146 to i32
%148 = icmp eq i32 %147, 3
br i1 %148, label %149, label %157
149: ; preds = %145
%150 = load i8*, i8** %14
%151 = getelementptr inbounds i8, i8* %150, i64 4
%152 = load i8, i8* %151
%153 = zext i8 %152 to i32
%154 = add nsw i32 %153, 1
%155 = load i32, i32* %8
%156 = add nsw i32 %155, %154
store i32 %156, i32* %8
br label %157
157: ; preds = %149, %145
br label %158
158: ; preds = %157, %144
br label %159
159: ; preds = %158, %139
br label %160
160: ; preds = %159, %132
%161 = load i32, i32* @STDOUT_FILENO
%162 = load i8*, i8** %14
%163 = load i32, i32* %8
%164 = sext i32 %163 to i64
%165 = getelementptr inbounds i8, i8* %162, i64 %164
%166 = load i32, i32* %8
%167 = sub nsw i32 188, %166
%168 = call i32 @write(i32 %161, i8* %165, i32 %167)
br label %169
169: ; preds = %160, %102
br label %170
170: ; preds = %169
%171 = load i32, i32* %12
%172 = add i32 %171, 188
store i32 %172, i32* %12
br label %93
173: ; preds = %100
br label %85
174: ; preds = %85
store i32 0, i32* %3
br label %175
175: ; preds = %174, %81, %69, %44, %38
%176 = load i32, i32* %3
ret i32 %176
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [66 x i8] c"Usage: 'ts2pes filename.ts payload_pid [buffer_size_in_packets]'\0A\00"
@.str.1 = [20 x i8] c"Can't find file %s\0A\00"
@.str.2 = [33 x i8] c"Invalid PID, range is [2..8190]\0A\00"
@.str.3 = [26 x i8] c"Buffer size is not valid\0A\00"
@.str.4 = [15 x i8] c"Out of memory\0A\00"
@.str.5 = [17 x i8] c"invalid packet!\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i16
%4 = bitcast i16* %3 to i8*
%5 = icmp sgt i32 %0, 2
br i1 %5, label %6, label %23
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %8, i32 %9)
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i8* @atoi(i8* %12)
%14 = ptrtoint i8* %13 to i64
%15 = trunc i64 %14 to i32
%16 = icmp eq i32 %0, 3
br i1 %16, label %26, label %17
17: ; preds = %6
%18 = getelementptr inbounds i8*, i8** %1, i64 3
%19 = load i8*, i8** %18
%20 = tail call i8* @atoi(i8* %19)
%21 = ptrtoint i8* %20 to i64
%22 = trunc i64 %21 to i32
br label %26
23: ; preds = %2
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0))
br label %100
26: ; preds = %6, %17
%27 = phi i32 [ %22, %17 ], [ 0, %6 ]
%28 = icmp slt i32 %10, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %7
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %31)
br label %100
33: ; preds = %26
%34 = and i32 %15, 65535
%35 = add nsw i32 %34, -8191
%36 = icmp ult i32 %35, -8189
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = tail call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %33, %37
%41 = icmp eq i32 %27, 0
br i1 %41, label %42, label %49
42: ; preds = %40
%43 = getelementptr inbounds i8*, i8** %1, i64 3
%44 = load i8*, i8** %43
%45 = icmp eq i8* %44, null
br i1 %45, label %49, label %46
46: ; preds = %42
%47 = load i32, i32* @stderr
%48 = tail call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %100
49: ; preds = %42, %40
%50 = phi i32 [ %27, %40 ], [ 1, %42 ]
%51 = mul i32 %50, 188
%52 = tail call i8* @malloc(i32 %51)
%53 = icmp eq i8* %52, null
br i1 %53, label %57, label %54
54: ; preds = %49
%55 = icmp ne i32 %51, 0
%56 = trunc i64 %14 to i16
br label %62
57: ; preds = %49
%58 = load i32, i32* @stderr
%59 = tail call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %100
60: ; preds = %97, %62
%61 = icmp eq i32 %63, 0
br i1 %61, label %100, label %62
62: ; preds = %54, %60
%63 = call i32 @read(i32 %10, i8* %52, i32 %51)
%64 = icmp ne i32 %63, 0
%65 = and i1 %64, %55
br i1 %65, label %66, label %60
66: ; preds = %62, %97
%67 = phi i32 [ %98, %97 ], [ 0, %62 ]
%68 = zext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %52, i64 %68
%70 = getelementptr inbounds i8, i8* %69, i64 1
%71 = call i32 @memcpy(i16* %3, i8* %70, i32 2)
%72 = load i16, i16* %3
%73 = call zeroext i16 @ntohs(i16 zeroext %72)
%74 = and i16 %73, 8191
store i16 %74, i16* %3
%75 = icmp eq i16 %74, %56
br i1 %75, label %76, label %97
76: ; preds = %66
%77 = getelementptr inbounds i8, i8* %69, i64 3
%78 = load i8, i8* %77
%79 = lshr i8 %78, 4
%80 = and i8 %79, 3
switch i8 %80, label %85 [
i8 0, label %81
i8 1, label %90
i8 2, label %84
]
81: ; preds = %76
%82 = load i32, i32* @stderr
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %90
84: ; preds = %76
br label %90
85: ; preds = %76
%86 = getelementptr inbounds i8, i8* %69, i64 4
%87 = load i8, i8* %86
%88 = zext i8 %87 to i32
%89 = add nuw nsw i32 %88, 5
br label %90
90: ; preds = %76, %85, %84, %81
%91 = phi i32 [ 188, %81 ], [ 188, %84 ], [ %89, %85 ], [ 4, %76 ]
%92 = load i32, i32* @STDOUT_FILENO
%93 = zext i32 %91 to i64
%94 = getelementptr inbounds i8, i8* %69, i64 %93
%95 = sub nsw i32 188, %91
%96 = call i32 @write(i32 %92, i8* %94, i32 %95)
br label %97
97: ; preds = %66, %90
%98 = add i32 %67, 188
%99 = icmp ult i32 %98, %51
br i1 %99, label %66, label %60
100: ; preds = %60, %57, %46, %29, %23
%101 = phi i32 [ 2, %29 ], [ 2, %46 ], [ 2, %57 ], [ 2, %23 ], [ 0, %60 ]
ret i32 %101
}