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

/* Variables and functions */
int /*<<< orphan*/ die();
int /*<<< orphan*/ err_msg(char *);
unsigned char *raw_linux_return();
unsigned int raw_linux_return_size();
int /*<<< orphan*/ write_int(unsigned char *, unsigned int);
void output_os_return(unsigned char *buf, unsigned int buf_size,
                      unsigned int *bytes_written) {
  unsigned char *bytes;
  unsigned int i;
  unsigned int length;
  length = raw_linux_return_size();
  if (buf_size < *bytes_written + length) {
    err_msg("buf_size too small for output_os_return\n");
    die();
  }
  bytes = raw_linux_return();
  for (i = 0; i < length; i++) {
    buf[(*bytes_written)++] = bytes[i];
  }
  length = *bytes_written;
  length += length % 4;
  write_int(&buf[68], length);
  write_int(&buf[72], length);
}


x86 assembly (clang, -O0):
<output_os_return>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1070 <raw_linux_return_size@plt>
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%ecx
add    -0x28(%rbp),%ecx
cmp    %ecx,%eax
jae    1194 <output_os_return+0x44>
lea    0xe78(%rip),%rdi
call   1060 <err_msg@plt>
mov    $0x0,%al
call   1050 <die@plt>
mov    $0x0,%al
call   1030 <raw_linux_return@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jae    11e0 <output_os_return+0x90>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rsi
mov    (%rsi),%ecx
mov    %ecx,%edi
add    $0x1,%edi
mov    %edi,(%rsi)
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a6 <output_os_return+0x56>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
and    $0x3,%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x44,%rdi
mov    -0x28(%rbp),%esi
call   1040 <write_int@plt>
mov    -0x8(%rbp),%rdi
add    $0x48,%rdi
mov    -0x28(%rbp),%esi
call   1040 <write_int@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<output_os_return>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r14
xor    %eax,%eax
call   1070 <raw_linux_return_size@plt>
mov    %eax,%ebp
mov    (%rbx),%eax
add    %ebp,%eax
cmp    %r15d,%eax
jbe    1185 <output_os_return+0x35>
lea    0xe87(%rip),%rdi
call   1060 <err_msg@plt>
xor    %eax,%eax
call   1050 <die@plt>
xor    %eax,%eax
call   1030 <raw_linux_return@plt>
test   %ebp,%ebp
je     11ec <output_os_return+0x9c>
mov    %ebp,%r8d
cmp    $0x1,%ebp
jne    119c <output_os_return+0x4c>
xor    %edx,%edx
jmp    11d8 <output_os_return+0x88>
mov    %r8d,%esi
and    $0xfffffffe,%esi
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl (%rax,%rdx,1),%ecx
mov    (%rbx),%ebp
lea    0x1(%rbp),%edi
mov    %edi,(%rbx)
mov    %cl,(%r14,%rbp,1)
movzbl 0x1(%rax,%rdx,1),%ecx
mov    (%rbx),%edi
lea    0x1(%rdi),%ebp
mov    %ebp,(%rbx)
mov    %cl,(%r14,%rdi,1)
add    $0x2,%rdx
cmp    %rdx,%rsi
jne    11b0 <output_os_return+0x60>
test   $0x1,%r8b
je     11ec <output_os_return+0x9c>
mov    (%rax,%rdx,1),%al
mov    (%rbx),%ecx
lea    0x1(%rcx),%edx
mov    %edx,(%rbx)
mov    %al,(%r14,%rcx,1)
mov    (%rbx),%eax
mov    %eax,%ebx
and    $0x3,%ebx
add    %eax,%ebx
lea    0x44(%r14),%rdi
mov    %ebx,%esi
call   1040 <write_int@plt>
add    $0x48,%r14
mov    %r14,%rdi
mov    %ebx,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <write_int@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"buf_size too small for output_os_return\0A\00"
define void @output_os_return(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%10 = call i32 (...) @raw_linux_return_size()
store i32 %10, i32* %9
%11 = load i32, i32* %5
%12 = load i32*, i32** %6
%13 = load i32, i32* %12
%14 = load i32, i32* %9
%15 = add i32 %13, %14
%16 = icmp ult i32 %11, %15
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = call i32 @err_msg(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%19 = call i32 (...) @die()
br label %20
20: ; preds = %17, %3
%21 = call i8* (...) @raw_linux_return()
store i8* %21, i8** %7
store i32 0, i32* %8
br label %22
22: ; preds = %38, %20
%23 = load i32, i32* %8
%24 = load i32, i32* %9
%25 = icmp ult i32 %23, %24
br i1 %25, label %26, label %41
26: ; preds = %22
%27 = load i8*, i8** %7
%28 = load i32, i32* %8
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = load i8*, i8** %4
%33 = load i32*, i32** %6
%34 = load i32, i32* %33
%35 = add i32 %34, 1
store i32 %35, i32* %33
%36 = zext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %32, i64 %36
store i8 %31, i8* %37
br label %38
38: ; preds = %26
%39 = load i32, i32* %8
%40 = add i32 %39, 1
store i32 %40, i32* %8
br label %22
41: ; preds = %22
%42 = load i32*, i32** %6
%43 = load i32, i32* %42
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = urem i32 %44, 4
%46 = load i32, i32* %9
%47 = add i32 %46, %45
store i32 %47, i32* %9
%48 = load i8*, i8** %4
%49 = getelementptr inbounds i8, i8* %48, i64 68
%50 = load i32, i32* %9
%51 = call i32 @write_int(i8* %49, i32 %50)
%52 = load i8*, i8** %4
%53 = getelementptr inbounds i8, i8* %52, i64 72
%54 = load i32, i32* %9
%55 = call i32 @write_int(i8* %53, i32 %54)
ret void
}

LLVM IR (-O2):
@.str = [41 x i8] c"buf_size too small for output_os_return\0A\00"
define void @output_os_return(i8* %0, i32 %1, i32* nocapture %2) {
%4 = tail call i32 (...) @raw_linux_return_size()
%5 = load i32, i32* %2
%6 = add i32 %5, %4
%7 = icmp ugt i32 %6, %1
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = tail call i32 @err_msg(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 (...) @die()
br label %11
11: ; preds = %8, %3
%12 = tail call i8* (...) @raw_linux_return()
%13 = icmp eq i32 %4, 0
br i1 %13, label %49, label %14
14: ; preds = %11
%15 = zext i32 %4 to i64
%16 = and i64 %15, 1
%17 = icmp eq i32 %4, 1
br i1 %17, label %39, label %18
18: ; preds = %14
%19 = and i64 %15, 4294967294
br label %20
20: ; preds = %20, %18
%21 = phi i64 [ 0, %18 ], [ %36, %20 ]
%22 = phi i64 [ 0, %18 ], [ %37, %20 ]
%23 = getelementptr inbounds i8, i8* %12, i64 %21
%24 = load i8, i8* %23
%25 = load i32, i32* %2
%26 = add i32 %25, 1
store i32 %26, i32* %2
%27 = zext i32 %25 to i64
%28 = getelementptr inbounds i8, i8* %0, i64 %27
store i8 %24, i8* %28
%29 = or i64 %21, 1
%30 = getelementptr inbounds i8, i8* %12, i64 %29
%31 = load i8, i8* %30
%32 = load i32, i32* %2
%33 = add i32 %32, 1
store i32 %33, i32* %2
%34 = zext i32 %32 to i64
%35 = getelementptr inbounds i8, i8* %0, i64 %34
store i8 %31, i8* %35
%36 = add nuw nsw i64 %21, 2
%37 = add i64 %22, 2
%38 = icmp eq i64 %37, %19
br i1 %38, label %39, label %20
39: ; preds = %20, %14
%40 = phi i64 [ 0, %14 ], [ %36, %20 ]
%41 = icmp eq i64 %16, 0
br i1 %41, label %49, label %42
42: ; preds = %39
%43 = getelementptr inbounds i8, i8* %12, i64 %40
%44 = load i8, i8* %43
%45 = load i32, i32* %2
%46 = add i32 %45, 1
store i32 %46, i32* %2
%47 = zext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %0, i64 %47
store i8 %44, i8* %48
br label %49
49: ; preds = %42, %39, %11
%50 = load i32, i32* %2
%51 = and i32 %50, 3
%52 = add i32 %51, %50
%53 = getelementptr inbounds i8, i8* %0, i64 68
%54 = tail call i32 @write_int(i8* %53, i32 %52)
%55 = getelementptr inbounds i8, i8* %0, i64 72
%56 = tail call i32 @write_int(i8* %55, i32 %52)
ret void
}