#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*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ pdu_decode(char *);
int /*<<< orphan*/ printf(char *, ...);
char *strchr(char *, char);
int main(int argc, char **argv) {
  char buffer[16384];
  char *pos;
  FILE *f;
  size_t len;
  if (argc != 2) {
    printf("Not enough parameters!\nUsage: pdu-decode comm.dump\n");
    return 1;
  }
  f = fopen(argv[1], "r");
  if (f == NULL) {
    printf("Could not open %s\n", argv[1]);
    return 1;
  }
  len = fread(buffer, 1, sizeof(buffer) - 1, f);
  if (!feof(f)) {
    printf("Could not read whole file %s\n", argv[1]);
    fclose(f);
    return 1;
  }
  buffer[len] = 0;
  fclose(f);
  pos = strchr(buffer, '\n');
  if (pos == NULL)
    return 2;
  pos++;
  pos = strchr(pos, '\n');
  if (pos == NULL)
    return 2;
  pos++;
  if (pdu_decode(pos) < 0)
    return 3;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ad <main+0x3d>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    132b <main+0x1bb>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe78(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x4020(%rbp)
cmpq   $0x0,-0x4020(%rbp)
jne    11f8 <main+0x88>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    132b <main+0x1bb>
lea    -0x4010(%rbp),%rdi
mov    -0x4020(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3fff,%edx
call   1030 <fread@plt>
mov    %rax,-0x4028(%rbp)
mov    -0x4020(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
jne    125f <main+0xef>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x4020(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    132b <main+0x1bb>
mov    -0x4028(%rbp),%rax
movb   $0x0,-0x4010(%rbp,%rax,1)
mov    -0x4020(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x4010(%rbp),%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
mov    %rax,-0x4018(%rbp)
cmpq   $0x0,-0x4018(%rbp)
jne    12ac <main+0x13c>
movl   $0x2,-0x4(%rbp)
jmp    132b <main+0x1bb>
mov    -0x4018(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x4018(%rbp)
mov    -0x4018(%rbp),%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
mov    %rax,-0x4018(%rbp)
cmpq   $0x0,-0x4018(%rbp)
jne    12f0 <main+0x180>
movl   $0x2,-0x4(%rbp)
jmp    132b <main+0x1bb>
mov    -0x4018(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x4018(%rbp)
mov    -0x4018(%rbp),%rdi
call   1040 <pdu_decode@plt>
cmp    $0x0,%rax
jge    1324 <main+0x1b4>
movl   $0x3,-0x4(%rbp)
jmp    132b <main+0x1bb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x4008,%rsp
cmp    $0x2,%edi
jne    1238 <main+0xb8>
mov    %rsi,%r15
mov    0x8(%rsi),%rdi
lea    0xe5b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     124c <main+0xcc>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%r14d
mov    $0x1,%esi
mov    $0x3fff,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1090 <feof@plt>
test   %eax,%eax
je     1266 <main+0xe6>
movb   $0x0,(%rsp,%r12,1)
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %rsp,%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
mov    $0x2,%r14d
test   %rax,%rax
je     1280 <main+0x100>
add    $0x1,%rax
mov    %rax,%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     1280 <main+0x100>
add    $0x1,%rax
mov    %rax,%rdi
call   1050 <pdu_decode@plt>
mov    %rax,%r14
sar    $0x3f,%r14
and    $0x3,%r14d
jmp    1280 <main+0x100>
lea    0xdf4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r14d
jmp    1280 <main+0x100>
mov    0x8(%r15),%rsi
lea    0xdab(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x1,%r14d
jmp    1280 <main+0x100>
mov    0x8(%r15),%rsi
lea    0xda4(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r14d,%eax
add    $0x4008,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [52 x i8] c"Not enough parameters!\0AUsage: pdu-decode comm.dump\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"Could not open %s\0A\00"
@.str.3 = [30 x i8] c"Could not read whole file %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [16384 x i8]
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %66
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %8
%19 = load i32*, i32** %8
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i32 1, i32* %3
br label %66
26: ; preds = %14
%27 = getelementptr inbounds [16384 x i8], [16384 x i8]* %6, i64 0, i64 0
%28 = load i32*, i32** %8
%29 = call i64 @fread(i8* %27, i32 1, i32 16383, i32* %28)
store i64 %29, i64* %9
%30 = load i32*, i32** %8
%31 = call i32 @feof(i32* %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %26
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %36)
%38 = load i32*, i32** %8
%39 = call i32 @fclose(i32* %38)
store i32 1, i32* %3
br label %66
40: ; preds = %26
%41 = load i64, i64* %9
%42 = getelementptr inbounds [16384 x i8], [16384 x i8]* %6, i64 0, i64 %41
store i8 0, i8* %42
%43 = load i32*, i32** %8
%44 = call i32 @fclose(i32* %43)
%45 = getelementptr inbounds [16384 x i8], [16384 x i8]* %6, i64 0, i64 0
%46 = call i8* @strchr(i8* %45, i8 signext 10)
store i8* %46, i8** %7
%47 = load i8*, i8** %7
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %50
49: ; preds = %40
store i32 2, i32* %3
br label %66
50: ; preds = %40
%51 = load i8*, i8** %7
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %7
%53 = load i8*, i8** %7
%54 = call i8* @strchr(i8* %53, i8 signext 10)
store i8* %54, i8** %7
%55 = load i8*, i8** %7
%56 = icmp eq i8* %55, null
br i1 %56, label %57, label %58
57: ; preds = %50
store i32 2, i32* %3
br label %66
58: ; preds = %50
%59 = load i8*, i8** %7
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %7
%61 = load i8*, i8** %7
%62 = call i64 @pdu_decode(i8* %61)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %58
store i32 3, i32* %3
br label %66
65: ; preds = %58
store i32 0, i32* %3
br label %66
66: ; preds = %65, %64, %57, %49, %33, %21, %12
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"Could not open %s\0A\00"
@.str.3 = [30 x i8] c"Could not read whole file %s\0A\00"
@str = [51 x i8] c"Not enough parameters!\0AUsage: pdu-decode comm.dump\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16384 x i8]
%4 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
br label %38
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = load i8*, i8** %9
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %38
16: ; preds = %8
%17 = call i64 @fread(i8* %4, i32 1, i32 16383, i32* %11)
%18 = tail call i32 @feof(i32* %11)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i8*, i8** %9
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %21)
%23 = tail call i32 @fclose(i32* %11)
br label %38
24: ; preds = %16
%25 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 %17
store i8 0, i8* %25
%26 = tail call i32 @fclose(i32* %11)
%27 = call i8* @strchr(i8* %4, i8 signext 10)
%28 = icmp eq i8* %27, null
br i1 %28, label %38, label %29
29: ; preds = %24
%30 = getelementptr inbounds i8, i8* %27, i64 1
%31 = call i8* @strchr(i8* %30, i8 signext 10)
%32 = icmp eq i8* %31, null
br i1 %32, label %38, label %33
33: ; preds = %29
%34 = getelementptr inbounds i8, i8* %31, i64 1
%35 = call i64 @pdu_decode(i8* %34)
%36 = icmp slt i64 %35, 0
%37 = select i1 %36, i32 3, i32 0
br label %38
38: ; preds = %33, %29, %24, %20, %13, %6
%39 = phi i32 [ 1, %6 ], [ 1, %13 ], [ 1, %20 ], [ 2, %24 ], [ 2, %29 ], [ %37, %33 ]
ret i32 %39
}