#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 */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ get_sockaddr(struct sockaddr_in *, char *, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int do_open_client_socket(char *host, int port, int connection_type) {
  struct sockaddr_in sin;
  int sock;
  int res;
  sock = socket(PF_INET, connection_type, 0);
  if (sock == -1) {
    fprintf(stderr, "socket() failed\n");
    return -1;
  }
  if (connection_type != SOCK_STREAM) {
    int on = 1;
    if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) == -1) {
      return -1;
    }
    return sock;
  }
  if (get_sockaddr(&sin, host, port) < 0) {
    fprintf(stderr, "get_sockaddr() failed\n");
    close(sock);
    return -1;
  }
  if ((res = connect(sock, (struct sockaddr *)&sin, sizeof(sin))) == -1) {
    close(sock);
    return -1;
  } else {
    fprintf(stderr, "server connect\n");
  }
  return sock;
}


x86 assembly (clang, -O0):
<do_open_client_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11b3 <do_open_client_socket+0x53>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <do_open_client_socket+0x13f>
mov    -0x18(%rbp),%eax
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%eax
je     1210 <do_open_client_socket+0xb0>
movl   $0x1,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1205 <do_open_client_socket+0xa5>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <do_open_client_socket+0x13f>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <do_open_client_socket+0x13f>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%rdi
call   1030 <get_sockaddr@plt>
cmp    $0x0,%rax
jge    1253 <do_open_client_socket+0xf3>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <do_open_client_socket+0x13f>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
jne    1284 <do_open_client_socket+0x124>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <do_open_client_socket+0x13f>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    0xd94(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_open_client_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    %edx,%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11d1 <do_open_client_socket+0x71>
mov    %eax,%ebx
mov    0x2e39(%rip),%rax
cmp    %ebp,(%rax)
jne    11e8 <do_open_client_socket+0x88>
mov    %rsp,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1030 <get_sockaddr@plt>
test   %rax,%rax
js     121c <do_open_client_socket+0xbc>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     1231 <do_open_client_socket+0xd1>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1060 <fprintf@plt>
jmp    123d <do_open_client_socket+0xdd>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1238 <do_open_client_socket+0xd8>
movl   $0x1,0x4(%rsp)
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
jmp    123d <do_open_client_socket+0xdd>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"socket() failed\0A\00"
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.1 = [23 x i8] c"get_sockaddr() failed\0A\00"
@.str.2 = [16 x i8] c"server connect\0A\00"
define i32 @do_open_client_socket(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* %7
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %56
20: ; preds = %3
%21 = load i32, i32* %7
%22 = load i32, i32* @SOCK_STREAM
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %33
24: ; preds = %20
store i32 1, i32* %11
%25 = load i32, i32* %9
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_BROADCAST
%28 = call i32 @setsockopt(i32 %25, i32 %26, i32 %27, i32* %11, i32 4)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %24
store i32 -1, i32* %4
br label %56
31: ; preds = %24
%32 = load i32, i32* %9
store i32 %32, i32* %4
br label %56
33: ; preds = %20
%34 = load i8*, i8** %5
%35 = load i32, i32* %6
%36 = call i64 @get_sockaddr(%struct.sockaddr_in* %8, i8* %34, i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%41 = load i32, i32* %9
%42 = call i32 @close(i32 %41)
store i32 -1, i32* %4
br label %56
43: ; preds = %33
%44 = load i32, i32* %9
%45 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%46 = call i32 @connect(i32 %44, %struct.sockaddr* %45, i32 4)
store i32 %46, i32* %10
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* %9
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %4
br label %56
51: ; preds = %43
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %54
54: ; preds = %51
%55 = load i32, i32* %9
store i32 %55, i32* %4
br label %56
56: ; preds = %54, %48, %38, %31, %30, %17
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"socket() failed\0A\00"
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.1 = [23 x i8] c"get_sockaddr() failed\0A\00"
@.str.2 = [16 x i8] c"server connect\0A\00"
define i32 @do_open_client_socket(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = load i32, i32* @PF_INET
%8 = tail call i32 @socket(i32 %7, i32 %2, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %39
13: ; preds = %3
%14 = load i32, i32* @SOCK_STREAM
%15 = icmp eq i32 %14, %2
br i1 %15, label %23, label %16
16: ; preds = %13
%17 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_BROADCAST
%20 = call i32 @setsockopt(i32 %8, i32 %18, i32 %19, i32* %5, i32 4)
%21 = icmp eq i32 %20, -1
%22 = select i1 %21, i32 -1, i32 %8
br label %39
23: ; preds = %13
%24 = call i64 @get_sockaddr(%struct.sockaddr_in* %4, i8* %0, i32 %1)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @close(i32 %8)
br label %39
30: ; preds = %23
%31 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%32 = call i32 @connect(i32 %8, %struct.sockaddr* %31, i32 4)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @close(i32 %8)
br label %39
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %36, %34, %26, %16, %10
%40 = phi i32 [ -1, %10 ], [ %22, %16 ], [ -1, %26 ], [ -1, %34 ], [ %8, %36 ]
ret i32 %40
}