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

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ my_send(int, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main() {
  struct sockaddr_in server;
  int sockfd;
  char *message = "{\"name\": \"a thing\"}";
  char content_length[25];
  sprintf(content_length, "Content-Length: %lu", strlen(message));
  bzero((char *)&server, sizeof(server));
  server.sin_family = AF_INET;
  server.sin_port = htons(8000);
  server.sin_addr.s_addr = inet_addr("127.0.0.1");
  sockfd = socket(PF_INET, SOCK_STREAM, 0);
  if (connect(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) {
    return 1;
  }
  my_send(sockfd, "POST / HTTP/1.1\r\n");
  my_send(sockfd, content_length);
  my_send(sockfd, "\r\n");
  my_send(sockfd, "Content-Type: application/json\r\n");
  my_send(sockfd, "\r\n");
  my_send(sockfd, message);
  my_send(sockfd, "\r\n");
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
lea    0xe4c(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x10(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x1f40,%edi
call   1040 <htons@plt>
mov    %eax,-0xc(%rbp)
lea    0xe2d(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x10(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    1246 <main+0xb6>
movl   $0x1,-0x4(%rbp)
jmp    12b8 <main+0x128>
mov    -0x14(%rbp),%edi
lea    0xde2(%rip),%rsi
call   1050 <my_send@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1050 <my_send@plt>
mov    -0x14(%rbp),%edi
lea    0xdd6(%rip),%rsi
call   1050 <my_send@plt>
mov    -0x14(%rbp),%edi
lea    0xdca(%rip),%rsi
call   1050 <my_send@plt>
mov    -0x14(%rbp),%edi
lea    0xdb8(%rip),%rsi
call   1050 <my_send@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1050 <my_send@plt>
mov    -0x14(%rbp),%edi
lea    0xd9d(%rip),%rsi
call   1050 <my_send@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
lea    0xe61(%rip),%rdi
call   1030 <strlen@plt>
lea    0xe69(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    $0x1f40,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xe3f(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,(%rsp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     1286 <main+0xf6>
lea    0xe09(%rip),%rsi
mov    %ebp,%edi
call   1050 <my_send@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
call   1050 <my_send@plt>
lea    0xdfe(%rip),%r14
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <my_send@plt>
lea    0xdf0(%rip),%rsi
mov    %ebp,%edi
call   1050 <my_send@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <my_send@plt>
lea    0xd94(%rip),%rsi
mov    %ebp,%edi
call   1050 <my_send@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <my_send@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    128b <main+0xfb>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"{\22name\22: \22a thing\22}\00"
@.str.1 = [20 x i8] c"Content-Length: %lu\00"
@AF_INET = global i32 0
@.str.2 = [10 x i8] c"127.0.0.1\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [18 x i8] c"POST / HTTP/1.1\0D\0A\00"
@.str.4 = [3 x i8] c"\0D\0A\00"
@.str.5 = [33 x i8] c"Content-Type: application/json\0D\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [25 x i8]
store i32 0, i32* %1
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8** %4
%6 = getelementptr inbounds [25 x i8], [25 x i8]* %5, i64 0, i64 0
%7 = load i8*, i8** %4
%8 = call i32 @strlen(i8* %7)
%9 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = bitcast %struct.sockaddr_in* %2 to i8*
%11 = call i32 @bzero(i8* %10, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %12, i32* %13
%14 = call i32 @htons(i32 8000)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %14, i32* %15
%16 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
store i32 %16, i32* %18
%19 = load i32, i32* @PF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%24 = call i64 @connect(i32 %22, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %0
store i32 1, i32* %1
br label %46
27: ; preds = %0
%28 = load i32, i32* %3
%29 = call i32 @my_send(i32 %28, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%30 = load i32, i32* %3
%31 = getelementptr inbounds [25 x i8], [25 x i8]* %5, i64 0, i64 0
%32 = call i32 @my_send(i32 %30, i8* %31)
%33 = load i32, i32* %3
%34 = call i32 @my_send(i32 %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%35 = load i32, i32* %3
%36 = call i32 @my_send(i32 %35, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
%37 = load i32, i32* %3
%38 = call i32 @my_send(i32 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%39 = load i32, i32* %3
%40 = load i8*, i8** %4
%41 = call i32 @my_send(i32 %39, i8* %40)
%42 = load i32, i32* %3
%43 = call i32 @my_send(i32 %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%44 = load i32, i32* %3
%45 = call i32 @close(i32 %44)
store i32 0, i32* %1
br label %46
46: ; preds = %27, %26
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"{\22name\22: \22a thing\22}\00"
@.str.1 = [20 x i8] c"Content-Length: %lu\00"
@AF_INET = global i32 0
@.str.2 = [10 x i8] c"127.0.0.1\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [18 x i8] c"POST / HTTP/1.1\0D\0A\00"
@.str.4 = [3 x i8] c"\0D\0A\00"
@.str.5 = [33 x i8] c"Content-Type: application/json\0D\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [25 x i8]
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = call i32 @bzero(i8* %3, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %8, i32* %9
%10 = tail call i32 @htons(i32 8000)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %10, i32* %11
%12 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @PF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%18 = call i64 @connect(i32 %16, %struct.sockaddr* %17, i32 12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %0
%21 = call i32 @my_send(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 @my_send(i32 %16, i8* %4)
%23 = call i32 @my_send(i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%24 = call i32 @my_send(i32 %16, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
%25 = call i32 @my_send(i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32 @my_send(i32 %16, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @my_send(i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 @close(i32 %16)
br label %29
29: ; preds = %0, %20
%30 = phi i32 [ 0, %20 ], [ 1, %0 ]
ret i32 %30
}