#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__ {
  long s_addr;
};
struct sockaddr_in {
  int sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct hostent {
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ in_addr_t;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EINVAL;
int ENOENT;
int ENOMEM;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, void *, int);
struct hostent *gethostbyname(char *);
int htons(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strchr(char const *, char);
int /*<<< orphan*/ strlcat(char *, char const *, size_t);
int /*<<< orphan*/ strlcpy(char *, char const *, int);
int strlen(char const *);
scalar_t__ strncmp(char const *, char *, int);
int write(int, char *, int);
int net_curl(const char *url, char *obuf, size_t obufsize,
             const char *headers) {
  int nread = 0;
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(80);
  int rc;
  char host[512 + 1];
  char path[512 + 1];
  int sock;
  printf("\n\ncurl '%s'\n", url);
  if (strlen(url) > 512)
    return EINVAL;
  if (strncmp(url, "http://", 7))
    return EINVAL;
  url += 7;
  char *pos = strchr(url, '/');
  if (pos == 0)
    return EINVAL;
  strlcpy(host, url, pos - url + 1);
  strlcpy(path, pos + 1, 512);
  pos = strchr(host, ':');
  if (pos != 0) {
    *pos = '\0';
    int port = atoi(pos + 1);
    if (port)
      addr.sin_port = port;
  }
  printf("curl host '%s' path '%s'\n", host, path);
  in_addr_t out;
  struct hostent *he = gethostbyname(host);
  if (he == 0) {
    rc == ENOENT;
    printf("can't resolve '%s', %d\n", host, rc);
    return rc;
  }
  addr.sin_addr.s_addr = *(long *)(he->h_addr_list[0]);
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    printf("can't open TCP socket\n");
    return ENOMEM;
  }
  printf("TCP - will connect\n");
  if (connect(sock, (void *)&addr, sizeof(addr))) {
    printf("can't connect to %s\n", host);
    goto err;
  }
  printf("TCP - connected\n");
  char buf[1024 * 10];
  memset(buf, 0, sizeof(buf));
  strlcpy(buf, "GET /", sizeof(buf));
  strlcat(buf, path, sizeof(buf));
  strlcat(buf, " HTTP/1.1\r\nHost: ", sizeof(buf));
  strlcat(buf, host, sizeof(buf));
  strlcat(buf,
          "\r\nUser-Agent: PhantomOSNetTest/0.1 (PhantomOS i686; "
          "ru)\r\nAccept: text/html,text/plain\r\nConnection: close\r\n",
          sizeof(buf));
  if (headers)
    strlcat(buf, headers, sizeof(buf));
  strlcat(buf, "\r\n", sizeof(buf));
  int len = strlen(buf);
  int nwrite = write(sock, buf, len);
  if (nwrite != len)
    goto err;
  memset(obuf, 0, obufsize);
  while (1) {
    nread = read(sock, buf, sizeof(buf) - 1);
    buf[sizeof(buf) - 1] = 0;
    if (nread == 0)
      break;
    if (nread < 0) {
      close(sock);
      return nread;
    }
    buf[nread] = 0;
    strlcat(obuf, buf, obufsize);
  }
err:
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<net_curl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2c90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    $0x50,%edi
call   1070 <htons@plt>
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x200,%eax
jle    1262 <net_curl+0x72>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    162b <net_curl+0x43b>
mov    -0x10(%rbp),%rdi
lea    0xda0(%rip),%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1292 <net_curl+0xa2>
mov    0x2d40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    162b <net_curl+0x43b>
mov    -0x10(%rbp),%rax
add    $0x7,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1080 <strchr@plt>
mov    %rax,-0x470(%rbp)
cmpq   $0x0,-0x470(%rbp)
jne    12d2 <net_curl+0xe2>
mov    0x2d00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    162b <net_curl+0x43b>
lea    -0x250(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x470(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edx
call   10e0 <strlcpy@plt>
lea    -0x460(%rbp),%rdi
mov    -0x470(%rbp),%rsi
add    $0x1,%rsi
mov    $0x200,%edx
call   10e0 <strlcpy@plt>
lea    -0x250(%rbp),%rdi
mov    $0x3a,%esi
call   1080 <strchr@plt>
mov    %rax,-0x470(%rbp)
cmpq   $0x0,-0x470(%rbp)
je     1373 <net_curl+0x183>
mov    -0x470(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x470(%rbp),%rdi
add    $0x1,%rdi
call   10f0 <atoi@plt>
mov    %eax,-0x474(%rbp)
cmpl   $0x0,-0x474(%rbp)
je     136e <net_curl+0x17e>
mov    -0x474(%rbp),%eax
mov    %eax,-0x48(%rbp)
jmp    1373 <net_curl+0x183>
lea    -0x250(%rbp),%rsi
lea    -0x460(%rbp),%rdx
lea    0xc8d(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
lea    -0x250(%rbp),%rdi
call   10d0 <gethostbyname@plt>
mov    %rax,-0x480(%rbp)
cmpq   $0x0,-0x480(%rbp)
jne    13d3 <net_curl+0x1e3>
lea    -0x250(%rbp),%rsi
mov    -0x4c(%rbp),%edx
lea    0xc6e(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    162b <net_curl+0x43b>
mov    -0x480(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2bfa(%rip),%rax
mov    (%rax),%edi
mov    0x2bd9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x464(%rbp)
cmpl   $0x0,-0x464(%rbp)
jge    1432 <net_curl+0x242>
lea    0xc2d(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    0x2bb0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    162b <net_curl+0x43b>
lea    0xc25(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    -0x464(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0x18,%edx
call   1100 <connect@plt>
cmp    $0x0,%rax
je     1478 <net_curl+0x288>
lea    -0x250(%rbp),%rsi
lea    0xc06(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    1619 <net_curl+0x429>
lea    0xc08(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
lea    -0x2c80(%rbp),%rdi
xor    %esi,%esi
mov    $0x2800,%edx
call   10a0 <memset@plt>
lea    -0x2c80(%rbp),%rdi
lea    0xbf1(%rip),%rsi
mov    $0x2800,%edx
call   10e0 <strlcpy@plt>
lea    -0x2c80(%rbp),%rdi
lea    -0x460(%rbp),%rsi
mov    $0x2800,%edx
call   1050 <strlcat@plt>
lea    -0x2c80(%rbp),%rdi
lea    0xbc7(%rip),%rsi
mov    $0x2800,%edx
call   1050 <strlcat@plt>
lea    -0x2c80(%rbp),%rdi
lea    -0x250(%rbp),%rsi
mov    $0x2800,%edx
call   1050 <strlcat@plt>
lea    -0x2c80(%rbp),%rdi
lea    0xba9(%rip),%rsi
mov    $0x2800,%edx
call   1050 <strlcat@plt>
cmpq   $0x0,-0x28(%rbp)
je     1531 <net_curl+0x341>
lea    -0x2c80(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x2800,%edx
call   1050 <strlcat@plt>
lea    -0x2c80(%rbp),%rdi
lea    0xbd9(%rip),%rsi
mov    $0x2800,%edx
call   1050 <strlcat@plt>
lea    -0x2c80(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x2c84(%rbp)
mov    -0x464(%rbp),%edi
lea    -0x2c80(%rbp),%rsi
mov    -0x2c84(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x2c88(%rbp)
mov    -0x2c88(%rbp),%eax
cmp    -0x2c84(%rbp),%eax
je     1590 <net_curl+0x3a0>
jmp    1619 <net_curl+0x429>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   10a0 <memset@plt>
mov    -0x464(%rbp),%edi
lea    -0x2c80(%rbp),%rsi
mov    $0x27ff,%edx
call   10c0 <read@plt>
mov    %eax,-0x2c(%rbp)
movb   $0x0,-0x481(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    15cf <net_curl+0x3df>
jmp    1614 <net_curl+0x424>
cmpl   $0x0,-0x2c(%rbp)
jge    15ef <net_curl+0x3ff>
mov    -0x464(%rbp),%edi
call   10b0 <close@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    162b <net_curl+0x43b>
movslq -0x2c(%rbp),%rax
movb   $0x0,-0x2c80(%rbp,%rax,1)
mov    -0x18(%rbp),%rdi
lea    -0x2c80(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <strlcat@plt>
jmp    159f <net_curl+0x3af>
jmp    1619 <net_curl+0x429>
mov    -0x464(%rbp),%edi
call   10b0 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2c90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_curl>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2c48,%rsp
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbp
mov    0x2dc4(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    $0x50,%edi
call   1080 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0xdc0(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %rbp,%rdi
call   1070 <strlen@plt>
cmp    $0x201,%eax
jge    1272 <net_curl+0x72>
lea    0xdad(%rip),%rsi
mov    %rbp,%rdi
mov    $0x7,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     128f <net_curl+0x8f>
mov    0x2d4f(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
add    $0x2c48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
add    $0x7,%rbp
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1090 <strchr@plt>
test   %rax,%rax
je     1272 <net_curl+0x72>
mov    %rax,%rbx
mov    %r12,(%rsp)
mov    %ebx,%edx
sub    %ebp,%edx
add    $0x1,%edx
lea    0x20(%rsp),%r12
mov    %r12,%rdi
mov    %rbp,%rsi
call   10f0 <strlcpy@plt>
add    $0x1,%rbx
lea    0x230(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x200,%edx
call   10f0 <strlcpy@plt>
mov    %r12,%rdi
mov    $0x3a,%esi
call   1090 <strchr@plt>
test   %rax,%rax
je     1305 <net_curl+0x105>
movb   $0x0,(%rax)
add    $0x1,%rax
mov    %rax,%rdi
call   1100 <atoi@plt>
test   %eax,%eax
je     1305 <net_curl+0x105>
mov    %eax,0x8(%rsp)
lea    0xd09(%rip),%rdi
lea    0x20(%rsp),%rbx
lea    0x230(%rsp),%rdx
mov    %rbx,%rsi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %rbx,%rdi
call   10e0 <gethostbyname@plt>
test   %rax,%rax
je     1391 <net_curl+0x191>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    (%r14),%edi
mov    0x2c88(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
test   %eax,%eax
js     13a9 <net_curl+0x1a9>
mov    %eax,%ebp
lea    0xd81(%rip),%rdi
call   1040 <puts@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x18,%edx
call   1110 <connect@plt>
test   %rax,%rax
je     13c1 <net_curl+0x1c1>
lea    0xcc7(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   10a0 <printf@plt>
jmp    1509 <net_curl+0x309>
lea    0xc97(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   10a0 <printf@plt>
jmp    127b <net_curl+0x7b>
lea    0xd52(%rip),%rdi
call   1040 <puts@plt>
mov    0x2c1c(%rip),%rax
jmp    1279 <net_curl+0x79>
lea    0xd2a(%rip),%rdi
call   1040 <puts@plt>
lea    0x440(%rsp),%r12
mov    $0x2800,%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10b0 <memset@plt>
lea    0xc71(%rip),%rsi
mov    %r12,%rdi
mov    $0x2800,%edx
call   10f0 <strlcpy@plt>
lea    0x230(%rsp),%rsi
mov    $0x2800,%edx
mov    %r12,%rdi
call   1060 <strlcat@plt>
lea    0xc4e(%rip),%rsi
mov    $0x2800,%edx
mov    %r12,%rdi
call   1060 <strlcat@plt>
lea    0x20(%rsp),%rsi
mov    $0x2800,%edx
mov    %r12,%rdi
call   1060 <strlcat@plt>
lea    0xc3a(%rip),%rsi
mov    $0x2800,%edx
mov    %r12,%rdi
call   1060 <strlcat@plt>
test   %r13,%r13
je     1461 <net_curl+0x261>
lea    0x440(%rsp),%rdi
mov    $0x2800,%edx
mov    %r13,%rsi
call   1060 <strlcat@plt>
lea    0xc74(%rip),%rsi
lea    0x440(%rsp),%r12
mov    $0x2800,%edx
mov    %r12,%rdi
call   1060 <strlcat@plt>
mov    %r12,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1050 <write@plt>
cmp    %ebx,%eax
jne    1509 <net_curl+0x309>
mov    %r15,%rdi
xor    %esi,%esi
mov    (%rsp),%rdx
call   10b0 <memset@plt>
lea    0x440(%rsp),%rsi
mov    %ebp,%edi
mov    $0x27ff,%edx
call   10d0 <read@plt>
movb   $0x0,0x2c3f(%rsp)
test   %eax,%eax
je     1509 <net_curl+0x309>
mov    %eax,%ebx
lea    0x440(%rsp),%r12
test   %ebx,%ebx
js     1517 <net_curl+0x317>
mov    %ebx,%eax
movb   $0x0,0x440(%rsp,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
mov    (%rsp),%rdx
call   1060 <strlcat@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x27ff,%edx
call   10d0 <read@plt>
mov    %eax,%ebx
movb   $0x0,0x2c3f(%rsp)
test   %eax,%eax
jne    14cf <net_curl+0x2cf>
mov    %ebp,%edi
call   10c0 <close@plt>
xor    %ebx,%ebx
jmp    127b <net_curl+0x7b>
mov    %ebp,%edi
call   10c0 <close@plt>
jmp    127b <net_curl+0x7b>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64* }
@AF_INET = global i32 0
@.str = [13 x i8] c"\0A\0Acurl '%s'\0A\00"
@EINVAL = global i32 0
@.str.1 = [8 x i8] c"http://\00"
@.str.2 = [26 x i8] c"curl host '%s' path '%s'\0A\00"
@ENOENT = global i32 0
@.str.3 = [24 x i8] c"can't resolve '%s', %d\0A\00"
@SOCK_STREAM = global i32 0
@.str.4 = [23 x i8] c"can't open TCP socket\0A\00"
@ENOMEM = global i32 0
@.str.5 = [20 x i8] c"TCP - will connect\0A\00"
@.str.6 = [21 x i8] c"can't connect to %s\0A\00"
@.str.7 = [17 x i8] c"TCP - connected\0A\00"
@.str.8 = [6 x i8] c"GET /\00"
@.str.9 = [18 x i8] c" HTTP/1.1\0D\0AHost: \00"
@.str.10 = [107 x i8] c"\0D\0AUser-Agent: PhantomOSNetTest/0.1 (PhantomOS i686; ru)\0D\0AAccept: text/html,text/plain\0D\0AConnection: close\0D\0A\00"
@.str.11 = [3 x i8] c"\0D\0A\00"
define i32 @net_curl(i8* %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
%13 = alloca [513 x i8]
%14 = alloca [513 x i8]
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i32
%18 = alloca i32
%19 = alloca %struct.hostent*
%20 = alloca [10240 x i8]
%21 = alloca i32
%22 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i32 0, i32* %10
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %23, i32* %24
%25 = call i32 @htons(i32 80)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i8*, i8** %6
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = load i8*, i8** %6
%30 = call i32 @strlen(i8* %29)
%31 = icmp sgt i32 %30, 512
br i1 %31, label %32, label %34
32: ; preds = %4
%33 = load i32, i32* @EINVAL
store i32 %33, i32* %5
br label %189
34: ; preds = %4
%35 = load i8*, i8** %6
%36 = call i64 @strncmp(i8* %35, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 7)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = load i32, i32* @EINVAL
store i32 %39, i32* %5
br label %189
40: ; preds = %34
%41 = load i8*, i8** %6
%42 = getelementptr inbounds i8, i8* %41, i64 7
store i8* %42, i8** %6
%43 = load i8*, i8** %6
%44 = call i8* @strchr(i8* %43, i8 signext 47)
store i8* %44, i8** %16
%45 = load i8*, i8** %16
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = load i32, i32* @EINVAL
store i32 %48, i32* %5
br label %189
49: ; preds = %40
%50 = getelementptr inbounds [513 x i8], [513 x i8]* %13, i64 0, i64 0
%51 = load i8*, i8** %6
%52 = load i8*, i8** %16
%53 = load i8*, i8** %6
%54 = ptrtoint i8* %52 to i64
%55 = ptrtoint i8* %53 to i64
%56 = sub i64 %54, %55
%57 = add nsw i64 %56, 1
%58 = trunc i64 %57 to i32
%59 = call i32 @strlcpy(i8* %50, i8* %51, i32 %58)
%60 = getelementptr inbounds [513 x i8], [513 x i8]* %14, i64 0, i64 0
%61 = load i8*, i8** %16
%62 = getelementptr inbounds i8, i8* %61, i64 1
%63 = call i32 @strlcpy(i8* %60, i8* %62, i32 512)
%64 = getelementptr inbounds [513 x i8], [513 x i8]* %13, i64 0, i64 0
%65 = call i8* @strchr(i8* %64, i8 signext 58)
store i8* %65, i8** %16
%66 = load i8*, i8** %16
%67 = icmp ne i8* %66, null
br i1 %67, label %68, label %79
68: ; preds = %49
%69 = load i8*, i8** %16
store i8 0, i8* %69
%70 = load i8*, i8** %16
%71 = getelementptr inbounds i8, i8* %70, i64 1
%72 = call i32 @atoi(i8* %71)
store i32 %72, i32* %17
%73 = load i32, i32* %17
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %68
%76 = load i32, i32* %17
%77 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %76, i32* %77
br label %78
78: ; preds = %75, %68
br label %79
79: ; preds = %78, %49
%80 = getelementptr inbounds [513 x i8], [513 x i8]* %13, i64 0, i64 0
%81 = getelementptr inbounds [513 x i8], [513 x i8]* %14, i64 0, i64 0
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %80, i8* %81)
%83 = getelementptr inbounds [513 x i8], [513 x i8]* %13, i64 0, i64 0
%84 = call %struct.hostent* @gethostbyname(i8* %83)
store %struct.hostent* %84, %struct.hostent** %19
%85 = load %struct.hostent*, %struct.hostent** %19
%86 = icmp eq %struct.hostent* %85, null
br i1 %86, label %87, label %96
87: ; preds = %79
%88 = load i32, i32* %12
%89 = load i32, i32* @ENOENT
%90 = icmp eq i32 %88, %89
%91 = zext i1 %90 to i32
%92 = getelementptr inbounds [513 x i8], [513 x i8]* %13, i64 0, i64 0
%93 = load i32, i32* %12
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %92, i32 %93)
%95 = load i32, i32* %12
store i32 %95, i32* %5
br label %189
96: ; preds = %79
%97 = load %struct.hostent*, %struct.hostent** %19
%98 = getelementptr inbounds %struct.hostent, %struct.hostent* %97, i32 0, i32 0
%99 = load i64*, i64** %98
%100 = getelementptr inbounds i64, i64* %99, i64 0
%101 = load i64, i64* %100
%102 = inttoptr i64 %101 to i64*
%103 = load i64, i64* %102
%104 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%105 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %104, i32 0, i32 0
store i64 %103, i64* %105
%106 = load i32, i32* @AF_INET
%107 = load i32, i32* @SOCK_STREAM
%108 = call i32 @socket(i32 %106, i32 %107, i32 0)
store i32 %108, i32* %15
%109 = load i32, i32* %15
%110 = icmp slt i32 %109, 0
br i1 %110, label %111, label %114
111: ; preds = %96
%112 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%113 = load i32, i32* @ENOMEM
store i32 %113, i32* %5
br label %189
114: ; preds = %96
%115 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%116 = load i32, i32* %15
%117 = bitcast %struct.sockaddr_in* %11 to i8*
%118 = call i64 @connect(i32 %116, i8* %117, i32 24)
%119 = icmp ne i64 %118, 0
br i1 %119, label %120, label %123
120: ; preds = %114
%121 = getelementptr inbounds [513 x i8], [513 x i8]* %13, i64 0, i64 0
%122 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %121)
br label %186
123: ; preds = %114
%124 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%125 = getelementptr inbounds [10240 x i8], [10240 x i8]* %20, i64 0, i64 0
%126 = call i32 @memset(i8* %125, i32 0, i64 10240)
%127 = getelementptr inbounds [10240 x i8], [10240 x i8]* %20, i64 0, i64 0
%128 = call i32 @strlcpy(i8* %127, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i32 10240)
%129 = getelementptr inbounds [10240 x i8], [10240 x i8]* %20, i64 0, i64 0
%130 = getelementptr inbounds [513 x i8], [513 x i8]* %14, i64 0, i64 0
%131 = call i32 @strlcat(i8* %129, i8* %130, i64 10240)
%132 = getelementptr inbounds [10240 x i8], [10240 x i8]* %20, i64 0, i64 0
%133 = call i32 @strlcat(i8* %132, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0), i64 10240)
%134 = getelementptr inbounds [10240 x i8], [10240 x i8]* %20, i64 0, i64 0
%135 = getelementptr inbounds [513 x i8], [513 x i8]* %13, i64 0, i64 0
%136 = call i32 @strlcat(i8* %134, i8* %135, i64 10240)
%137 = getelementptr inbounds [10240 x i8], [10240 x i8]* %20, i64 0, i64 0
%138 = call i32 @strlcat(i8* %137, i8* getelementptr inbounds ([107 x i8], [107 x i8]* @.str.10, i64 0, i64 0), i64 10240)
%139 = load i8*, i8** %9
%140 = icmp ne i8* %139, null
br i1 %140, label %141, label %145
141: ; preds = %123
%142 = getelementptr inbounds [10240 x i8], [10240 x i8]* %20, i64 0, i64 0
%143 = load i8*, i8** %9
%144 = call i32 @strlcat(i8* %142, i8* %143, i64 10240)
br label %145
145: ; preds = %141, %123
%146 = getelementptr inbounds [10240 x i8], [10240 x i8]* %20, i64 0, i64 0
%147 = call i32 @strlcat(i8* %146, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0), i64 10240)
%148 = getelementptr inbounds [10240 x i8], [10240 x i8]* %20, i64 0, i64 0
%149 = call i32 @strlen(i8* %148)
store i32 %149, i32* %21
%150 = load i32, i32* %15
%151 = getelementptr inbounds [10240 x i8], [10240 x i8]* %20, i64 0, i64 0
%152 = load i32, i32* %21
%153 = call i32 @write(i32 %150, i8* %151, i32 %152)
store i32 %153, i32* %22
%154 = load i32, i32* %22
%155 = load i32, i32* %21
%156 = icmp ne i32 %154, %155
br i1 %156, label %157, label %158
157: ; preds = %145
br label %186
158: ; preds = %145
%159 = load i8*, i8** %7
%160 = load i64, i64* %8
%161 = call i32 @memset(i8* %159, i32 0, i64 %160)
br label %162
162: ; preds = %158, %177
%163 = load i32, i32* %15
%164 = getelementptr inbounds [10240 x i8], [10240 x i8]* %20, i64 0, i64 0
%165 = call i32 @read(i32 %163, i8* %164, i32 10239)
store i32 %165, i32* %10
%166 = getelementptr inbounds [10240 x i8], [10240 x i8]* %20, i64 0, i64 10239
store i8 0, i8* %166
%167 = load i32, i32* %10
%168 = icmp eq i32 %167, 0
br i1 %168, label %169, label %170
169: ; preds = %162
br label %185
170: ; preds = %162
%171 = load i32, i32* %10
%172 = icmp slt i32 %171, 0
br i1 %172, label %173, label %177
173: ; preds = %170
%174 = load i32, i32* %15
%175 = call i32 @close(i32 %174)
%176 = load i32, i32* %10
store i32 %176, i32* %5
br label %189
177: ; preds = %170
%178 = load i32, i32* %10
%179 = sext i32 %178 to i64
%180 = getelementptr inbounds [10240 x i8], [10240 x i8]* %20, i64 0, i64 %179
store i8 0, i8* %180
%181 = load i8*, i8** %7
%182 = getelementptr inbounds [10240 x i8], [10240 x i8]* %20, i64 0, i64 0
%183 = load i64, i64* %8
%184 = call i32 @strlcat(i8* %181, i8* %182, i64 %183)
br label %162
185: ; preds = %169
br label %186
186: ; preds = %185, %157, %120
%187 = load i32, i32* %15
%188 = call i32 @close(i32 %187)
store i32 0, i32* %5
br label %189
189: ; preds = %186, %173, %111, %87, %47, %38, %32
%190 = load i32, i32* %5
ret i32 %190
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64* }
@AF_INET = global i32 0
@.str = [13 x i8] c"\0A\0Acurl '%s'\0A\00"
@EINVAL = global i32 0
@.str.1 = [8 x i8] c"http://\00"
@.str.2 = [26 x i8] c"curl host '%s' path '%s'\0A\00"
@ENOENT = global i32 0
@.str.3 = [24 x i8] c"can't resolve '%s', %d\0A\00"
@SOCK_STREAM = global i32 0
@ENOMEM = global i32 0
@.str.6 = [21 x i8] c"can't connect to %s\0A\00"
@.str.8 = [6 x i8] c"GET /\00"
@.str.9 = [18 x i8] c" HTTP/1.1\0D\0AHost: \00"
@.str.10 = [107 x i8] c"\0D\0AUser-Agent: PhantomOSNetTest/0.1 (PhantomOS i686; ru)\0D\0AAccept: text/html,text/plain\0D\0AConnection: close\0D\0A\00"
@.str.11 = [3 x i8] c"\0D\0A\00"
@str = [19 x i8] c"TCP - will connect\00"
@str.12 = [16 x i8] c"TCP - connected\00"
@str.13 = [22 x i8] c"can't open TCP socket\00"
define i32 @net_curl(i8* %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca [513 x i8]
%7 = alloca [513 x i8]
%8 = alloca [10240 x i8]
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %10, i32* %11
%12 = tail call i32 @htons(i32 80)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %12, i32* %13
%14 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%15 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0)
%17 = tail call i32 @strlen(i8* %0)
%18 = icmp sgt i32 %17, 512
br i1 %18, label %19, label %21
19: ; preds = %4
%20 = load i32, i32* @EINVAL
br label %109
21: ; preds = %4
%22 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 7)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %21
%25 = load i32, i32* @EINVAL
br label %109
26: ; preds = %21
%27 = getelementptr inbounds i8, i8* %0, i64 7
%28 = tail call i8* @strchr(i8* %27, i8 signext 47)
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i32, i32* @EINVAL
br label %109
32: ; preds = %26
%33 = ptrtoint i8* %28 to i64
%34 = ptrtoint i8* %27 to i64
%35 = sub i64 %33, %34
%36 = trunc i64 %35 to i32
%37 = add i32 %36, 1
%38 = call i32 @strlcpy(i8* %14, i8* %27, i32 %37)
%39 = getelementptr inbounds i8, i8* %28, i64 1
%40 = call i32 @strlcpy(i8* %15, i8* %39, i32 512)
%41 = call i8* @strchr(i8* %14, i8 signext 58)
%42 = icmp eq i8* %41, null
br i1 %42, label %48, label %43
43: ; preds = %32
store i8 0, i8* %41
%44 = getelementptr inbounds i8, i8* %41, i64 1
%45 = call i32 @atoi(i8* %44)
%46 = icmp eq i32 %45, 0
br i1 %46, label %48, label %47
47: ; preds = %43
store i32 %45, i32* %13
br label %48
48: ; preds = %43, %47, %32
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %14, i8* %15)
%50 = call %struct.hostent* @gethostbyname(i8* %14)
%51 = icmp eq %struct.hostent* %50, null
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %14, i32 undef)
br label %109
54: ; preds = %48
%55 = getelementptr inbounds %struct.hostent, %struct.hostent* %50, i64 0, i32 0
%56 = load i64*, i64** %55
%57 = load i64, i64* %56
%58 = inttoptr i64 %57 to i64*
%59 = load i64, i64* %58
%60 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i64 %59, i64* %60
%61 = load i32, i32* @AF_INET
%62 = load i32, i32* @SOCK_STREAM
%63 = call i32 @socket(i32 %61, i32 %62, i32 0)
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %54
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.13, i64 0, i64 0))
%67 = load i32, i32* @ENOMEM
br label %109
68: ; preds = %54
%69 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%70 = call i64 @connect(i32 %63, i8* %9, i32 24)
%71 = icmp eq i64 %70, 0
br i1 %71, label %74, label %72
72: ; preds = %68
%73 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %14)
br label %107
74: ; preds = %68
%75 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.12, i64 0, i64 0))
%76 = getelementptr inbounds [10240 x i8], [10240 x i8]* %8, i64 0, i64 0
%77 = call i32 @memset(i8* %76, i32 0, i64 10240)
%78 = call i32 @strlcpy(i8* %76, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0), i32 10240)
%79 = call i32 @strlcat(i8* %76, i8* %15, i64 10240)
%80 = call i32 @strlcat(i8* %76, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0), i64 10240)
%81 = call i32 @strlcat(i8* %76, i8* %14, i64 10240)
%82 = call i32 @strlcat(i8* %76, i8* getelementptr inbounds ([107 x i8], [107 x i8]* @.str.10, i64 0, i64 0), i64 10240)
%83 = icmp eq i8* %3, null
br i1 %83, label %86, label %84
84: ; preds = %74
%85 = call i32 @strlcat(i8* %76, i8* %3, i64 10240)
br label %86
86: ; preds = %84, %74
%87 = call i32 @strlcat(i8* %76, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.11, i64 0, i64 0), i64 10240)
%88 = call i32 @strlen(i8* %76)
%89 = call i32 @write(i32 %63, i8* %76, i32 %88)
%90 = icmp eq i32 %89, %88
br i1 %90, label %91, label %107
91: ; preds = %86
%92 = call i32 @memset(i8* %1, i32 0, i64 %2)
%93 = call i32 @read(i32 %63, i8* %76, i32 10239)
%94 = getelementptr inbounds [10240 x i8], [10240 x i8]* %8, i64 0, i64 10239
store i8 0, i8* %94
%95 = icmp eq i32 %93, 0
br i1 %95, label %107, label %96
96: ; preds = %91, %101
%97 = phi i32 [ %105, %101 ], [ %93, %91 ]
%98 = icmp slt i32 %97, 0
br i1 %98, label %99, label %101
99: ; preds = %96
%100 = call i32 @close(i32 %63)
br label %109
101: ; preds = %96
%102 = zext i32 %97 to i64
%103 = getelementptr inbounds [10240 x i8], [10240 x i8]* %8, i64 0, i64 %102
store i8 0, i8* %103
%104 = call i32 @strlcat(i8* %1, i8* %76, i64 %2)
%105 = call i32 @read(i32 %63, i8* %76, i32 10239)
store i8 0, i8* %94
%106 = icmp eq i32 %105, 0
br i1 %106, label %107, label %96
107: ; preds = %101, %91, %86, %72
%108 = call i32 @close(i32 %63)
br label %109
109: ; preds = %30, %107, %99, %65, %52, %24, %19
%110 = phi i32 [ %20, %19 ], [ %25, %24 ], [ %31, %30 ], [ undef, %52 ], [ %67, %65 ], [ 0, %107 ], [ %97, %99 ]
ret i32 %110
}