#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 *alloca(int);
int /*<<< orphan*/ memcpy(int *, void *, int);
int strlen(void *);
int write(int, int *, int);
int ipc_sndmsg(int socket, void *msg, int msg_size) {
  int *b, l;
  if (msg_size < 0)
    msg_size = strlen(msg) + 1;
  l = msg_size + 2 * sizeof(int);
  b = alloca(l);
  if (!b)
    return -1;
  b[0] = 0xbabecafe;
  b[1] = msg_size;
  memcpy(&b[2], msg, msg_size);
  return write(socket, b, l);
}


x86 assembly (clang, -O0):
<ipc_sndmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    116b <ipc_sndmsg+0x2b>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
add    $0x8,%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <alloca@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1199 <ipc_sndmsg+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <ipc_sndmsg+0x93>
mov    -0x20(%rbp),%rax
movl   $0xbabecafe,(%rax)
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_sndmsg>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r15
mov    %edi,%r14d
test   %edx,%edx
jns    1161 <ipc_sndmsg+0x21>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
add    $0x1,%ebp
lea    0x8(%rbp),%r12d
mov    %r12d,%edi
call   1050 <alloca@plt>
test   %rax,%rax
je     11a5 <ipc_sndmsg+0x65>
mov    %rax,%rbx
movl   $0xbabecafe,(%rax)
mov    %ebp,0x4(%rax)
mov    %rax,%rdi
add    $0x8,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1060 <memcpy@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <write@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ipc_sndmsg(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %14, 1
store i32 %15, i32* %7
br label %16
16: ; preds = %12, %3
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = add i64 %18, 8
%20 = trunc i64 %19 to i32
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = call i32* @alloca(i32 %21)
store i32* %22, i32** %8
%23 = load i32*, i32** %8
%24 = icmp ne i32* %23, null
br i1 %24, label %26, label %25
25: ; preds = %16
store i32 -1, i32* %4
br label %41
26: ; preds = %16
%27 = load i32*, i32** %8
%28 = getelementptr inbounds i32, i32* %27, i64 0
store i32 -1161901314, i32* %28
%29 = load i32, i32* %7
%30 = load i32*, i32** %8
%31 = getelementptr inbounds i32, i32* %30, i64 1
store i32 %29, i32* %31
%32 = load i32*, i32** %8
%33 = getelementptr inbounds i32, i32* %32, i64 2
%34 = load i8*, i8** %6
%35 = load i32, i32* %7
%36 = call i32 @memcpy(i32* %33, i8* %34, i32 %35)
%37 = load i32, i32* %5
%38 = load i32*, i32** %8
%39 = load i32, i32* %9
%40 = call i32 @write(i32 %37, i32* %38, i32 %39)
store i32 %40, i32* %4
br label %41
41: ; preds = %26, %25
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
define i32 @ipc_sndmsg(i32 %0, i8* %1, i32 %2) {
%4 = icmp slt i32 %2, 0
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = tail call i32 @strlen(i8* %1)
%7 = add nsw i32 %6, 1
br label %8
8: ; preds = %5, %3
%9 = phi i32 [ %7, %5 ], [ %2, %3 ]
%10 = add i32 %9, 8
%11 = tail call i32* @alloca(i32 %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %18, label %13
13: ; preds = %8
store i32 -1161901314, i32* %11
%14 = getelementptr inbounds i32, i32* %11, i64 1
store i32 %9, i32* %14
%15 = getelementptr inbounds i32, i32* %11, i64 2
%16 = tail call i32 @memcpy(i32* %15, i8* %1, i32 %9)
%17 = tail call i32 @write(i32 %0, i32* %11, i32 %10)
br label %18
18: ; preds = %8, %13
%19 = phi i32 [ %17, %13 ], [ -1, %8 ]
ret i32 %19
}