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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_client_socket(struct sockaddr_in *server_addr, int portnumber,
                         char *remote_ip) {
  int sockfd;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    printf("Socket error\n");
    return -1;
  }
  printf("dbg: create socket!\n");
  bzero(server_addr, sizeof(struct sockaddr_in));
  server_addr->sin_family = AF_INET;
  server_addr->sin_addr.s_addr = inet_addr(remote_ip);
  server_addr->sin_port = htons(portnumber);
  printf("dbg, finish create socket!\n");
  return sockfd;
}


x86 assembly (clang, -O0):
<create_client_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11a0 <create_client_socket+0x50>
lea    0xe71(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <create_client_socket+0xae>
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x20(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
lea    0xe30(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_client_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e80(%rip),%r12
mov    (%r12),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11c1 <create_client_socket+0x71>
lea    0xe76(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rbx)
mov    %r15,%rdi
call   1050 <inet_addr@plt>
mov    %eax,0x4(%rbx)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,(%rbx)
lea    0xe55(%rip),%rdi
jmp    11c8 <create_client_socket+0x78>
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%eax
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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"Socket error\0A\00"
@.str.1 = [21 x i8] c"dbg: create socket!\0A\00"
@.str.2 = [28 x i8] c"dbg, finish create socket!\0A\00"
define i32 @create_client_socket(%struct.sockaddr_in* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %8
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %33
15: ; preds = %3
%16 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%17 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%18 = call i32 @bzero(%struct.sockaddr_in* %17, i32 12)
%19 = load i32, i32* @AF_INET
%20 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 2
store i32 %19, i32* %21
%22 = load i8*, i8** %7
%23 = call i32 @inet_addr(i8* %22)
%24 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %23, i32* %26
%27 = load i32, i32* %6
%28 = call i32 @htons(i32 %27)
%29 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* %8
store i32 %32, i32* %4
br label %33
33: ; preds = %15, %13
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@str = [20 x i8] c"dbg: create socket!\00"
@str.3 = [27 x i8] c"dbg, finish create socket!\00"
@str.4 = [13 x i8] c"Socket error\00"
define i32 @create_client_socket(%struct.sockaddr_in* nocapture writeonly %0, i32 %1, i8* %2) {
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %17, label %8
8: ; preds = %3
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @bzero(%struct.sockaddr_in* %0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 2
store i32 %11, i32* %12
%13 = tail call i32 @inet_addr(i8* %2)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 1, i32 0
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 %1)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %0, i64 0, i32 0
store i32 %15, i32* %16
br label %17
17: ; preds = %3, %8
%18 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @str.3, i64 0, i64 0), %8 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @str.4, i64 0, i64 0), %3 ]
%19 = tail call i32 @puts(i8* dereferenceable(1) %18)
ret i32 %6
}