#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_in6 {
  int sin6_family;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int sin_family;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sin6;
typedef int /*<<< orphan*/ sin;

/* Variables and functions */
int AF_INET;
int AF_INET6;
scalar_t__ EADDRNOTAVAIL;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ inet_pton(int, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ strchr(char *, char);
int main(int argc, char **argv) {
  int af, s;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s x.x.x.x\n", argv[0]);
    return -1;
  }
  af = strchr(argv[1], ':') ? AF_INET6 : AF_INET;
  s = socket(af, SOCK_STREAM, 0);
  if (s < 0) {
    perror("socket");
    return -1;
  }
  if (af == AF_INET) {
    struct sockaddr_in sin = {
        .sin_family = AF_INET,
    };
    if (inet_pton(af, argv[1], &sin.sin_addr) <= 0) {
      fprintf(stderr, "%s: invalid address\n", argv[1]);
      return -1;
    }
    if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
      if (errno == EADDRNOTAVAIL)
        return 1;
      perror("bind");
      return -1;
    }
  } else {
    struct sockaddr_in6 sin6 = {
        .sin6_family = AF_INET6,
    };
    if (inet_pton(af, argv[1], &sin6.sin6_addr) <= 0) {
      fprintf(stderr, "%s: invalid address\n", argv[1]);
      return -1;
    }
    if (bind(s, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
      if (errno == EADDRNOTAVAIL)
        return 1;
      perror("bind");
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a8 <main+0x48>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe69(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b0 <main+0x250>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x3a,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     11d5 <main+0x75>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    11e1 <main+0x81>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    121f <main+0xbf>
lea    0xe05(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b0 <main+0x250>
mov    -0x14(%rbp),%eax
mov    0x2dbf(%rip),%rcx
cmp    (%rcx),%eax
jne    12ed <main+0x18d>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdx
add    $0x4,%rdx
call   1050 <inet_pton@plt>
cmp    $0x0,%rax
jg     128f <main+0x12f>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd9c(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b0 <main+0x250>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    12e8 <main+0x188>
mov    0x2d07(%rip),%rax
mov    (%rax),%rax
mov    0x2d25(%rip),%rcx
cmp    (%rcx),%rax
jne    12d0 <main+0x170>
movl   $0x1,-0x4(%rbp)
jmp    13b0 <main+0x250>
lea    0xd58(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b0 <main+0x250>
jmp    13a9 <main+0x249>
mov    0x2ce4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x28(%rbp),%rdx
add    $0x4,%rdx
call   1050 <inet_pton@plt>
cmp    $0x0,%rax
jg     134b <main+0x1eb>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xce0(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b0 <main+0x250>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    13a4 <main+0x244>
mov    0x2c4b(%rip),%rax
mov    (%rax),%rax
mov    0x2c69(%rip),%rcx
cmp    (%rcx),%rax
jne    138c <main+0x22c>
movl   $0x1,-0x4(%rbp)
jmp    13b0 <main+0x250>
lea    0xc9c(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b0 <main+0x250>
jmp    13a9 <main+0x249>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    118c <main+0x2c>
mov    0x8(%rbx),%rdi
mov    $0x3a,%esi
call   1030 <strchr@plt>
test   %rax,%rax
jne    11a4 <main+0x44>
mov    0x2e5e(%rip),%rax
jmp    11ab <main+0x4b>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe61(%rip),%rsi
jmp    1234 <main+0xd4>
mov    0x2e2d(%rip),%rax
mov    (%rax),%ebp
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     126b <main+0x10b>
mov    %eax,%r14d
mov    0x2e17(%rip),%rax
cmp    (%rax),%ebp
jne    11db <main+0x7b>
mov    %ebp,0x8(%rsp)
jmp    11e8 <main+0x88>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0xc(%rsp),%rdx
movl   $0x0,0xc(%rsp)
mov    0x8(%rbx),%rsi
mov    %ebp,%edi
call   1050 <inet_pton@plt>
test   %rax,%rax
jle    1220 <main+0xc0>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0x8,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     1247 <main+0xe7>
xor    %eax,%eax
jmp    123e <main+0xde>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xde6(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d6a(%rip),%rax
mov    (%rax),%rcx
mov    0x2d88(%rip),%rdx
mov    $0x1,%eax
cmp    (%rdx),%rcx
je     123e <main+0xde>
lea    0xdc6(%rip),%rdi
jmp    1272 <main+0x112>
lea    0xda1(%rip),%rdi
call   1070 <perror@plt>
jmp    1239 <main+0xd9>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"Usage: %s x.x.x.x\0A\00"
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [21 x i8] c"%s: invalid address\0A\00"
@errno = global i64 0
@EADDRNOTAVAIL = global i64 0
@.str.3 = [5 x i8] c"bind\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in6
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %102
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @strchr(i8* %21, i8 signext 58)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = load i32, i32* @AF_INET6
br label %28
26: ; preds = %18
%27 = load i32, i32* @AF_INET
br label %28
28: ; preds = %26, %24
%29 = phi i32 [ %25, %24 ], [ %27, %26 ]
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = load i32, i32* @SOCK_STREAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %102
37: ; preds = %28
%38 = load i32, i32* %6
%39 = load i32, i32* @AF_INET
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %71
41: ; preds = %37
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%43 = load i32, i32* @AF_INET
store i32 %43, i32* %42
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 0, i32* %44
%45 = load i32, i32* %6
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%50 = call i64 @inet_pton(i32 %45, i8* %48, i32* %49)
%51 = icmp sle i64 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %41
%53 = load i32, i32* @stderr
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
%57 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %56)
store i32 -1, i32* %3
br label %102
58: ; preds = %41
%59 = load i32, i32* %7
%60 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%61 = call i64 @bind(i32 %59, %struct.sockaddr* %60, i32 8)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %70
63: ; preds = %58
%64 = load i64, i64* @errno
%65 = load i64, i64* @EADDRNOTAVAIL
%66 = icmp eq i64 %64, %65
br i1 %66, label %67, label %68
67: ; preds = %63
store i32 1, i32* %3
br label %102
68: ; preds = %63
%69 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %102
70: ; preds = %58
br label %101
71: ; preds = %37
%72 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %9, i32 0, i32 0
%73 = load i32, i32* @AF_INET6
store i32 %73, i32* %72
%74 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %9, i32 0, i32 1
store i32 0, i32* %74
%75 = load i32, i32* %6
%76 = load i8**, i8*** %5
%77 = getelementptr inbounds i8*, i8** %76, i64 1
%78 = load i8*, i8** %77
%79 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %9, i32 0, i32 1
%80 = call i64 @inet_pton(i32 %75, i8* %78, i32* %79)
%81 = icmp sle i64 %80, 0
br i1 %81, label %82, label %88
82: ; preds = %71
%83 = load i32, i32* @stderr
%84 = load i8**, i8*** %5
%85 = getelementptr inbounds i8*, i8** %84, i64 1
%86 = load i8*, i8** %85
%87 = call i32 @fprintf(i32 %83, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %86)
store i32 -1, i32* %3
br label %102
88: ; preds = %71
%89 = load i32, i32* %7
%90 = bitcast %struct.sockaddr_in6* %9 to %struct.sockaddr*
%91 = call i64 @bind(i32 %89, %struct.sockaddr* %90, i32 8)
%92 = icmp slt i64 %91, 0
br i1 %92, label %93, label %100
93: ; preds = %88
%94 = load i64, i64* @errno
%95 = load i64, i64* @EADDRNOTAVAIL
%96 = icmp eq i64 %94, %95
br i1 %96, label %97, label %98
97: ; preds = %93
store i32 1, i32* %3
br label %102
98: ; preds = %93
%99 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %102
100: ; preds = %88
br label %101
101: ; preds = %100, %70
store i32 0, i32* %3
br label %102
102: ; preds = %101, %98, %97, %82, %68, %67, %52, %35, %12
%103 = load i32, i32* %3
ret i32 %103
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"Usage: %s x.x.x.x\0A\00"
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [21 x i8] c"%s: invalid address\0A\00"
@errno = global i64 0
@EADDRNOTAVAIL = global i64 0
@.str.3 = [5 x i8] c"bind\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in6
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %75
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i64 @strchr(i8* %12, i8 signext 58)
%14 = icmp eq i64 %13, 0
%15 = load i32, i32* @AF_INET6
%16 = load i32, i32* @AF_INET
%17 = select i1 %14, i32 %16, i32 %15
%18 = load i32, i32* @SOCK_STREAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %10
%22 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %75
23: ; preds = %10
%24 = load i32, i32* @AF_INET
%25 = icmp eq i32 %17, %24
br i1 %25, label %26, label %50
26: ; preds = %23
%27 = bitcast %struct.sockaddr_in* %3 to i8*
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %17, i32* %28
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 0, i32* %29
%30 = load i8*, i8** %11
%31 = call i64 @inet_pton(i32 %17, i8* %30, i32* %29)
%32 = icmp slt i64 %31, 1
br i1 %32, label %33, label %37
33: ; preds = %26
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %11
%36 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %35)
br label %47
37: ; preds = %26
%38 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%39 = call i64 @bind(i32 %19, %struct.sockaddr* %38, i32 8)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %49
41: ; preds = %37
%42 = load i64, i64* @errno
%43 = load i64, i64* @EADDRNOTAVAIL
%44 = icmp eq i64 %42, %43
br i1 %44, label %47, label %45
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %33, %45, %41
%48 = phi i32 [ 1, %41 ], [ -1, %45 ], [ -1, %33 ]
br label %75
49: ; preds = %37
br label %75
50: ; preds = %23
%51 = bitcast %struct.sockaddr_in6* %4 to i8*
%52 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i64 0, i32 0
%53 = load i32, i32* @AF_INET6
store i32 %53, i32* %52
%54 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i64 0, i32 1
store i32 0, i32* %54
%55 = load i8*, i8** %11
%56 = call i64 @inet_pton(i32 %17, i8* %55, i32* %54)
%57 = icmp slt i64 %56, 1
br i1 %57, label %58, label %62
58: ; preds = %50
%59 = load i32, i32* @stderr
%60 = load i8*, i8** %11
%61 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %60)
br label %72
62: ; preds = %50
%63 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
%64 = call i64 @bind(i32 %19, %struct.sockaddr* %63, i32 8)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %74
66: ; preds = %62
%67 = load i64, i64* @errno
%68 = load i64, i64* @EADDRNOTAVAIL
%69 = icmp eq i64 %67, %68
br i1 %69, label %72, label %70
70: ; preds = %66
%71 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %72
72: ; preds = %58, %70, %66
%73 = phi i32 [ 1, %66 ], [ -1, %70 ], [ -1, %58 ]
br label %75
74: ; preds = %62
br label %75
75: ; preds = %49, %74, %72, %47, %21, %6
%76 = phi i32 [ -1, %6 ], [ -1, %21 ], [ %48, %47 ], [ %73, %72 ], [ 0, %74 ], [ 0, %49 ]
ret i32 %76
}