#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 send(int, char *, int, int /*<<< orphan*/);
scalar_t__ sprintf(char *, char *, ...);
int sendRequest(int fd, const char *jsonBuf, const char *cmd, int jLen) {
  int offset = 0;
  int iret;
  char header_str[2048];
  offset += sprintf(header_str + offset, "POST /merlin/%s HTTP/1.1\r\n", cmd);
  offset += sprintf(header_str + offset, "Content-Type: application/json\r\n");
  offset += sprintf(header_str + offset, "Content-Length: %d\r\n", jLen);
  offset += sprintf(header_str + offset, "Agent: merlin HTTP agent\r\n");
  offset += sprintf(header_str + offset, "Authorization: Basic %s\r\n",
                    "KkZvY3VzVmlzaW9uOm5vaXNpVnN1Y29GKg==");
  offset += sprintf(header_str + offset,
                    "Data_Param: version=1.0.0.0&operation="
                    "%s&session=0&sequence=1&unit=DEVICE&id=0\r\n\r\n",
                    cmd);
  if (jLen + offset >= 2048)
    return -1;
  if (jsonBuf)
    offset += sprintf(header_str + offset, "%s", jsonBuf);
  iret = send(fd, header_str, offset, 0);
  return iret;
}


x86 assembly (clang, -O0):
<sendRequest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x830(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
lea    0xea7(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
lea    -0x830(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
lea    0xe99(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
lea    -0x830(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x1c(%rbp),%edx
lea    0xe8e(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
lea    -0x830(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
lea    0xe7a(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
lea    -0x830(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
lea    0xe6c(%rip),%rsi
lea    0xe7f(%rip),%rdx
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
lea    -0x830(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
lea    0xe77(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
cmp    $0x800,%eax
jl     1265 <sendRequest+0x145>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ba <sendRequest+0x19a>
cmpq   $0x0,-0x10(%rbp)
je     129d <sendRequest+0x17d>
lea    -0x830(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
lea    0xe75(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    -0x20(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendRequest>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %ecx,%r13d
mov    %rdx,%r12
mov    %rsi,%r15
mov    %edi,0xc(%rsp)
lea    0xebb(%rip),%rsi
lea    0x10(%rsp),%rdi
xor    %eax,%eax
call   1040 <sprintf@plt>
mov    %rax,%rbx
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
lea    0xeb5(%rip),%rsi
xor    %eax,%eax
call   1040 <sprintf@plt>
mov    %rax,%rbp
add    %rbx,%rbp
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
lea    0xeb7(%rip),%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1040 <sprintf@plt>
mov    %rax,%rbx
add    %rbp,%rbx
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
lea    0xeaa(%rip),%rsi
xor    %eax,%eax
call   1040 <sprintf@plt>
mov    %rax,%rbp
add    %rbx,%rbp
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
lea    0xea6(%rip),%rsi
lea    0xeb9(%rip),%rdx
xor    %eax,%eax
call   1040 <sprintf@plt>
mov    %rax,%r14
add    %rbp,%r14
movslq %r14d,%rax
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
lea    0xebf(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <sprintf@plt>
mov    %rax,%rbx
add    %r14,%rbx
add    %ebx,%r13d
mov    $0xffffffff,%eax
cmp    $0x7ff,%r13d
jg     1244 <sendRequest+0x124>
mov    0xc(%rsp),%ebp
test   %r15,%r15
je     1234 <sendRequest+0x114>
movslq %ebx,%rbx
lea    (%rsp,%rbx,1),%rdi
add    $0x10,%rdi
lea    0xed6(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <sprintf@plt>
add    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"POST /merlin/%s HTTP/1.1\0D\0A\00"
@.str.1 = [33 x i8] c"Content-Type: application/json\0D\0A\00"
@.str.2 = [21 x i8] c"Content-Length: %d\0D\0A\00"
@.str.3 = [27 x i8] c"Agent: merlin HTTP agent\0D\0A\00"
@.str.4 = [26 x i8] c"Authorization: Basic %s\0D\0A\00"
@.str.5 = [37 x i8] c"KkZvY3VzVmlzaW9uOm5vaXNpVnN1Y29GKg==\00"
@.str.6 = [83 x i8] c"Data_Param: version=1.0.0.0&operation=%s&session=0&sequence=1&unit=DEVICE&id=0\0D\0A\0D\0A\00"
@.str.7 = [3 x i8] c"%s\00"
define i32 @sendRequest(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [2048 x i8]
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %10
%13 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%14 = load i32, i32* %10
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i8*, i8** %8
%18 = call i64 (i8*, i8*, ...) @sprintf(i8* %16, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* %10
%20 = sext i32 %19 to i64
%21 = add nsw i64 %20, %18
%22 = trunc i64 %21 to i32
store i32 %22, i32* %10
%23 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%24 = load i32, i32* %10
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = call i64 (i8*, i8*, ...) @sprintf(i8* %26, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* %10
%29 = sext i32 %28 to i64
%30 = add nsw i64 %29, %27
%31 = trunc i64 %30 to i32
store i32 %31, i32* %10
%32 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%33 = load i32, i32* %10
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i32, i32* %9
%37 = call i64 (i8*, i8*, ...) @sprintf(i8* %35, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = add nsw i64 %39, %37
%41 = trunc i64 %40 to i32
store i32 %41, i32* %10
%42 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = call i64 (i8*, i8*, ...) @sprintf(i8* %45, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = add nsw i64 %48, %46
%50 = trunc i64 %49 to i32
store i32 %50, i32* %10
%51 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%52 = load i32, i32* %10
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
%55 = call i64 (i8*, i8*, ...) @sprintf(i8* %54, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0))
%56 = load i32, i32* %10
%57 = sext i32 %56 to i64
%58 = add nsw i64 %57, %55
%59 = trunc i64 %58 to i32
store i32 %59, i32* %10
%60 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%61 = load i32, i32* %10
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
%64 = load i8*, i8** %8
%65 = call i64 (i8*, i8*, ...) @sprintf(i8* %63, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.6, i64 0, i64 0), i8* %64)
%66 = load i32, i32* %10
%67 = sext i32 %66 to i64
%68 = add nsw i64 %67, %65
%69 = trunc i64 %68 to i32
store i32 %69, i32* %10
%70 = load i32, i32* %9
%71 = load i32, i32* %10
%72 = add nsw i32 %70, %71
%73 = icmp sge i32 %72, 2048
br i1 %73, label %74, label %75
74: ; preds = %4
store i32 -1, i32* %5
br label %95
75: ; preds = %4
%76 = load i8*, i8** %7
%77 = icmp ne i8* %76, null
br i1 %77, label %78, label %89
78: ; preds = %75
%79 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%80 = load i32, i32* %10
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8, i8* %79, i64 %81
%83 = load i8*, i8** %7
%84 = call i64 (i8*, i8*, ...) @sprintf(i8* %82, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %83)
%85 = load i32, i32* %10
%86 = sext i32 %85 to i64
%87 = add nsw i64 %86, %84
%88 = trunc i64 %87 to i32
store i32 %88, i32* %10
br label %89
89: ; preds = %78, %75
%90 = load i32, i32* %6
%91 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%92 = load i32, i32* %10
%93 = call i32 @send(i32 %90, i8* %91, i32 %92, i32 0)
store i32 %93, i32* %11
%94 = load i32, i32* %11
store i32 %94, i32* %5
br label %95
95: ; preds = %89, %74
%96 = load i32, i32* %5
ret i32 %96
}

LLVM IR (-O2):
@.str = [27 x i8] c"POST /merlin/%s HTTP/1.1\0D\0A\00"
@.str.1 = [33 x i8] c"Content-Type: application/json\0D\0A\00"
@.str.2 = [21 x i8] c"Content-Length: %d\0D\0A\00"
@.str.3 = [27 x i8] c"Agent: merlin HTTP agent\0D\0A\00"
@.str.4 = [26 x i8] c"Authorization: Basic %s\0D\0A\00"
@.str.5 = [37 x i8] c"KkZvY3VzVmlzaW9uOm5vaXNpVnN1Y29GKg==\00"
@.str.6 = [83 x i8] c"Data_Param: version=1.0.0.0&operation=%s&session=0&sequence=1&unit=DEVICE&id=0\0D\0A\0D\0A\00"
@.str.7 = [3 x i8] c"%s\00"
define i32 @sendRequest(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca [2048 x i8]
%6 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%7 = call i64 (i8*, i8*, ...) @sprintf(i8* %6, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %2)
%8 = shl i64 %7, 32
%9 = ashr exact i64 %8, 32
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 %9
%11 = call i64 (i8*, i8*, ...) @sprintf(i8* %10, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%12 = add i64 %11, %7
%13 = shl i64 %12, 32
%14 = ashr exact i64 %13, 32
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 %14
%16 = call i64 (i8*, i8*, ...) @sprintf(i8* %15, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %3)
%17 = add i64 %12, %16
%18 = shl i64 %17, 32
%19 = ashr exact i64 %18, 32
%20 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 %19
%21 = call i64 (i8*, i8*, ...) @sprintf(i8* %20, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%22 = add i64 %17, %21
%23 = shl i64 %22, 32
%24 = ashr exact i64 %23, 32
%25 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 %24
%26 = call i64 (i8*, i8*, ...) @sprintf(i8* %25, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0))
%27 = add i64 %22, %26
%28 = shl i64 %27, 32
%29 = ashr exact i64 %28, 32
%30 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 %29
%31 = call i64 (i8*, i8*, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.6, i64 0, i64 0), i8* %2)
%32 = add i64 %27, %31
%33 = trunc i64 %32 to i32
%34 = add nsw i32 %33, %3
%35 = icmp sgt i32 %34, 2047
br i1 %35, label %48, label %36
36: ; preds = %4
%37 = icmp eq i8* %1, null
br i1 %37, label %45, label %38
38: ; preds = %36
%39 = shl i64 %32, 32
%40 = ashr exact i64 %39, 32
%41 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 %40
%42 = call i64 (i8*, i8*, ...) @sprintf(i8* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %1)
%43 = add i64 %42, %32
%44 = trunc i64 %43 to i32
br label %45
45: ; preds = %38, %36
%46 = phi i32 [ %44, %38 ], [ %33, %36 ]
%47 = call i32 @send(i32 %0, i8* %6, i32 %46, i32 0)
br label %48
48: ; preds = %4, %45
%49 = phi i32 [ %47, %45 ], [ -1, %4 ]
ret i32 %49
}