#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*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ memcpy(unsigned char *, char *, int);
int write_shellcode(FILE *fp, char *string, int string_len, char *label) {
  unsigned char buf[1024];
  int len;
  int i;
  memcpy(buf, (char *)string, string_len);
  len = string_len;
  fprintf(fp, "%s = \"", label);
  for (i = 0; i < len; i++) {
    fprintf(fp, "\\x%02x", buf[i]);
  }
  fprintf(fp, "\"\n\n");
}


x86 assembly (clang, -O0):
<write_shellcode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x434(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe9b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x438(%rbp)
mov    -0x438(%rbp),%eax
cmp    -0x434(%rbp),%eax
jge    11bd <write_shellcode+0x9d>
mov    -0x10(%rbp),%rdi
movslq -0x438(%rbp),%rax
movzbl -0x430(%rbp,%rax,1),%edx
ff
lea    0xe65(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x438(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x438(%rbp)
jmp    1176 <write_shellcode+0x56>
mov    -0x10(%rbp),%rdi
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_shellcode>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rcx,%r14
mov    %edx,%ebp
mov    %rdi,%r15
mov    %rsp,%rdi
call   1040 <memcpy@plt>
lea    0xeac(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
test   %ebp,%ebp
jle    118a <write_shellcode+0x5a>
mov    %ebp,%ebp
lea    0xe99(%rip),%r14
xor    %ebx,%ebx
movzbl (%rsp,%rbx,1),%edx
mov    %r15,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1170 <write_shellcode+0x40>
lea    0xe7d(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1050 <fwrite@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%s = \22\00"
@.str.1 = [7 x i8] c"\\x%02x\00"
@.str.2 = [4 x i8] c"\22\0A\0A\00"
define i32 @write_shellcode(i32* %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca [1024 x i8]
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%14 = load i8*, i8** %7
%15 = load i32, i32* %8
%16 = call i32 @memcpy(i8* %13, i8* %14, i32 %15)
%17 = load i32, i32* %8
store i32 %17, i32* %11
%18 = load i32*, i32** %6
%19 = load i8*, i8** %9
%20 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 0, i32* %12
br label %21
21: ; preds = %33, %4
%22 = load i32, i32* %12
%23 = load i32, i32* %11
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %36
25: ; preds = %21
%26 = load i32*, i32** %6
%27 = load i32, i32* %12
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %25
%34 = load i32, i32* %12
%35 = add nsw i32 %34, 1
store i32 %35, i32* %12
br label %21
36: ; preds = %21
%37 = load i32*, i32** %6
%38 = call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s = \22\00"
@.str.1 = [7 x i8] c"\\x%02x\00"
@.str.2 = [4 x i8] c"\22\0A\0A\00"
define i32 @write_shellcode(i32* nocapture %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = call i32 @memcpy(i8* %6, i8* %1, i32 %2)
%8 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %3)
%9 = icmp sgt i32 %2, 0
br i1 %9, label %10, label %20
10: ; preds = %4
%11 = zext i32 %2 to i64
br label %12
12: ; preds = %10, %12
%13 = phi i64 [ 0, %10 ], [ %18, %12 ]
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = add nuw nsw i64 %13, 1
%19 = icmp eq i64 %18, %11
br i1 %19, label %20, label %12
20: ; preds = %12, %4
%21 = call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 3, i64 1, i32* %0)
ret i32 undef
}