#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 /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa_dest;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ bzero(char *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stuff_net_addr(int /*<<< orphan*/ *, char *);
int setup_transmit(int usd, char *host, short port) {
  struct sockaddr_in sa_dest;
  bzero((char *)&sa_dest, sizeof(sa_dest));
  sa_dest.sin_family = AF_INET;
  if (!stuff_net_addr(&(sa_dest.sin_addr), host))
    return 0;
  sa_dest.sin_port = htons(port);
  if (connect(usd, (struct sockaddr *)&sa_dest, sizeof(sa_dest)) == -1) {
    perror("connect");
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<setup_transmit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dx,%ax
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1050 <bzero@plt>
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
add    $0x4,%rdi
mov    -0x10(%rbp),%rsi
call   1070 <stuff_net_addr@plt>
cmp    $0x0,%eax
jne    11a6 <setup_transmit+0x56>
movl   $0x0,-0x4(%rbp)
jmp    11eb <setup_transmit+0x9b>
movswl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    11e4 <setup_transmit+0x94>
lea    0xe2d(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11eb <setup_transmit+0x9b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_transmit>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsp,%rdi
mov    $0xc,%esi
call   1050 <bzero@plt>
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x4(%rsp),%rdi
mov    %rbx,%rsi
call   1070 <stuff_net_addr@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
je     11b6 <setup_transmit+0x66>
movswl %bp,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   1060 <connect@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    $0xffffffff,%ecx
je     11bf <setup_transmit+0x6f>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe3a(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
jmp    11b6 <setup_transmit+0x66>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [8 x i8] c"connect\00"
define i32 @setup_transmit(i32 %0, i8* %1, i16 signext %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i16
%8 = alloca %struct.sockaddr_in
store i32 %0, i32* %5
store i8* %1, i8** %6
store i16 %2, i16* %7
%9 = bitcast %struct.sockaddr_in* %8 to i8*
%10 = call i32 @bzero(i8* %9, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%14 = load i8*, i8** %6
%15 = call i32 @stuff_net_addr(i32* %13, i8* %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %3
store i32 0, i32* %4
br label %29
18: ; preds = %3
%19 = load i16, i16* %7
%20 = call i32 @htons(i16 signext %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* %5
%23 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%24 = call i32 @connect(i32 %22, %struct.sockaddr* %23, i32 12)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %29
28: ; preds = %18
store i32 1, i32* %4
br label %29
29: ; preds = %28, %26, %17
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [8 x i8] c"connect\00"
define i32 @setup_transmit(i32 %0, i8* %1, i16 signext %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = call i32 @bzero(i8* %5, i32 12)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%10 = call i32 @stuff_net_addr(i32* %9, i8* %1)
%11 = icmp eq i32 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %3
%13 = call i32 @htons(i16 signext %2)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %13, i32* %14
%15 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%16 = call i32 @connect(i32 %0, %struct.sockaddr* %15, i32 12)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %12, %3, %18
%21 = phi i32 [ 0, %18 ], [ 0, %3 ], [ 1, %12 ]
ret i32 %21
}