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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ htons(int);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int test_bad_connect(int sock) {
  struct sockaddr_in sa;
  int t;
  bzero(&sa, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_port = htons(81);
  if ((t = inet_pton(AF_INET, "127.0.0.1", &sa.sin_addr)) != 1) {
    fprintf(stderr, "My bad. Can't inet_pton. Return code: %d\n", t);
    return -1;
  }
  return connect(sock, (struct sockaddr *)&sa, sizeof(sa));
}


x86 assembly (clang, -O0):
<test_bad_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x51,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rdx
lea    0xe6a(%rip),%rsi
call   1050 <inet_pton@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x1,%eax
je     11cb <test_bad_connect+0x7b>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe50(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <test_bad_connect+0x8f>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_bad_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    0x2e76(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    $0x51,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%edi
lea    0xe6f(%rip),%rsi
mov    %rbx,%rdx
call   1050 <inet_pton@plt>
cmp    $0x1,%eax
jne    11b0 <test_bad_connect+0x60>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
jmp    11cc <test_bad_connect+0x7c>
mov    0x2e21(%rip),%rcx
mov    (%rcx),%edi
lea    0xe4a(%rip),%rsi
mov    %eax,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"My bad. Can't inet_pton. Return code: %d\0A\00"
define i32 @test_bad_connect(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32 %0, i32* %3
%6 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %7, i32* %8
%9 = call i32 @htons(i32 81)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %9, i32* %10
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%13 = call i32 @inet_pton(i32 %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %12)
store i32 %13, i32* %5
%14 = icmp ne i32 %13, 1
br i1 %14, label %15, label %19
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = load i32, i32* %5
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %17)
store i32 -1, i32* %2
br label %23
19: ; preds = %1
%20 = load i32, i32* %3
%21 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%22 = call i32 @connect(i32 %20, %struct.sockaddr* %21, i32 12)
store i32 %22, i32* %2
br label %23
23: ; preds = %19, %15
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"My bad. Can't inet_pton. Return code: %d\0A\00"
define i32 @test_bad_connect(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %5, i32* %6
%7 = tail call i32 @htons(i32 81)
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %7, i32* %8
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%11 = call i32 @inet_pton(i32 %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %10)
%12 = icmp eq i32 %11, 1
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %19
16: ; preds = %1
%17 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%18 = call i32 @connect(i32 %0, %struct.sockaddr* %17, i32 12)
br label %19
19: ; preds = %16, %13
%20 = phi i32 [ -1, %13 ], [ %18, %16 ]
ret i32 %20
}