#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 */
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned long);
int /*<<< orphan*/ perror(char const *);
int create_gdb_commands(const char *filename, unsigned long num_bytes) {
  FILE *cmd_fp;
  cmd_fp = fopen(filename, "w");
  if (cmd_fp == NULL) {
    perror(filename);
    return -1;
  }
  fprintf(cmd_fp,
          "set m68k on\n"
          "disassemble main main+%lu\n"
          "quit\n",
          num_bytes);
  if (fclose(cmd_fp)) {
    perror(filename);
    return -2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<create_gdb_commands>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1184 <create_gdb_commands+0x44>
mov    -0x10(%rbp),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <create_gdb_commands+0x87>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe6f(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     11c0 <create_gdb_commands+0x80>
mov    -0x10(%rbp),%rdi
call   1060 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11c7 <create_gdb_commands+0x87>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_gdb_commands>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xeaf(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1186 <create_gdb_commands+0x46>
mov    %rax,%rbx
lea    0xe9d(%rip),%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    118d <create_gdb_commands+0x4d>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xffffffff,%ebp
jmp    1192 <create_gdb_commands+0x52>
mov    $0xfffffffe,%ebp
mov    %r14,%rdi
call   1060 <perror@plt>
jmp    117f <create_gdb_commands+0x3f>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [44 x i8] c"set m68k on\0Adisassemble main main+%lu\0Aquit\0A\00"
define i32 @create_gdb_commands(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i8*, i8** %4
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i32 @perror(i8* %12)
store i32 -1, i32* %3
br label %25
14: ; preds = %2
%15 = load i32*, i32** %6
%16 = load i64, i64* %5
%17 = call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 %16)
%18 = load i32*, i32** %6
%19 = call i64 @fclose(i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = load i8*, i8** %4
%23 = call i32 @perror(i8* %22)
store i32 -2, i32* %3
br label %25
24: ; preds = %14
store i32 0, i32* %3
br label %25
25: ; preds = %24, %21, %11
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [44 x i8] c"set m68k on\0Adisassemble main main+%lu\0Aquit\0A\00"
define i32 @create_gdb_commands(i8* nocapture %0, i64 %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = tail call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 %1)
%7 = tail call i64 @fclose(i32* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %5, %2
%10 = phi i32 [ -1, %2 ], [ -2, %5 ]
%11 = tail call i32 @perror(i8* %0)
br label %12
12: ; preds = %9, %5
%13 = phi i32 [ 0, %5 ], [ %10, %9 ]
ret i32 %13
}