#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 */
typedef struct TYPE_7__ TYPE_2__;
typedef struct TYPE_6__ TYPE_1__;

/* Type definitions */
typedef TYPE_1__ *XdmcpHeaderPtr;
typedef TYPE_2__ *XdmcpBufferPtr;
struct TYPE_7__ {
  int size;
  scalar_t__ pointer;
  int /*<<< orphan*/ *data;
};
struct TYPE_6__ {
  scalar_t__ length;
  scalar_t__ opcode;
  scalar_t__ version;
};
typedef int /*<<< orphan*/ BYTE;

/* Variables and functions */
int FALSE;
int TRUE;
int XDM_MAX_MSGLEN;
scalar_t__ Xalloc(int);
int /*<<< orphan*/ XdmcpWriteCARD16(TYPE_2__ *, scalar_t__);
int /*<<< orphan*/ Xfree(unsigned long *);
int XdmcpWriteHeader(XdmcpBufferPtr buffer, XdmcpHeaderPtr header) {
  BYTE *newData;
  if ((int)buffer->size < 6 + (int)header->length) {
    newData = (BYTE *)Xalloc(XDM_MAX_MSGLEN * sizeof(BYTE));
    if (!newData)
      return FALSE;
    Xfree((unsigned long *)(buffer->data));
    buffer->data = newData;
    buffer->size = XDM_MAX_MSGLEN;
  }
  buffer->pointer = 0;
  if (!XdmcpWriteCARD16(buffer, header->version))
    return FALSE;
  if (!XdmcpWriteCARD16(buffer, header->opcode))
    return FALSE;
  if (!XdmcpWriteCARD16(buffer, header->length))
    return FALSE;
  return TRUE;
}


x86 assembly (clang, -O0):
<XdmcpWriteHeader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
add    $0x6,%ecx
cmp    %ecx,%eax
jge    11b5 <XdmcpWriteHeader+0x85>
mov    0x2e79(%rip),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <Xalloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118d <XdmcpWriteHeader+0x5d>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    124d <XdmcpWriteHeader+0x11d>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <Xfree@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    0x2e2b(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
movq   $0x0,0x8(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rsi
call   1040 <XdmcpWriteCARD16@plt>
cmp    $0x0,%eax
jne    11ec <XdmcpWriteHeader+0xbc>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    124d <XdmcpWriteHeader+0x11d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rsi
call   1040 <XdmcpWriteCARD16@plt>
cmp    $0x0,%eax
jne    1217 <XdmcpWriteHeader+0xe7>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    124d <XdmcpWriteHeader+0x11d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1040 <XdmcpWriteCARD16@plt>
cmp    $0x0,%eax
jne    1241 <XdmcpWriteHeader+0x111>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    124d <XdmcpWriteHeader+0x11d>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<XdmcpWriteHeader>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rsi),%eax
add    $0x6,%eax
cmp    %eax,(%rdi)
jge    1175 <XdmcpWriteHeader+0x45>
mov    0x2e8a(%rip),%r12
mov    (%r12),%edi
shl    $0x2,%edi
call   1050 <Xalloc@plt>
test   %rax,%rax
je     11b3 <XdmcpWriteHeader+0x83>
mov    %rax,%r15
mov    0x10(%rbx),%rdi
call   1030 <Xfree@plt>
mov    %r15,0x10(%rbx)
mov    (%r12),%eax
mov    %eax,(%rbx)
movq   $0x0,0x8(%rbx)
mov    0x10(%r14),%rsi
mov    %rbx,%rdi
call   1040 <XdmcpWriteCARD16@plt>
mov    0x2e40(%rip),%r15
test   %eax,%eax
je     11c3 <XdmcpWriteHeader+0x93>
mov    0x8(%r14),%rsi
mov    %rbx,%rdi
call   1040 <XdmcpWriteCARD16@plt>
test   %eax,%eax
je     11c3 <XdmcpWriteHeader+0x93>
mov    (%r14),%rsi
mov    %rbx,%rdi
call   1040 <XdmcpWriteCARD16@plt>
test   %eax,%eax
jne    11bc <XdmcpWriteHeader+0x8c>
mov    0x2e16(%rip),%r15
jmp    11c3 <XdmcpWriteHeader+0x93>
mov    0x2e25(%rip),%r15
mov    (%r15),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.TYPE_7__ = type { i32, i64, i32* }
%struct.TYPE_6__ = type { i64, i64, i64 }
@XDM_MAX_MSGLEN = global i32 0
@FALSE = global i32 0
@TRUE = global i32 0
define i32 @XdmcpWriteHeader(%struct.TYPE_7__* %0, %struct.TYPE_6__* %1) {
%3 = alloca i32
%4 = alloca %struct.TYPE_7__*
%5 = alloca %struct.TYPE_6__*
%6 = alloca i32*
store %struct.TYPE_7__* %0, %struct.TYPE_7__** %4
store %struct.TYPE_6__* %1, %struct.TYPE_6__** %5
%7 = load %struct.TYPE_7__*, %struct.TYPE_7__** %4
%8 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %7, i32 0, i32 0
%9 = load i32, i32* %8
%10 = load %struct.TYPE_6__*, %struct.TYPE_6__** %5
%11 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %10, i32 0, i32 0
%12 = load i64, i64* %11
%13 = trunc i64 %12 to i32
%14 = add nsw i32 6, %13
%15 = icmp slt i32 %9, %14
br i1 %15, label %16, label %39
16: ; preds = %2
%17 = load i32, i32* @XDM_MAX_MSGLEN
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 4
%20 = trunc i64 %19 to i32
%21 = call i64 @Xalloc(i32 %20)
%22 = inttoptr i64 %21 to i32*
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp ne i32* %23, null
br i1 %24, label %27, label %25
25: ; preds = %16
%26 = load i32, i32* @FALSE
store i32 %26, i32* %3
br label %70
27: ; preds = %16
%28 = load %struct.TYPE_7__*, %struct.TYPE_7__** %4
%29 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %28, i32 0, i32 2
%30 = load i32*, i32** %29
%31 = bitcast i32* %30 to i64*
%32 = call i32 @Xfree(i64* %31)
%33 = load i32*, i32** %6
%34 = load %struct.TYPE_7__*, %struct.TYPE_7__** %4
%35 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %34, i32 0, i32 2
store i32* %33, i32** %35
%36 = load i32, i32* @XDM_MAX_MSGLEN
%37 = load %struct.TYPE_7__*, %struct.TYPE_7__** %4
%38 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %37, i32 0, i32 0
store i32 %36, i32* %38
br label %39
39: ; preds = %27, %2
%40 = load %struct.TYPE_7__*, %struct.TYPE_7__** %4
%41 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %40, i32 0, i32 1
store i64 0, i64* %41
%42 = load %struct.TYPE_7__*, %struct.TYPE_7__** %4
%43 = load %struct.TYPE_6__*, %struct.TYPE_6__** %5
%44 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %43, i32 0, i32 2
%45 = load i64, i64* %44
%46 = call i32 @XdmcpWriteCARD16(%struct.TYPE_7__* %42, i64 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %50, label %48
48: ; preds = %39
%49 = load i32, i32* @FALSE
store i32 %49, i32* %3
br label %70
50: ; preds = %39
%51 = load %struct.TYPE_7__*, %struct.TYPE_7__** %4
%52 = load %struct.TYPE_6__*, %struct.TYPE_6__** %5
%53 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %52, i32 0, i32 1
%54 = load i64, i64* %53
%55 = call i32 @XdmcpWriteCARD16(%struct.TYPE_7__* %51, i64 %54)
%56 = icmp ne i32 %55, 0
br i1 %56, label %59, label %57
57: ; preds = %50
%58 = load i32, i32* @FALSE
store i32 %58, i32* %3
br label %70
59: ; preds = %50
%60 = load %struct.TYPE_7__*, %struct.TYPE_7__** %4
%61 = load %struct.TYPE_6__*, %struct.TYPE_6__** %5
%62 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %61, i32 0, i32 0
%63 = load i64, i64* %62
%64 = call i32 @XdmcpWriteCARD16(%struct.TYPE_7__* %60, i64 %63)
%65 = icmp ne i32 %64, 0
br i1 %65, label %68, label %66
66: ; preds = %59
%67 = load i32, i32* @FALSE
store i32 %67, i32* %3
br label %70
68: ; preds = %59
%69 = load i32, i32* @TRUE
store i32 %69, i32* %3
br label %70
70: ; preds = %68, %66, %57, %48, %25
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.TYPE_7__ = type { i32, i64, i32* }
%struct.TYPE_6__ = type { i64, i64, i64 }
@XDM_MAX_MSGLEN = global i32 0
@FALSE = global i32 0
@TRUE = global i32 0
define i32 @XdmcpWriteHeader(%struct.TYPE_7__* %0, %struct.TYPE_6__* nocapture %1) {
%3 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %0, i64 0, i32 0
%4 = load i32, i32* %3
%5 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %1, i64 0, i32 0
%6 = load i64, i64* %5
%7 = trunc i64 %6 to i32
%8 = add nsw i32 %7, 6
%9 = icmp slt i32 %4, %8
br i1 %9, label %10, label %22
10: ; preds = %2
%11 = load i32, i32* @XDM_MAX_MSGLEN
%12 = shl i32 %11, 2
%13 = tail call i64 @Xalloc(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %38, label %15
15: ; preds = %10
%16 = inttoptr i64 %13 to i32*
%17 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %0, i64 0, i32 2
%18 = bitcast i32** %17 to i64**
%19 = load i64*, i64** %18
%20 = tail call i32 @Xfree(i64* %19)
store i32* %16, i32** %17
%21 = load i32, i32* @XDM_MAX_MSGLEN
store i32 %21, i32* %3
br label %22
22: ; preds = %15, %2
%23 = getelementptr inbounds %struct.TYPE_7__, %struct.TYPE_7__* %0, i64 0, i32 1
store i64 0, i64* %23
%24 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %1, i64 0, i32 2
%25 = load i64, i64* %24
%26 = tail call i32 @XdmcpWriteCARD16(%struct.TYPE_7__* %0, i64 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %38, label %28
28: ; preds = %22
%29 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %1, i64 0, i32 1
%30 = load i64, i64* %29
%31 = tail call i32 @XdmcpWriteCARD16(%struct.TYPE_7__* %0, i64 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %28
%34 = load i64, i64* %5
%35 = tail call i32 @XdmcpWriteCARD16(%struct.TYPE_7__* %0, i64 %34)
%36 = icmp eq i32 %35, 0
%37 = select i1 %36, i32* @FALSE, i32* @TRUE
br label %38
38: ; preds = %33, %28, %22, %10
%39 = phi i32* [ @FALSE, %10 ], [ @FALSE, %22 ], [ @FALSE, %28 ], [ %37, %33 ]
%40 = load i32, i32* %39
ret i32 %40
}