#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 uint8_t;
typedef int uint32_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memcpy(char *, int *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int write(int, char *, int);
void ipc_send_message(int sockfd, uint32_t message_size, uint32_t message_type,
                      uint8_t *payload) {
  int buffer_size =
      strlen("i3-ipc") + sizeof(uint32_t) + sizeof(uint32_t) + message_size;
  char msg[buffer_size];
  char *walk = msg;
  strcpy(walk, "i3-ipc");
  walk += strlen("i3-ipc");
  memcpy(walk, &message_size, sizeof(uint32_t));
  walk += sizeof(uint32_t);
  memcpy(walk, &message_type, sizeof(uint32_t));
  walk += sizeof(uint32_t);
  memcpy(walk, payload, message_size);
  int sent_bytes = 0;
  int bytes_to_go = buffer_size;
  while (sent_bytes < bytes_to_go) {
    int n = write(sockfd, msg + sent_bytes, bytes_to_go);
    if (n == -1)
      err(EXIT_FAILURE, "write() failed");
    sent_bytes += n;
    bytes_to_go -= n;
  }
}


x86 assembly (clang, -O0):
<ipc_send_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
lea    0xe94(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%edx
mov    %ecx,%eax
mov    %edx,%ecx
lea    0x8(%rax,%rcx,1),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x50(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x30(%rbp)
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1030 <strcpy@plt>
lea    0xe3c(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x40(%rbp),%eax
jge    128f <ipc_send_message+0x13f>
mov    -0x50(%rbp),%rsi
mov    -0x4(%rbp),%edi
movslq -0x3c(%rbp),%rax
add    %rax,%rsi
mov    -0x40(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1276 <ipc_send_message+0x126>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
call   1070 <err@plt>
mov    -0x44(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x44(%rbp),%ecx
mov    -0x40(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x40(%rbp)
jmp    1232 <ipc_send_message+0xe2>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_send_message>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,-0x38(%rbp)
mov    %esi,%ebx
mov    %edi,%r14d
mov    %esi,-0x2c(%rbp)
mov    %edx,-0x30(%rbp)
lea    0xe89(%rip),%r12
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rbx,1),%r13d
add    $0x8,%r13d
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
movslq %eax,%rbx
lea    (%r15,%rbx,1),%rdi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
lea    (%rbx,%r15,1),%rdi
add    $0x4,%rdi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
lea    (%rbx,%r15,1),%rdi
add    $0x8,%rdi
mov    -0x2c(%rbp),%edx
mov    -0x38(%rbp),%rsi
call   1060 <memcpy@plt>
test   %r13d,%r13d
jle    123a <ipc_send_message+0xea>
xor    %r12d,%r12d
jmp    120b <ipc_send_message+0xbb>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %ebx,%r12d
sub    %ebx,%r13d
cmp    %r13d,%r12d
jge    123a <ipc_send_message+0xea>
movslq %r12d,%rsi
add    %r15,%rsi
mov    %r14d,%edi
mov    %r13d,%edx
call   1040 <write@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1200 <ipc_send_message+0xb0>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
call   1070 <err@plt>
jmp    1200 <ipc_send_message+0xb0>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"i3-ipc\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [15 x i8] c"write() failed\00"
define void @ipc_send_message(i32 %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%16 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = sext i32 %16 to i64
%18 = add i64 %17, 4
%19 = add i64 %18, 4
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = add i64 %19, %21
%23 = trunc i64 %22 to i32
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = zext i32 %24 to i64
%26 = call i8* @llvm.stacksave()
store i8* %26, i8** %10
%27 = alloca i8, i64 %25
store i64 %25, i64* %11
store i8* %27, i8** %12
%28 = load i8*, i8** %12
%29 = call i32 @strcpy(i8* %28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%31 = load i8*, i8** %12
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8* %33, i8** %12
%34 = load i8*, i8** %12
%35 = call i32 @memcpy(i8* %34, i32* %6, i32 4)
%36 = load i8*, i8** %12
%37 = getelementptr inbounds i8, i8* %36, i64 4
store i8* %37, i8** %12
%38 = load i8*, i8** %12
%39 = call i32 @memcpy(i8* %38, i32* %7, i32 4)
%40 = load i8*, i8** %12
%41 = getelementptr inbounds i8, i8* %40, i64 4
store i8* %41, i8** %12
%42 = load i8*, i8** %12
%43 = load i32*, i32** %8
%44 = load i32, i32* %6
%45 = call i32 @memcpy(i8* %42, i32* %43, i32 %44)
store i32 0, i32* %13
%46 = load i32, i32* %9
store i32 %46, i32* %14
br label %47
47: ; preds = %63, %4
%48 = load i32, i32* %13
%49 = load i32, i32* %14
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %70
51: ; preds = %47
%52 = load i32, i32* %5
%53 = load i32, i32* %13
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %27, i64 %54
%56 = load i32, i32* %14
%57 = call i32 @write(i32 %52, i8* %55, i32 %56)
store i32 %57, i32* %15
%58 = load i32, i32* %15
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %63
60: ; preds = %51
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @err(i32 %61, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %63
63: ; preds = %60, %51
%64 = load i32, i32* %15
%65 = load i32, i32* %13
%66 = add nsw i32 %65, %64
store i32 %66, i32* %13
%67 = load i32, i32* %15
%68 = load i32, i32* %14
%69 = sub nsw i32 %68, %67
store i32 %69, i32* %14
br label %47
70: ; preds = %47
%71 = load i8*, i8** %10
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"i3-ipc\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [15 x i8] c"write() failed\00"
define void @ipc_send_message(i32 %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
store i32 %1, i32* %5
store i32 %2, i32* %6
%7 = tail call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%8 = add i32 %7, 8
%9 = add i32 %8, %1
%10 = zext i32 %9 to i64
%11 = alloca i8, i64 %10
%12 = call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %11, i64 %14
%16 = call i32 @memcpy(i8* %15, i32* %5, i32 4)
%17 = getelementptr inbounds i8, i8* %15, i64 4
%18 = call i32 @memcpy(i8* %17, i32* %6, i32 4)
%19 = getelementptr inbounds i8, i8* %17, i64 4
%20 = load i32, i32* %5
%21 = call i32 @memcpy(i8* %19, i32* %3, i32 %20)
%22 = icmp sgt i32 %9, 0
br i1 %22, label %23, label %37
23: ; preds = %4, %33
%24 = phi i32 [ %34, %33 ], [ 0, %4 ]
%25 = phi i32 [ %35, %33 ], [ %9, %4 ]
%26 = sext i32 %24 to i64
%27 = getelementptr inbounds i8, i8* %11, i64 %26
%28 = call i32 @write(i32 %0, i8* %27, i32 %25)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @err(i32 %31, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %30, %23
%34 = add nsw i32 %28, %24
%35 = sub nsw i32 %25, %28
%36 = icmp slt i32 %34, %35
br i1 %36, label %23, label %37
37: ; preds = %33, %4
ret void
}