#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 MAX_VINT_SIZE;
int int_endian_swap(int);
int /*<<< orphan*/ printf(char *);
int write_all(int, char const *, int);
int write_raw_varint32(char *, int);
int write_request(int socket_id, const char *header, int header_len,
                  const char *request, int request_len) {
  const MAX_VINT_SIZE = 8;
  char header_len_buffer[MAX_VINT_SIZE];
  char request_len_buffer[MAX_VINT_SIZE];
  int rc;
  int header_vint_len = write_raw_varint32(&(header_len_buffer[0]), header_len);
  int request_vint_len =
      write_raw_varint32(&(request_len_buffer[0]), request_len);
  int total_len = int_endian_swap(header_vint_len + header_len +
                                  request_vint_len + request_len);
  rc = write_all(socket_id, (char *)(&total_len), 4);
  if (0 != rc) {
    printf("write total length failed.\n");
    return -1;
  }
  rc = write_all(socket_id, header_len_buffer, header_vint_len);
  if (0 != rc) {
    printf("write header vint failed.\n");
    return -1;
  }
  rc = write_all(socket_id, header, header_len);
  if (0 != rc) {
    printf("write header buffer failed.\n");
    return -1;
  }
  rc = write_all(socket_id, request_len_buffer, request_vint_len);
  if (0 != rc) {
    printf("write request vint failed.\n");
    return -1;
  }
  rc = write_all(socket_id, request, request_len);
  if (0 != rc) {
    printf("write request buffer failed.\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movl   $0x8,-0x28(%rbp)
mov    %rsp,%rax
mov    %rax,-0x30(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <write_raw_varint32@plt>
mov    %eax,-0x38(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1050 <write_raw_varint32@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%edi
add    -0x14(%rbp),%edi
add    -0x3c(%rbp),%edi
add    -0x24(%rbp),%edi
call   1040 <int_endian_swap@plt>
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x4,%edx
call   1060 <write_all@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmp    -0x34(%rbp),%eax
je     11d8 <write_request+0x98>
lea    0xe40(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12d6 <write_request+0x196>
mov    -0x8(%rbp),%edi
mov    -0x38(%rbp),%edx
lea    -0x50(%rbp),%rsi
call   1060 <write_all@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmp    -0x34(%rbp),%eax
je     1214 <write_request+0xd4>
lea    0xe20(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12d6 <write_request+0x196>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <write_all@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmp    -0x34(%rbp),%eax
je     1250 <write_request+0x110>
lea    0xdff(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12d6 <write_request+0x196>
mov    -0x8(%rbp),%edi
mov    -0x3c(%rbp),%edx
lea    -0x60(%rbp),%rsi
call   1060 <write_all@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmp    -0x34(%rbp),%eax
je     128c <write_request+0x14c>
lea    0xde0(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12d6 <write_request+0x196>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <write_all@plt>
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmp    -0x34(%rbp),%eax
je     12c8 <write_request+0x188>
lea    0xdc0(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    12d6 <write_request+0x196>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_request>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8d,%ebx
mov    %rcx,0x30(%rsp)
mov    %edx,%ebp
mov    %rsi,%r12
mov    %edi,%r15d
lea    0x20(%rsp),%rdi
mov    %edx,%esi
call   1050 <write_raw_varint32@plt>
mov    %eax,%r14d
lea    0x10(%rsp),%rdi
mov    %ebx,%esi
call   1050 <write_raw_varint32@plt>
mov    %eax,%r13d
lea    (%rbx,%rbp,1),%edi
add    %r14d,%edi
add    %eax,%edi
call   1040 <int_endian_swap@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %r15d,%edi
mov    $0x4,%edx
call   1060 <write_all@plt>
test   %eax,%eax
je     11ac <write_request+0x6c>
lea    0xec4(%rip),%rdi
jmp    121a <write_request+0xda>
lea    0x20(%rsp),%rsi
mov    %r15d,%edi
mov    %r14d,%edx
call   1060 <write_all@plt>
test   %eax,%eax
je     11c9 <write_request+0x89>
lea    0xe8d(%rip),%rdi
jmp    121a <write_request+0xda>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1060 <write_all@plt>
test   %eax,%eax
je     11e3 <write_request+0xa3>
lea    0xe57(%rip),%rdi
jmp    121a <write_request+0xda>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    %r13d,%edx
call   1060 <write_all@plt>
test   %eax,%eax
je     1200 <write_request+0xc0>
lea    0xe1f(%rip),%rdi
jmp    121a <write_request+0xda>
mov    %r15d,%edi
mov    0x30(%rsp),%rsi
mov    %ebx,%edx
call   1060 <write_all@plt>
test   %eax,%eax
je     1233 <write_request+0xf3>
lea    0xde6(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    1224 <write_request+0xe4>

LLVM IR (-O0):
@.str = [28 x i8] c"write total length failed.\0A\00"
@.str.1 = [27 x i8] c"write header vint failed.\0A\00"
@.str.2 = [29 x i8] c"write header buffer failed.\0A\00"
@.str.3 = [28 x i8] c"write request vint failed.\0A\00"
@.str.4 = [30 x i8] c"write request buffer failed.\0A\00"
@MAX_VINT_SIZE = global i32 0
define i32 @write_request(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 %4, i32* %11
store i32 8, i32* %12
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %13
%20 = alloca i8, i64 8
%21 = alloca i8, i64 8
%22 = getelementptr inbounds i8, i8* %20, i64 0
%23 = load i32, i32* %9
%24 = call i32 @write_raw_varint32(i8* %22, i32 %23)
store i32 %24, i32* %15
%25 = getelementptr inbounds i8, i8* %21, i64 0
%26 = load i32, i32* %11
%27 = call i32 @write_raw_varint32(i8* %25, i32 %26)
store i32 %27, i32* %16
%28 = load i32, i32* %15
%29 = load i32, i32* %9
%30 = add nsw i32 %28, %29
%31 = load i32, i32* %16
%32 = add nsw i32 %30, %31
%33 = load i32, i32* %11
%34 = add nsw i32 %32, %33
%35 = call i32 @int_endian_swap(i32 %34)
store i32 %35, i32* %17
%36 = load i32, i32* %7
%37 = bitcast i32* %17 to i8*
%38 = call i32 @write_all(i32 %36, i8* %37, i32 4)
store i32 %38, i32* %14
%39 = load i32, i32* %14
%40 = icmp ne i32 0, %39
br i1 %40, label %41, label %43
41: ; preds = %5
%42 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
store i32 1, i32* %18
br label %78
43: ; preds = %5
%44 = load i32, i32* %7
%45 = load i32, i32* %15
%46 = call i32 @write_all(i32 %44, i8* %20, i32 %45)
store i32 %46, i32* %14
%47 = load i32, i32* %14
%48 = icmp ne i32 0, %47
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
store i32 1, i32* %18
br label %78
51: ; preds = %43
%52 = load i32, i32* %7
%53 = load i8*, i8** %8
%54 = load i32, i32* %9
%55 = call i32 @write_all(i32 %52, i8* %53, i32 %54)
store i32 %55, i32* %14
%56 = load i32, i32* %14
%57 = icmp ne i32 0, %56
br i1 %57, label %58, label %60
58: ; preds = %51
%59 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %6
store i32 1, i32* %18
br label %78
60: ; preds = %51
%61 = load i32, i32* %7
%62 = load i32, i32* %16
%63 = call i32 @write_all(i32 %61, i8* %21, i32 %62)
store i32 %63, i32* %14
%64 = load i32, i32* %14
%65 = icmp ne i32 0, %64
br i1 %65, label %66, label %68
66: ; preds = %60
%67 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %6
store i32 1, i32* %18
br label %78
68: ; preds = %60
%69 = load i32, i32* %7
%70 = load i8*, i8** %10
%71 = load i32, i32* %11
%72 = call i32 @write_all(i32 %69, i8* %70, i32 %71)
store i32 %72, i32* %14
%73 = load i32, i32* %14
%74 = icmp ne i32 0, %73
br i1 %74, label %75, label %77
75: ; preds = %68
%76 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %6
store i32 1, i32* %18
br label %78
77: ; preds = %68
store i32 0, i32* %6
store i32 1, i32* %18
br label %78
78: ; preds = %77, %75, %66, %58, %49, %41
%79 = load i8*, i8** %13
%80 = load i32, i32* %6
ret i32 %80
}

LLVM IR (-O2):
@MAX_VINT_SIZE = global i32 0
@str = [29 x i8] c"write request buffer failed.\00"
@str.5 = [27 x i8] c"write request vint failed.\00"
@str.6 = [28 x i8] c"write header buffer failed.\00"
@str.7 = [26 x i8] c"write header vint failed.\00"
@str.8 = [27 x i8] c"write total length failed.\00"
define i32 @write_request(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca [8 x i8]
%8 = alloca [8 x i8]
%9 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%10 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%11 = call i32 @write_raw_varint32(i8* %10, i32 %2)
%12 = call i32 @write_raw_varint32(i8* %9, i32 %4)
%13 = bitcast i32* %6 to i8*
%14 = add i32 %4, %2
%15 = add i32 %14, %11
%16 = add i32 %15, %12
%17 = call i32 @int_endian_swap(i32 %16)
store i32 %17, i32* %6
%18 = call i32 @write_all(i32 %0, i8* %13, i32 4)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %32
20: ; preds = %5
%21 = call i32 @write_all(i32 %0, i8* %10, i32 %11)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %20
%24 = call i32 @write_all(i32 %0, i8* %1, i32 %2)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = call i32 @write_all(i32 %0, i8* %9, i32 %12)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = call i32 @write_all(i32 %0, i8* %3, i32 %4)
%31 = icmp eq i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %29, %26, %23, %20, %5
%33 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0), %5 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0), %20 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @str.6, i64 0, i64 0), %23 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0), %26 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0), %29 ]
%34 = call i32 @puts(i8* dereferenceable(1) %33)
br label %35
35: ; preds = %32, %29
%36 = phi i32 [ 0, %29 ], [ -1, %32 ]
ret i32 %36
}