#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fread(unsigned char *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, long, int, int /*<<< orphan*/);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  if (argc != 3) {
    fprintf(stderr, "usage: %s <in.bin> <out.gdb>\n", argv[0]);
    exit(1);
  }
  FILE *in = fopen(argv[1], "r");
  FILE *out = fopen(argv[2], "w+");
  if (!in) {
    fprintf(stderr, "error opening input file %s\n", argv[1]);
    exit(2);
  }
  if (!out) {
    fprintf(stderr, "error opening output file %s\n", argv[2]);
    exit(3);
  }
  fseek(in, 0L, SEEK_END);
  long sz = ftell(in);
  fseek(in, 0L, SEEK_SET);
  if (!sz) {
    fprintf(stderr, "empty input file\n");
    exit(4);
  } else {
    unsigned char buf[sz];
    fread(buf, sz, 1, in);
    fwrite(buf, sz, 1, stdout);
    fflush(stdout);
    fread(buf, sz, 1, stdin);
    int i;
    fprintf(out, "set gdb_rpc={%d", buf[0]);
    for (i = 2; i < sz; i++)
      fprintf(out, ",%d", buf[i]);
    fprintf(out, "}\n");
    fclose(out);
    fclose(in);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11d9 <main+0x49>
mov    0x2e19(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe38(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe36(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe20(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    123e <main+0xae>
mov    0x2db5(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1273 <main+0xe3>
mov    0x2d80(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d6a(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d27(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
cmpq   $0x0,-0x28(%rbp)
jne    12d9 <main+0x149>
mov    0x2d12(%rip),%rax
mov    (%rax),%rdi
lea    0xd96(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x4,%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2c9f(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2c96(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x48(%rbp),%rax
mov    -0x20(%rbp),%rdi
movzbl (%rax),%edx
lea    0xd07(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x3c(%rbp)
movslq -0x3c(%rbp),%rax
cmp    -0x28(%rbp),%rax
jge    13b1 <main+0x221>
mov    -0x48(%rbp),%rax
mov    -0x20(%rbp),%rdi
movslq -0x3c(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xce4(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1377 <main+0x1e7>
mov    -0x20(%rbp),%rdi
lea    0xcc8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    11f7 <main+0x67>
mov    0x8(%rbx),%rdi
lea    0xe6a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
lea    0xe59(%rip),%rsi
call   1090 <fopen@plt>
test   %r14,%r14
jne    121c <main+0x8c>
mov    0x2df8(%rip),%rax
mov    (%rax),%rdi
mov    0x8(%rbx),%rdx
lea    0xe3d(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%rdi
mov    (%rbx),%rdx
lea    0xdf5(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %rax,%r15
test   %rax,%rax
jne    124a <main+0xba>
mov    0x2da5(%rip),%rax
mov    (%rax),%rdi
mov    0x10(%rbx),%rdx
lea    0xe07(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r14,%rdi
call   1060 <ftell@plt>
mov    %rax,%r12
mov    0x2d59(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %r12,%r12
jne    12aa <main+0x11a>
mov    0x2d49(%rip),%rax
mov    (%rax),%rcx
lea    0xdcd(%rip),%rdi
mov    $0x11,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    $0x4,%edi
call   10a0 <exit@plt>
mov    %rsp,%r13
mov    %r13,-0x30(%rbp)
lea    0xf(%r12),%rbx
and    $0xfffffffffffffff0,%rbx
sub    %rbx,%r13
mov    %r13,%rsp
neg    %rbx
mov    %r13,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %r14,-0x38(%rbp)
mov    %r14,%rcx
call   1030 <fread@plt>
mov    0x2cdf(%rip),%r14
mov    (%r14),%ecx
mov    %r13,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    (%r14),%edi
call   1070 <fflush@plt>
mov    0x2cdd(%rip),%rax
mov    (%rax),%rcx
mov    %r13,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x30(%rbp),%rax
movzbl (%rax,%rbx,1),%edx
lea    0xd4b(%rip),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1050 <fprintf@plt>
cmp    $0x3,%r12
jl     136c <main+0x1dc>
mov    $0x2,%r14d
lea    0xd3e(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x0(%r13,%r14,1),%edx
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%r14
cmp    %r14,%r12
jne    1350 <main+0x1c0>
lea    0xd11(%rip),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   10b0 <fwrite@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32* null
@.str = [30 x i8] c"usage: %s <in.bin> <out.gdb>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [29 x i8] c"error opening input file %s\0A\00"
@.str.4 = [30 x i8] c"error opening output file %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.5 = [18 x i8] c"empty input file\0A\00"
@stdout = global i32 0
@stdin = global i32* null
@.str.6 = [16 x i8] c"set gdb_rpc={%d\00"
@.str.7 = [4 x i8] c",%d\00"
@.str.8 = [3 x i8] c"}\0A\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 i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i32*, i32** @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %15, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %6
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %29, i32** %7
%30 = load i32*, i32** %6
%31 = icmp ne i32* %30, null
br i1 %31, label %39, label %32
32: ; preds = %21
%33 = load i32*, i32** @stderr
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 (i32*, i8*, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %36)
%38 = call i32 @exit(i32 2)
unreachable
39: ; preds = %21
%40 = load i32*, i32** %7
%41 = icmp ne i32* %40, null
br i1 %41, label %49, label %42
42: ; preds = %39
%43 = load i32*, i32** @stderr
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 2
%46 = load i8*, i8** %45
%47 = call i32 (i32*, i8*, ...) @fprintf(i32* %43, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %46)
%48 = call i32 @exit(i32 3)
unreachable
49: ; preds = %39
%50 = load i32*, i32** %6
%51 = load i32, i32* @SEEK_END
%52 = call i32 @fseek(i32* %50, i64 0, i32 %51)
%53 = load i32*, i32** %6
%54 = call i64 @ftell(i32* %53)
store i64 %54, i64* %8
%55 = load i32*, i32** %6
%56 = load i32, i32* @SEEK_SET
%57 = call i32 @fseek(i32* %55, i64 0, i32 %56)
%58 = load i64, i64* %8
%59 = icmp ne i64 %58, 0
br i1 %59, label %64, label %60
60: ; preds = %49
%61 = load i32*, i32** @stderr
%62 = call i32 (i32*, i8*, ...) @fprintf(i32* %61, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%63 = call i32 @exit(i32 4)
unreachable
64: ; preds = %49
%65 = load i64, i64* %8
%66 = call i8* @llvm.stacksave()
store i8* %66, i8** %9
%67 = alloca i8, i64 %65
store i64 %65, i64* %10
%68 = load i64, i64* %8
%69 = load i32*, i32** %6
%70 = call i32 @fread(i8* %67, i64 %68, i32 1, i32* %69)
%71 = load i64, i64* %8
%72 = load i32, i32* @stdout
%73 = call i32 @fwrite(i8* %67, i64 %71, i32 1, i32 %72)
%74 = load i32, i32* @stdout
%75 = call i32 @fflush(i32 %74)
%76 = load i64, i64* %8
%77 = load i32*, i32** @stdin
%78 = call i32 @fread(i8* %67, i64 %76, i32 1, i32* %77)
%79 = load i32*, i32** %7
%80 = getelementptr inbounds i8, i8* %67, i64 0
%81 = load i8, i8* %80
%82 = zext i8 %81 to i32
%83 = call i32 (i32*, i8*, ...) @fprintf(i32* %79, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %82)
store i32 2, i32* %11
br label %84
84: ; preds = %97, %64
%85 = load i32, i32* %11
%86 = sext i32 %85 to i64
%87 = load i64, i64* %8
%88 = icmp slt i64 %86, %87
br i1 %88, label %89, label %100
89: ; preds = %84
%90 = load i32*, i32** %7
%91 = load i32, i32* %11
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %67, i64 %92
%94 = load i8, i8* %93
%95 = zext i8 %94 to i32
%96 = call i32 (i32*, i8*, ...) @fprintf(i32* %90, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %95)
br label %97
97: ; preds = %89
%98 = load i32, i32* %11
%99 = add nsw i32 %98, 1
store i32 %99, i32* %11
br label %84
100: ; preds = %84
%101 = load i32*, i32** %7
%102 = call i32 (i32*, i8*, ...) @fprintf(i32* %101, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
%103 = load i32*, i32** %7
%104 = call i32 @fclose(i32* %103)
%105 = load i32*, i32** %6
%106 = call i32 @fclose(i32* %105)
%107 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32* null
@.str = [30 x i8] c"usage: %s <in.bin> <out.gdb>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [29 x i8] c"error opening input file %s\0A\00"
@.str.4 = [30 x i8] c"error opening output file %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.5 = [18 x i8] c"empty input file\0A\00"
@stdout = global i32 0
@stdin = global i32* null
@.str.6 = [16 x i8] c"set gdb_rpc={%d\00"
@.str.7 = [4 x i8] c",%d\00"
@.str.8 = [3 x i8] c"}\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i32*, i32** @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32* %12, null
br i1 %16, label %17, label %22
17: ; preds = %9
%18 = load i32*, i32** @stderr
%19 = load i8*, i8** %10
%20 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = tail call i32 @exit(i32 2)
unreachable
22: ; preds = %9
%23 = icmp eq i32* %15, null
br i1 %23, label %24, label %29
24: ; preds = %22
%25 = load i32*, i32** @stderr
%26 = load i8*, i8** %13
%27 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %26)
%28 = tail call i32 @exit(i32 3)
unreachable
29: ; preds = %22
%30 = load i32, i32* @SEEK_END
%31 = tail call i32 @fseek(i32* %12, i64 0, i32 %30)
%32 = tail call i64 @ftell(i32* %12)
%33 = load i32, i32* @SEEK_SET
%34 = tail call i32 @fseek(i32* %12, i64 0, i32 %33)
%35 = icmp eq i64 %32, 0
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = load i32*, i32** @stderr
%38 = tail call i64 bitcast (i32 (i8*, i64, i32, i32)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i64 17, i64 1, i32* %37)
%39 = tail call i32 @exit(i32 4)
unreachable
40: ; preds = %29
%41 = alloca i8, i64 %32
%42 = call i32 @fread(i8* %41, i64 %32, i32 1, i32* %12)
%43 = load i32, i32* @stdout
%44 = call i32 @fwrite(i8* %41, i64 %32, i32 1, i32 %43)
%45 = load i32, i32* @stdout
%46 = call i32 @fflush(i32 %45)
%47 = load i32*, i32** @stdin
%48 = call i32 @fread(i8* %41, i64 %32, i32 1, i32* %47)
%49 = load i8, i8* %41
%50 = zext i8 %49 to i32
%51 = call i32 (i32*, i8*, ...) @fprintf(i32* %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i32 %50)
%52 = icmp sgt i64 %32, 2
br i1 %52, label %53, label %61
53: ; preds = %40, %53
%54 = phi i64 [ %59, %53 ], [ 2, %40 ]
%55 = getelementptr inbounds i8, i8* %41, i64 %54
%56 = load i8, i8* %55
%57 = zext i8 %56 to i32
%58 = call i32 (i32*, i8*, ...) @fprintf(i32* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %57)
%59 = add nuw nsw i64 %54, 1
%60 = icmp eq i64 %59, %32
br i1 %60, label %61, label %53
61: ; preds = %53, %40
%62 = call i64 bitcast (i32 (i8*, i64, i32, i32)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i64 2, i64 1, i32* %15)
%63 = call i32 @fclose(i32* %15)
%64 = call i32 @fclose(i32* %12)
%65 = call i32 @exit(i32 0)
unreachable
}