#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 */
struct packetheader {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ ILibAddHeaderLine(struct packetheader *, char *, int, char *,
                                     int);
struct packetheader *ILibCreateEmptyPacket();
int /*<<< orphan*/ ILibSetDirective(struct packetheader *, char *, int, char *,
                                    int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int sprintf(char *, char *, char *, int);
scalar_t__ strlen(char *);
struct packetheader *RNDBuildPacket(char *IP, int Port, char *Path, char *cmd) {
  struct packetheader *RetVal = ILibCreateEmptyPacket();
  char *HostLine = (char *)malloc((int)strlen(IP) + 7);
  int HostLineLength = sprintf(HostLine, "%s:%d", IP, Port);
  ILibSetDirective(RetVal, cmd, (int)strlen(cmd), Path, (int)strlen(Path));
  ILibAddHeaderLine(RetVal, "Host", 4, HostLine, HostLineLength);
  ILibAddHeaderLine(RetVal, "User-Agent", 10,
                    "POSIX, UPnP/1.0, Intel MicroStack/1.0.1189", 42);
  free(HostLine);
  return (RetVal);
}


x86 assembly (clang, -O0):
<RNDBuildPacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    $0x0,%al
call   1040 <ILibCreateEmptyPacket@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
add    $0x7,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0xe45(%rip),%rsi
call   1080 <sprintf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x44(%rbp),%edx
mov    -0x40(%rbp),%rcx
mov    %eax,%r8d
call   1090 <ILibSetDirective@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    -0x34(%rbp),%r8d
lea    0xdec(%rip),%rsi
mov    $0x4,%edx
call   1050 <ILibAddHeaderLine@plt>
mov    -0x28(%rbp),%rdi
lea    0xddc(%rip),%rsi
mov    $0xa,%edx
lea    0xddb(%rip),%rcx
mov    $0x2a,%r8d
call   1050 <ILibAddHeaderLine@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RNDBuildPacket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r15
mov    %esi,%r13d
mov    %rdi,%rbp
xor    %eax,%eax
call   1040 <ILibCreateEmptyPacket@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    0x7(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0xe55(%rip),%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
mov    %r13d,%ecx
call   1080 <sprintf@plt>
mov    %eax,%r13d
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
mov    %r15,%rcx
mov    %eax,%r8d
call   1090 <ILibSetDirective@plt>
lea    0xe1d(%rip),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
mov    %r14,%rcx
mov    %r13d,%r8d
call   1050 <ILibAddHeaderLine@plt>
lea    0xe08(%rip),%rsi
lea    0xe0c(%rip),%rcx
mov    %rbx,%rdi
mov    $0xa,%edx
mov    $0x2a,%r8d
call   1050 <ILibAddHeaderLine@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.packetheader = type { i32 }
@.str = [6 x i8] c"%s:%d\00"
@.str.1 = [5 x i8] c"Host\00"
@.str.2 = [11 x i8] c"User-Agent\00"
@.str.3 = [43 x i8] c"POSIX, UPnP/1.0, Intel MicroStack/1.0.1189\00"
define %struct.packetheader* @RNDBuildPacket(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.packetheader*
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%12 = call %struct.packetheader* (...) @ILibCreateEmptyPacket()
store %struct.packetheader* %12, %struct.packetheader** %9
%13 = load i8*, i8** %5
%14 = call i64 @strlen(i8* %13)
%15 = trunc i64 %14 to i32
%16 = add nsw i32 %15, 7
%17 = call i64 @malloc(i32 %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %10
%19 = load i8*, i8** %10
%20 = load i8*, i8** %5
%21 = load i32, i32* %6
%22 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %21)
store i32 %22, i32* %11
%23 = load %struct.packetheader*, %struct.packetheader** %9
%24 = load i8*, i8** %8
%25 = load i8*, i8** %8
%26 = call i64 @strlen(i8* %25)
%27 = trunc i64 %26 to i32
%28 = load i8*, i8** %7
%29 = load i8*, i8** %7
%30 = call i64 @strlen(i8* %29)
%31 = trunc i64 %30 to i32
%32 = call i32 @ILibSetDirective(%struct.packetheader* %23, i8* %24, i32 %27, i8* %28, i32 %31)
%33 = load %struct.packetheader*, %struct.packetheader** %9
%34 = load i8*, i8** %10
%35 = load i32, i32* %11
%36 = call i32 @ILibAddHeaderLine(%struct.packetheader* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4, i8* %34, i32 %35)
%37 = load %struct.packetheader*, %struct.packetheader** %9
%38 = call i32 @ILibAddHeaderLine(%struct.packetheader* %37, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 42)
%39 = load i8*, i8** %10
%40 = call i32 @free(i8* %39)
%41 = load %struct.packetheader*, %struct.packetheader** %9
ret %struct.packetheader* %41
}

LLVM IR (-O2):
%struct.packetheader = type { i32 }
@.str = [6 x i8] c"%s:%d\00"
@.str.1 = [5 x i8] c"Host\00"
@.str.2 = [11 x i8] c"User-Agent\00"
@.str.3 = [43 x i8] c"POSIX, UPnP/1.0, Intel MicroStack/1.0.1189\00"
define %struct.packetheader* @RNDBuildPacket(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = tail call %struct.packetheader* (...) @ILibCreateEmptyPacket()
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = trunc i64 %6 to i32
%8 = add nsw i32 %7, 7
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @sprintf(i8* dereferenceable(1) %10, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%12 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%13 = trunc i64 %12 to i32
%14 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%15 = trunc i64 %14 to i32
%16 = tail call i32 @ILibSetDirective(%struct.packetheader* %5, i8* %3, i32 %13, i8* %2, i32 %15)
%17 = tail call i32 @ILibAddHeaderLine(%struct.packetheader* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4, i8* %10, i32 %11)
%18 = tail call i32 @ILibAddHeaderLine(%struct.packetheader* %5, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 42)
%19 = tail call i32 @free(i8* %10)
ret %struct.packetheader* %5
}