#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
int sdbd_connect(char *url) {
  struct sockaddr_in serv_addr;
  struct hostent *server;
  char *p, host[1024];
  int port, sockfd;
  p = strstr(url, ":url=");
  if (p == NULL) {
    fprintf(stderr, "No url\n");
    return -1;
  }
  strncpy(host, url, p - url);
  host[p - url] = '\0';
  url = p + 5;
  p = strchr(host, ':');
  if (p == NULL) {
    fprintf(stderr, "No port\n");
    return -1;
  }
  *p = '\0';
  port = atoi(p + 1);
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    fprintf(stderr, "Error opening socket\n");
    return -1;
  }
  server = gethostbyname(host);
  if (server == NULL) {
    fprintf(stderr, "Error, no such host\n");
    return -1;
  }
  memset((char *)&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  memcpy((char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr,
         server->h_length);
  serv_addr.sin_port = htons(port);
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    fprintf(stderr, "Error connecting\n");
    close(sockfd);
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<sdbd_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe26(%rip),%rsi
call   10d0 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    120f <sdbd_connect+0x4f>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <sdbd_connect+0x207>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
movb   $0x0,-0x430(%rbp,%rax,1)
mov    -0x30(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x10(%rbp)
lea    -0x430(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    128c <sdbd_connect+0xcc>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <sdbd_connect+0x207>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x434(%rbp)
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x438(%rbp)
cmpl   $0x0,-0x438(%rbp)
jge    12f3 <sdbd_connect+0x133>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
lea    0xd35(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <sdbd_connect+0x207>
lea    -0x430(%rbp),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    132f <sdbd_connect+0x16f>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
lea    0xd0f(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <sdbd_connect+0x207>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2ca2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
add    $0x4,%rdi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   10a0 <memcpy@plt>
mov    -0x434(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x438(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    13be <sdbd_connect+0x1fe>
mov    0x2c3f(%rip),%rax
mov    (%rax),%edi
lea    0xca0(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x438(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <sdbd_connect+0x207>
mov    -0x438(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sdbd_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rdi,%rbx
lea    0xe27(%rip),%rsi
call   10d0 <strstr@plt>
test   %rax,%rax
je     12cf <sdbd_connect+0x10f>
mov    %rax,%rbp
sub    %rbx,%rbp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x10(%rsp,%rbp,1)
mov    %r14,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     12e1 <sdbd_connect+0x121>
movb   $0x0,(%rax)
add    $0x1,%rax
mov    %rax,%rdi
call   10b0 <atoi@plt>
mov    %eax,%r14d
mov    0x2db5(%rip),%r12
mov    (%r12),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
js     12f3 <sdbd_connect+0x133>
mov    %eax,%ebx
lea    0x10(%rsp),%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     1305 <sdbd_connect+0x145>
mov    %rax,%rbp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
lea    0x4(%rsp),%rdi
mov    0x8(%rbp),%rsi
mov    0x0(%rbp),%edx
call   10a0 <memcpy@plt>
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
jns    131f <sdbd_connect+0x15f>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
lea    0xd81(%rip),%rsi
call   1090 <fprintf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    131a <sdbd_connect+0x15a>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
lea    0xd27(%rip),%rsi
jmp    1315 <sdbd_connect+0x155>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
lea    0xd1d(%rip),%rsi
jmp    1315 <sdbd_connect+0x155>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
lea    0xd14(%rip),%rsi
jmp    1315 <sdbd_connect+0x155>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
lea    0xd18(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [6 x i8] c":url=\00"
@stderr = global i32 0
@.str.1 = [8 x i8] c"No url\0A\00"
@.str.2 = [9 x i8] c"No port\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [22 x i8] c"Error opening socket\0A\00"
@.str.4 = [21 x i8] c"Error, no such host\0A\00"
@.str.5 = [18 x i8] c"Error connecting\0A\00"
define i32 @sdbd_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.hostent*
%6 = alloca i8*
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = call i8* @strstr(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %93
17: ; preds = %1
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%19 = load i8*, i8** %3
%20 = load i8*, i8** %6
%21 = load i8*, i8** %3
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
%25 = trunc i64 %24 to i32
%26 = call i32 @strncpy(i8* %18, i8* %19, i32 %25)
%27 = load i8*, i8** %6
%28 = load i8*, i8** %3
%29 = ptrtoint i8* %27 to i64
%30 = ptrtoint i8* %28 to i64
%31 = sub i64 %29, %30
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %31
store i8 0, i8* %32
%33 = load i8*, i8** %6
%34 = getelementptr inbounds i8, i8* %33, i64 5
store i8* %34, i8** %3
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%36 = call i8* @strchr(i8* %35, i8 signext 58)
store i8* %36, i8** %6
%37 = load i8*, i8** %6
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %42
39: ; preds = %17
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %93
42: ; preds = %17
%43 = load i8*, i8** %6
store i8 0, i8* %43
%44 = load i8*, i8** %6
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = call i32 @atoi(i8* %45)
store i32 %46, i32* %8
%47 = load i32, i32* @AF_INET
%48 = load i32, i32* @SOCK_STREAM
%49 = call i32 @socket(i32 %47, i32 %48, i32 0)
store i32 %49, i32* %9
%50 = load i32, i32* %9
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %42
%53 = load i32, i32* @stderr
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %93
55: ; preds = %42
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%57 = call %struct.hostent* @gethostbyname(i8* %56)
store %struct.hostent* %57, %struct.hostent** %5
%58 = load %struct.hostent*, %struct.hostent** %5
%59 = icmp eq %struct.hostent* %58, null
br i1 %59, label %60, label %63
60: ; preds = %55
%61 = load i32, i32* @stderr
%62 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %93
63: ; preds = %55
%64 = bitcast %struct.sockaddr_in* %4 to i8*
%65 = call i32 @memset(i8* %64, i32 0, i32 12)
%66 = load i32, i32* @AF_INET
%67 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %66, i32* %67
%68 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%69 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %68, i32 0, i32 0
%70 = bitcast i32* %69 to i8*
%71 = load %struct.hostent*, %struct.hostent** %5
%72 = getelementptr inbounds %struct.hostent, %struct.hostent* %71, i32 0, i32 1
%73 = load i64, i64* %72
%74 = inttoptr i64 %73 to i8*
%75 = load %struct.hostent*, %struct.hostent** %5
%76 = getelementptr inbounds %struct.hostent, %struct.hostent* %75, i32 0, i32 0
%77 = load i32, i32* %76
%78 = call i32 @memcpy(i8* %70, i8* %74, i32 %77)
%79 = load i32, i32* %8
%80 = call i32 @htons(i32 %79)
%81 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %80, i32* %81
%82 = load i32, i32* %9
%83 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%84 = call i64 @connect(i32 %82, %struct.sockaddr* %83, i32 12)
%85 = icmp slt i64 %84, 0
br i1 %85, label %86, label %91
86: ; preds = %63
%87 = load i32, i32* @stderr
%88 = call i32 @fprintf(i32 %87, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%89 = load i32, i32* %9
%90 = call i32 @close(i32 %89)
store i32 -1, i32* %2
br label %93
91: ; preds = %63
%92 = load i32, i32* %9
store i32 %92, i32* %2
br label %93
93: ; preds = %91, %86, %60, %52, %39, %14
%94 = load i32, i32* %2
ret i32 %94
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [6 x i8] c":url=\00"
@stderr = global i32 0
@.str.1 = [8 x i8] c"No url\0A\00"
@.str.2 = [9 x i8] c"No port\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [22 x i8] c"Error opening socket\0A\00"
@.str.4 = [21 x i8] c"Error, no such host\0A\00"
@.str.5 = [18 x i8] c"Error connecting\0A\00"
define i32 @sdbd_connect(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca [1024 x i8]
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %60
11: ; preds = %1
%12 = ptrtoint i8* %6 to i64
%13 = ptrtoint i8* %0 to i64
%14 = sub i64 %12, %13
%15 = trunc i64 %14 to i32
%16 = call i32 @strncpy(i8* %5, i8* %0, i32 %15)
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %14
store i8 0, i8* %17
%18 = call i8* @strchr(i8* %5, i8 signext 58)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %11
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %60
23: ; preds = %11
store i8 0, i8* %18
%24 = getelementptr inbounds i8, i8* %18, i64 1
%25 = call i32 @atoi(i8* %24)
%26 = load i32, i32* @AF_INET
%27 = load i32, i32* @SOCK_STREAM
%28 = call i32 @socket(i32 %26, i32 %27, i32 0)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %60
33: ; preds = %23
%34 = call %struct.hostent* @gethostbyname(i8* %5)
%35 = icmp eq %struct.hostent* %34, null
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %60
39: ; preds = %33
%40 = call i32 @memset(i8* %4, i32 0, i32 12)
%41 = load i32, i32* @AF_INET
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %41, i32* %42
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%44 = bitcast i32* %43 to i8*
%45 = getelementptr inbounds %struct.hostent, %struct.hostent* %34, i64 0, i32 1
%46 = load i64, i64* %45
%47 = inttoptr i64 %46 to i8*
%48 = getelementptr inbounds %struct.hostent, %struct.hostent* %34, i64 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 @memcpy(i8* %44, i8* %47, i32 %49)
%51 = call i32 @htons(i32 %25)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %51, i32* %52
%53 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%54 = call i64 @connect(i32 %28, %struct.sockaddr* %53, i32 12)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %39
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%59 = call i32 @close(i32 %28)
br label %60
60: ; preds = %39, %56, %36, %30, %20, %8
%61 = phi i32 [ -1, %8 ], [ -1, %20 ], [ -1, %30 ], [ -1, %36 ], [ -1, %56 ], [ %28, %39 ]
ret i32 %61
}