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

/* Variables and functions */
unsigned long ULONG_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
unsigned long strtoul(char *, char **, int);
int parse_args(int argc, char **argv, uint16_t *pid, FILE **tsfile) {
  if (argc != 3) {
    fprintf(stderr, "Usage : %s <pid> <tsfile>\n", argv[0]);
    return 1;
  }
  char *endptr;
  unsigned long ttx_pid = strtoul(argv[1], &endptr, 10);
  if (ULONG_MAX == ttx_pid || endptr == argv[1] || *endptr != '\0') {
    fprintf(stderr, "Could not convert %s to an unsigned integer.\n", argv[1]);
    return 1;
  } else if (ttx_pid > ((2 << 13) - 1)) {
    fprintf(stderr, "Value %lu is too large for a valid PID.\n", ttx_pid);
    return 1;
  }
  FILE *f = fopen(argv[2], "rb");
  if (NULL == f) {
    fprintf(stderr, "Could not open file %s : %s\n", argv[2], strerror(errno));
    return 1;
  }
  *pid = (ttx_pid & 0x1fff);
  *tsfile = f;
  return 0;
}


x86 assembly (clang, -O0):
<parse_args>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     118b <parse_args+0x4b>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe88(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <parse_args+0x192>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%rax
cmp    -0x30(%rbp),%rax
je     11db <parse_args+0x9b>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    0x8(%rcx),%rax
je     11db <parse_args+0x9b>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1206 <parse_args+0xc6>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <parse_args+0x192>
cmpq   $0x3fff,-0x30(%rbp)
jbe    123b <parse_args+0xfb>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xe21(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <parse_args+0x192>
jmp    1240 <parse_args+0x100>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe23(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x38(%rbp),%rax
jne    12ae <parse_args+0x16e>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <parse_args+0x192>
mov    -0x30(%rbp),%rcx
and    $0x1fff,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_args>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1182 <parse_args+0x42>
mov    %rcx,%r14
mov    %rdx,%r15
mov    0x8(%rbx),%rdi
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1050 <strtoul@plt>
mov    %rax,%r12
mov    0x2e51(%rip),%rax
cmp    %r12,(%rax)
jne    1197 <parse_args+0x57>
mov    0x8(%rbx),%rdx
jmp    11cb <parse_args+0x8b>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe6b(%rip),%rsi
jmp    11db <parse_args+0x9b>
mov    0x8(%rsp),%rax
mov    0x8(%rbx),%rdx
cmp    %rdx,%rax
je     11c8 <parse_args+0x88>
cmpb   $0x0,(%rax)
jne    11cb <parse_args+0x8b>
cmp    $0x4000,%r12
jb     11f4 <parse_args+0xb4>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
mov    %r12,%rdx
jmp    11db <parse_args+0x9b>
mov    %rax,%rdx
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%rbx),%rdi
lea    0xe73(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     121a <parse_args+0xda>
and    $0x1fff,%r12d
mov    %r12,(%r15)
mov    %rax,(%r14)
xor    %eax,%eax
jmp    11e7 <parse_args+0xa7>
mov    0x2dbf(%rip),%rax
mov    (%rax),%ebp
mov    0x10(%rbx),%rbx
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe39(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11e2 <parse_args+0xa2>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"Usage : %s <pid> <tsfile>\0A\00"
@ULONG_MAX = global i64 0
@.str.1 = [46 x i8] c"Could not convert %s to an unsigned integer.\0A\00"
@.str.2 = [41 x i8] c"Value %lu is too large for a valid PID.\0A\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [29 x i8] c"Could not open file %s : %s\0A\00"
@errno = global i32 0
define i32 @parse_args(i32 %0, i8** %1, i64* %2, i32** %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i64*
%9 = alloca i32**
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32*
store i32 %0, i32* %6
store i8** %1, i8*** %7
store i64* %2, i64** %8
store i32** %3, i32*** %9
%13 = load i32, i32* %6
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %21
15: ; preds = %4
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %7
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 1, i32* %5
br label %75
21: ; preds = %4
%22 = load i8**, i8*** %7
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i64 @strtoul(i8* %24, i8** %10, i32 10)
store i64 %25, i64* %11
%26 = load i64, i64* @ULONG_MAX
%27 = load i64, i64* %11
%28 = icmp eq i64 %26, %27
br i1 %28, label %40, label %29
29: ; preds = %21
%30 = load i8*, i8** %10
%31 = load i8**, i8*** %7
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = icmp eq i8* %30, %33
br i1 %34, label %40, label %35
35: ; preds = %29
%36 = load i8*, i8** %10
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %35, %29, %21
%41 = load i32, i32* @stderr
%42 = load i8**, i8*** %7
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %44)
store i32 1, i32* %5
br label %75
46: ; preds = %35
%47 = load i64, i64* %11
%48 = icmp ugt i64 %47, 16383
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i32, i32* @stderr
%51 = load i64, i64* %11
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i64 %51)
store i32 1, i32* %5
br label %75
53: ; preds = %46
br label %54
54: ; preds = %53
%55 = load i8**, i8*** %7
%56 = getelementptr inbounds i8*, i8** %55, i64 2
%57 = load i8*, i8** %56
%58 = call i32* @fopen(i8* %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %58, i32** %12
%59 = load i32*, i32** %12
%60 = icmp eq i32* null, %59
br i1 %60, label %61, label %69
61: ; preds = %54
%62 = load i32, i32* @stderr
%63 = load i8**, i8*** %7
%64 = getelementptr inbounds i8*, i8** %63, i64 2
%65 = load i8*, i8** %64
%66 = load i32, i32* @errno
%67 = call i8* @strerror(i32 %66)
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %65, i8* %67)
store i32 1, i32* %5
br label %75
69: ; preds = %54
%70 = load i64, i64* %11
%71 = and i64 %70, 8191
%72 = load i64*, i64** %8
store i64 %71, i64* %72
%73 = load i32*, i32** %12
%74 = load i32**, i32*** %9
store i32* %73, i32** %74
store i32 0, i32* %5
br label %75
75: ; preds = %69, %61, %49, %40, %15
%76 = load i32, i32* %5
ret i32 %76
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"Usage : %s <pid> <tsfile>\0A\00"
@ULONG_MAX = global i64 0
@.str.1 = [46 x i8] c"Could not convert %s to an unsigned integer.\0A\00"
@.str.2 = [41 x i8] c"Value %lu is too large for a valid PID.\0A\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [29 x i8] c"Could not open file %s : %s\0A\00"
@errno = global i32 0
define i32 @parse_args(i32 %0, i8** nocapture %1, i64* nocapture writeonly %2, i32** nocapture writeonly %3) {
%5 = alloca i8*
%6 = icmp eq i32 %0, 3
br i1 %6, label %11, label %7
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %51
11: ; preds = %4
%12 = bitcast i8** %5 to i8*
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i64 @strtoul(i8* %14, i8** %5, i32 10)
%16 = load i64, i64* @ULONG_MAX
%17 = icmp eq i64 %16, %15
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = load i8*, i8** %13
br label %27
20: ; preds = %11
%21 = load i8*, i8** %5
%22 = load i8*, i8** %13
%23 = icmp eq i8* %21, %22
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = load i8, i8* %21
%26 = icmp eq i8 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %18, %24, %20
%28 = phi i8* [ %19, %18 ], [ %22, %24 ], [ %21, %20 ]
%29 = load i32, i32* @stderr
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %49
31: ; preds = %24
%32 = icmp ugt i64 %15, 16383
br i1 %32, label %33, label %36
33: ; preds = %31
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i64 %15)
br label %49
36: ; preds = %31
%37 = getelementptr inbounds i8*, i8** %1, i64 2
%38 = load i8*, i8** %37
%39 = tail call i32* @fopen(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %47
41: ; preds = %36
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %37
%44 = load i32, i32* @errno
%45 = tail call i8* @strerror(i32 %44)
%46 = tail call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %43, i8* %45)
br label %49
47: ; preds = %36
%48 = and i64 %15, 8191
store i64 %48, i64* %2
store i32* %39, i32** %3
br label %49
49: ; preds = %41, %47, %33, %27
%50 = phi i32 [ 1, %27 ], [ 1, %33 ], [ 1, %41 ], [ 0, %47 ]
br label %51
51: ; preds = %49, %7
%52 = phi i32 [ 1, %7 ], [ %50, %49 ]
ret i32 %52
}