#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;

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
void build_header(uint8_t method, char *host, char *path, char *dat,
                  char *buff) {
  char len_buff[20];
  switch (method) {
  case 0:
    memcpy(buff, "HEAD ", sizeof("HEAD "));
    break;
  case 2:
    memcpy(buff, "POST ", sizeof("POST "));
    break;
  case 1:
    memcpy(buff, "GET ", sizeof("GET "));
    break;
  case 3:
    memcpy(buff, "PUT ", sizeof("PUT "));
    break;
  case 4:
    memcpy(buff, "DELETE ", sizeof("DELETE "));
    break;
  default:
    memcpy(buff, "HEAD ", sizeof("HEAD "));
    break;
  }
  strcat(buff, "/");
  strcat(buff, path);
  strcat(buff, " HTTP/1.1\r\n");
  strcat(buff, "Host: ");
  strcat(buff, host);
  strcat(buff, "\r\n");
  strcat(buff, "Content-Type: application/json\r\n");
  strcat(buff, "Accept: application/json\r\n");
  if (method == 0 || method == 4 || method == 1) {
    strcat(buff, "\r\n");
    return;
  } else if (method == 3 || method == 2) {
    sprintf(len_buff, "%d", strlen(dat));
    strcat(buff, "Content-Length: ");
    strcat(buff, len_buff);
    strcat(buff, "\r\n\r\n");
    strcat(buff, dat);
    strcat(buff, "\r\n\r\n");
  }
}


x86 assembly (clang, -O0):
<build_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x4(%rbp),%eax
mov    %rax,-0x48(%rbp)
sub    $0x4,%rax
ja     1202 <build_header+0xc2>
mov    -0x48(%rbp),%rax
lea    0xe89(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x28(%rbp),%rdi
lea    0xe89(%rip),%rsi
mov    $0x6,%edx
call   1040 <memcpy@plt>
jmp    1217 <build_header+0xd7>
mov    -0x28(%rbp),%rdi
lea    0xe75(%rip),%rsi
mov    $0x6,%edx
call   1040 <memcpy@plt>
jmp    1217 <build_header+0xd7>
mov    -0x28(%rbp),%rdi
lea    0xe61(%rip),%rsi
mov    $0x5,%edx
call   1040 <memcpy@plt>
jmp    1217 <build_header+0xd7>
mov    -0x28(%rbp),%rdi
lea    0xe4c(%rip),%rsi
mov    $0x5,%edx
call   1040 <memcpy@plt>
jmp    1217 <build_header+0xd7>
mov    -0x28(%rbp),%rdi
lea    0xe37(%rip),%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
jmp    1217 <build_header+0xd7>
mov    -0x28(%rbp),%rdi
lea    0xe07(%rip),%rsi
mov    $0x6,%edx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1050 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x28(%rbp),%rdi
lea    0xdf5(%rip),%rsi
call   1050 <strcat@plt>
mov    -0x28(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   1050 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x28(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   1050 <strcat@plt>
mov    -0x28(%rbp),%rdi
lea    0xdcb(%rip),%rsi
call   1050 <strcat@plt>
mov    -0x28(%rbp),%rdi
lea    0xddc(%rip),%rsi
call   1050 <strcat@plt>
cmpl   $0x0,-0x4(%rbp)
je     12af <build_header+0x16f>
cmpl   $0x4,-0x4(%rbp)
je     12af <build_header+0x16f>
cmpl   $0x1,-0x4(%rbp)
jne    12c4 <build_header+0x184>
mov    -0x28(%rbp),%rdi
lea    0xddf(%rip),%rsi
call   1050 <strcat@plt>
jmp    134a <build_header+0x20a>
cmpl   $0x3,-0x4(%rbp)
je     12d8 <build_header+0x198>
cmpl   $0x2,-0x4(%rbp)
jne    1345 <build_header+0x205>
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
lea    0xd8d(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xd80(%rip),%rsi
call   1050 <strcat@plt>
mov    -0x28(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x28(%rbp),%rdi
lea    0xd74(%rip),%rsi
call   1050 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x28(%rbp),%rdi
lea    0xd57(%rip),%rsi
call   1050 <strcat@plt>
jmp    134a <build_header+0x20a>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_header>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %r8,%rbx
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %edi,%ebp
cmp    $0x4,%edi
ja     1171 <build_header+0x31>
mov    %ebp,%eax
lea    0xe98(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe9c(%rip),%rsi
jmp    118a <build_header+0x4a>
lea    0xe9f(%rip),%rsi
jmp    119b <build_header+0x5b>
lea    0xe90(%rip),%rsi
mov    %rbx,%rdi
mov    $0x6,%edx
jmp    11b4 <build_header+0x74>
lea    0xe8a(%rip),%rsi
mov    %rbx,%rdi
mov    $0x5,%edx
jmp    11b4 <build_header+0x74>
lea    0xe7e(%rip),%rsi
mov    %rbx,%rdi
mov    $0x8,%edx
call   1040 <memcpy@plt>
lea    0xe72(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcat@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <strcat@plt>
lea    0xe5a(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcat@plt>
lea    0xe57(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strcat@plt>
lea    0xe96(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcat@plt>
lea    0xe35(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcat@plt>
lea    0xe47(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcat@plt>
cmp    $0x4,%ebp
ja     1253 <build_header+0x113>
mov    $0x13,%eax
bt     %ebp,%eax
jae    1253 <build_header+0x113>
lea    0xe5a(%rip),%rsi
mov    %rbx,%rdi
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <strcat@plt>
and    $0xfffffffe,%ebp
cmp    $0x2,%ebp
jne    12b9 <build_header+0x179>
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0xe19(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %eax,%edx
call   1060 <sprintf@plt>
lea    0xe08(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcat@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <strcat@plt>
lea    0xdff(%rip),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strcat@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <strcat@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"HEAD \00"
@.str.1 = [6 x i8] c"POST \00"
@.str.2 = [5 x i8] c"GET \00"
@.str.3 = [5 x i8] c"PUT \00"
@.str.4 = [8 x i8] c"DELETE \00"
@.str.5 = [2 x i8] c"/\00"
@.str.6 = [12 x i8] c" HTTP/1.1\0D\0A\00"
@.str.7 = [7 x i8] c"Host: \00"
@.str.8 = [3 x i8] c"\0D\0A\00"
@.str.9 = [33 x i8] c"Content-Type: application/json\0D\0A\00"
@.str.10 = [27 x i8] c"Accept: application/json\0D\0A\00"
@.str.11 = [3 x i8] c"%d\00"
@.str.12 = [17 x i8] c"Content-Length: \00"
@.str.13 = [5 x i8] c"\0D\0A\0D\0A\00"
define void @build_header(i32 %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca [20 x i8]
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* %4, i8** %10
%12 = load i32, i32* %6
switch i32 %12, label %28 [
i32 0, label %13
i32 2, label %16
i32 1, label %19
i32 3, label %22
i32 4, label %25
]
13: ; preds = %5
%14 = load i8*, i8** %10
%15 = call i32 @memcpy(i8* %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 6)
br label %31
16: ; preds = %5
%17 = load i8*, i8** %10
%18 = call i32 @memcpy(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 6)
br label %31
19: ; preds = %5
%20 = load i8*, i8** %10
%21 = call i32 @memcpy(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 5)
br label %31
22: ; preds = %5
%23 = load i8*, i8** %10
%24 = call i32 @memcpy(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 5)
br label %31
25: ; preds = %5
%26 = load i8*, i8** %10
%27 = call i32 @memcpy(i8* %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 8)
br label %31
28: ; preds = %5
%29 = load i8*, i8** %10
%30 = call i32 @memcpy(i8* %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 6)
br label %31
31: ; preds = %28, %25, %22, %19, %16, %13
%32 = load i8*, i8** %10
%33 = call i32 @strcat(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%34 = load i8*, i8** %10
%35 = load i8*, i8** %8
%36 = call i32 @strcat(i8* %34, i8* %35)
%37 = load i8*, i8** %10
%38 = call i32 @strcat(i8* %37, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%39 = load i8*, i8** %10
%40 = call i32 @strcat(i8* %39, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%41 = load i8*, i8** %10
%42 = load i8*, i8** %7
%43 = call i32 @strcat(i8* %41, i8* %42)
%44 = load i8*, i8** %10
%45 = call i32 @strcat(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
%46 = load i8*, i8** %10
%47 = call i32 @strcat(i8* %46, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0))
%48 = load i8*, i8** %10
%49 = call i32 @strcat(i8* %48, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0))
%50 = load i32, i32* %6
%51 = icmp eq i32 %50, 0
br i1 %51, label %58, label %52
52: ; preds = %31
%53 = load i32, i32* %6
%54 = icmp eq i32 %53, 4
br i1 %54, label %58, label %55
55: ; preds = %52
%56 = load i32, i32* %6
%57 = icmp eq i32 %56, 1
br i1 %57, label %58, label %61
58: ; preds = %55, %52, %31
%59 = load i8*, i8** %10
%60 = call i32 @strcat(i8* %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
br label %85
61: ; preds = %55
%62 = load i32, i32* %6
%63 = icmp eq i32 %62, 3
br i1 %63, label %67, label %64
64: ; preds = %61
%65 = load i32, i32* %6
%66 = icmp eq i32 %65, 2
br i1 %66, label %67, label %84
67: ; preds = %64, %61
%68 = getelementptr inbounds [20 x i8], [20 x i8]* %11, i64 0, i64 0
%69 = load i8*, i8** %9
%70 = call i32 @strlen(i8* %69)
%71 = call i32 @sprintf(i8* %68, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0), i32 %70)
%72 = load i8*, i8** %10
%73 = call i32 @strcat(i8* %72, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0))
%74 = load i8*, i8** %10
%75 = getelementptr inbounds [20 x i8], [20 x i8]* %11, i64 0, i64 0
%76 = call i32 @strcat(i8* %74, i8* %75)
%77 = load i8*, i8** %10
%78 = call i32 @strcat(i8* %77, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.13, i64 0, i64 0))
%79 = load i8*, i8** %10
%80 = load i8*, i8** %9
%81 = call i32 @strcat(i8* %79, i8* %80)
%82 = load i8*, i8** %10
%83 = call i32 @strcat(i8* %82, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.13, i64 0, i64 0))
br label %84
84: ; preds = %67, %64
br label %85
85: ; preds = %58, %84
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"HEAD \00"
@.str.1 = [6 x i8] c"POST \00"
@.str.2 = [5 x i8] c"GET \00"
@.str.3 = [5 x i8] c"PUT \00"
@.str.4 = [8 x i8] c"DELETE \00"
@.str.5 = [2 x i8] c"/\00"
@.str.6 = [12 x i8] c" HTTP/1.1\0D\0A\00"
@.str.7 = [7 x i8] c"Host: \00"
@.str.8 = [3 x i8] c"\0D\0A\00"
@.str.9 = [33 x i8] c"Content-Type: application/json\0D\0A\00"
@.str.10 = [27 x i8] c"Accept: application/json\0D\0A\00"
@.str.11 = [3 x i8] c"%d\00"
@.str.12 = [17 x i8] c"Content-Length: \00"
@.str.13 = [5 x i8] c"\0D\0A\0D\0A\00"
define void @build_header(i32 %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca [20 x i8]
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
switch i32 %0, label %18 [
i32 0, label %8
i32 2, label %10
i32 1, label %12
i32 3, label %14
i32 4, label %16
]
8: ; preds = %5
%9 = tail call i32 @memcpy(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 6)
br label %20
10: ; preds = %5
%11 = tail call i32 @memcpy(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 6)
br label %20
12: ; preds = %5
%13 = tail call i32 @memcpy(i8* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 5)
br label %20
14: ; preds = %5
%15 = tail call i32 @memcpy(i8* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 5)
br label %20
16: ; preds = %5
%17 = tail call i32 @memcpy(i8* %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 8)
br label %20
18: ; preds = %5
%19 = tail call i32 @memcpy(i8* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 6)
br label %20
20: ; preds = %18, %16, %14, %12, %10, %8
%21 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%22 = tail call i32 @strcat(i8* %4, i8* %2)
%23 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%24 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%25 = tail call i32 @strcat(i8* %4, i8* %1)
%26 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
%27 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0))
%28 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0))
switch i32 %0, label %31 [
i32 4, label %29
i32 1, label %29
i32 0, label %29
]
29: ; preds = %20, %20, %20
%30 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
br label %42
31: ; preds = %20
%32 = and i32 %0, -2
%33 = icmp eq i32 %32, 2
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = tail call i32 @strlen(i8* %3)
%36 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0), i32 %35)
%37 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.12, i64 0, i64 0))
%38 = call i32 @strcat(i8* %4, i8* %7)
%39 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.13, i64 0, i64 0))
%40 = call i32 @strcat(i8* %4, i8* %3)
%41 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.13, i64 0, i64 0))
br label %42
42: ; preds = %31, %34, %29
ret void
}