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

/* Variables and functions */
scalar_t__ errno;
scalar_t__ fill_sockaddr(char *, struct sockaddr_in *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ print_errlog(char *, int, scalar_t__, char *, char *);
int /*<<< orphan*/ print_log(char *, ...);
char *strdup(char *);
char *strlen(char *);
int strtol(char *, int /*<<< orphan*/ *, int);
int parse_hostport(char *addr_str, struct sockaddr_in *addr, char **host_out) {
  char *host, *c, *dup;
  int port, err = -1;
  dup = strdup(addr_str);
  if (dup == NULL) {
    print_log("failed to allocate address buffer. len=%ld", strlen(addr_str));
    goto exit;
  }
  c = dup;
  host = c;
  while (*c != ':' && *c != '\0')
    c++;
  if (*c == '\0') {
    print_log("bad address string %s", addr_str);
    goto exit;
  }
  *c++ = '\0';
  errno = 0;
  port = strtol(c, NULL, 10);
  if (errno != 0) {
    do {
      print_errlog("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                   "outputs/2022-01-22/02-19-57/repos/jam2in/arcus-misc/refs/"
                   "heads/master/acp-c/common.c",
                   99, errno,
                   "error while parsing port number (strtol). address=%s",
                   addr_str);
    } while (0);
    goto exit;
  }
  if (port <= 0 || port >= 64 * 1024) {
    print_log("bad port %d in address string %s", port, addr_str);
    goto exit;
  }
  memset(addr, 0, sizeof(*addr));
  if (0 != fill_sockaddr(host, addr)) {
    print_log("cannot find the address for host %s", host);
    goto exit;
  }
  addr->sin_port = htons(port);
  err = 0;
exit:
  if (err == 0 && host_out != NULL)
    *host_out = host;
  else if (dup)
    free(dup);
  return err;
}


x86 assembly (clang, -O0):
<parse_hostport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e2 <parse_hostport+0x52>
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rsi
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1050 <print_log@plt>
jmp    135f <parse_hostport+0x1cf>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x3a,%ecx
mov    %al,-0x39(%rbp)
je     1217 <parse_hostport+0x87>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1227 <parse_hostport+0x97>
jmp    1238 <parse_hostport+0xa8>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11f2 <parse_hostport+0x62>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    125f <parse_hostport+0xcf>
mov    -0x8(%rbp),%rsi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1050 <print_log@plt>
jmp    135f <parse_hostport+0x1cf>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,(%rax)
mov    0x2d68(%rip),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   10a0 <strtol@plt>
mov    %eax,-0x34(%rbp)
mov    0x2d45(%rip),%rax
cmpq   $0x0,(%rax)
je     12d5 <parse_hostport+0x145>
jmp    12aa <parse_hostport+0x11a>
mov    0x2d2f(%rip),%rax
mov    (%rax),%rdx
mov    -0x8(%rbp),%r8
lea    0xd82(%rip),%rdi
mov    $0x63,%esi
lea    0xe03(%rip),%rcx
call   1090 <print_errlog@plt>
jmp    135f <parse_hostport+0x1cf>
cmpl   $0x0,-0x34(%rbp)
jle    12ec <parse_hostport+0x15c>
cmpl   $0x10000,-0x34(%rbp)
jl     1306 <parse_hostport+0x176>
mov    -0x34(%rbp),%esi
mov    -0x8(%rbp),%rdx
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1050 <print_log@plt>
jmp    135f <parse_hostport+0x1cf>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1080 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <fill_sockaddr@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1348 <parse_hostport+0x1b8>
mov    -0x20(%rbp),%rsi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1050 <print_log@plt>
jmp    135f <parse_hostport+0x1cf>
mov    -0x34(%rbp),%edi
call   1070 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jne    1384 <parse_hostport+0x1f4>
cmpq   $0x0,-0x18(%rbp)
je     1384 <parse_hostport+0x1f4>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    139d <parse_hostport+0x20d>
cmpq   $0x0,-0x30(%rbp)
je     1398 <parse_hostport+0x208>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    139d <parse_hostport+0x20d>
mov    -0x38(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_hostport>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   10b0 <strdup@plt>
test   %rax,%rax
je     1215 <parse_hostport+0x85>
mov    %rax,%r12
mov    %rax,%rdi
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rdi
movzbl -0x1(%rdi),%eax
test   %al,%al
je     1235 <parse_hostport+0xa5>
cmp    $0x3a,%al
jne    11c0 <parse_hostport+0x30>
movb   $0x0,-0x1(%rdi)
mov    0x2e05(%rip),%rbp
movq   $0x0,0x0(%rbp)
xor    %esi,%esi
mov    $0xa,%edx
call   10a0 <strtol@plt>
mov    0x0(%rbp),%rdx
test   %rdx,%rdx
je     1264 <parse_hostport+0xd4>
lea    0xe42(%rip),%rdi
lea    0xec8(%rip),%rcx
mov    $0x63,%esi
mov    %rbx,%r8
call   1090 <print_errlog@plt>
jmp    1246 <parse_hostport+0xb6>
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0xddc(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <print_log@plt>
mov    $0xffffffff,%ebx
jmp    1253 <parse_hostport+0xc3>
lea    0xdef(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <print_log@plt>
mov    $0xffffffff,%ebx
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r13d
add    $0xffff0000,%eax
cmp    $0xffff0000,%eax
ja     1289 <parse_hostport+0xf9>
lea    0xe89(%rip),%rdi
mov    %r13d,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <print_log@plt>
jmp    1246 <parse_hostport+0xb6>
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1080 <memset@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <fill_sockaddr@plt>
test   %rax,%rax
je     12b6 <parse_hostport+0x126>
lea    0xe73(%rip),%rdi
mov    %r12,%rsi
jmp    123f <parse_hostport+0xaf>
mov    %r13d,%edi
call   1070 <htons@plt>
mov    %eax,(%r15)
test   %r14,%r14
je     124b <parse_hostport+0xbb>
mov    %r12,(%r14)
xor    %ebx,%ebx
jmp    1253 <parse_hostport+0xc3>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
@.str = [43 x i8] c"failed to allocate address buffer. len=%ld\00"
@.str.1 = [22 x i8] c"bad address string %s\00"
@errno = global i64 0
@.str.2 = [141 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/jam2in/arcus-misc/refs/heads/master/acp-c/common.c\00"
@.str.3 = [53 x i8] c"error while parsing port number (strtol). address=%s\00"
@.str.4 = [33 x i8] c"bad port %d in address string %s\00"
@.str.5 = [36 x i8] c"cannot find the address for host %s\00"
define i32 @parse_hostport(i8* %0, %struct.sockaddr_in* %1, i8** %2) {
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in*
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
store i8** %2, i8*** %6
store i32 -1, i32* %11
%12 = load i8*, i8** %4
%13 = call i8* @strdup(i8* %12)
store i8* %13, i8** %9
%14 = load i8*, i8** %9
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i8*, i8** %4
%18 = call i8* @strlen(i8* %17)
%19 = call i32 (i8*, ...) @print_log(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %84
20: ; preds = %3
%21 = load i8*, i8** %9
store i8* %21, i8** %8
%22 = load i8*, i8** %8
store i8* %22, i8** %7
br label %23
23: ; preds = %35, %20
%24 = load i8*, i8** %8
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 58
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = load i8*, i8** %8
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 0
br label %33
33: ; preds = %28, %23
%34 = phi i1 [ false, %23 ], [ %32, %28 ]
br i1 %34, label %35, label %38
35: ; preds = %33
%36 = load i8*, i8** %8
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %8
br label %23
38: ; preds = %33
%39 = load i8*, i8** %8
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = load i8*, i8** %4
%45 = call i32 (i8*, ...) @print_log(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %44)
br label %84
46: ; preds = %38
%47 = load i8*, i8** %8
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %8
store i8 0, i8* %47
store i64 0, i64* @errno
%49 = load i8*, i8** %8
%50 = call i32 @strtol(i8* %49, i32* null, i32 10)
store i32 %50, i32* %10
%51 = load i64, i64* @errno
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %59
53: ; preds = %46
br label %54
54: ; preds = %53
%55 = load i64, i64* @errno
%56 = load i8*, i8** %4
%57 = call i32 @print_errlog(i8* getelementptr inbounds ([141 x i8], [141 x i8]* @.str.2, i64 0, i64 0), i32 99, i64 %55, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i8* %56)
br label %58
58: ; preds = %54
br label %84
59: ; preds = %46
%60 = load i32, i32* %10
%61 = icmp sle i32 %60, 0
br i1 %61, label %65, label %62
62: ; preds = %59
%63 = load i32, i32* %10
%64 = icmp sge i32 %63, 65536
br i1 %64, label %65, label %69
65: ; preds = %62, %59
%66 = load i32, i32* %10
%67 = load i8*, i8** %4
%68 = call i32 (i8*, ...) @print_log(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %66, i8* %67)
br label %84
69: ; preds = %62
%70 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%71 = call i32 @memset(%struct.sockaddr_in* %70, i32 0, i32 4)
%72 = load i8*, i8** %7
%73 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%74 = call i64 @fill_sockaddr(i8* %72, %struct.sockaddr_in* %73)
%75 = icmp ne i64 0, %74
br i1 %75, label %76, label %79
76: ; preds = %69
%77 = load i8*, i8** %7
%78 = call i32 (i8*, ...) @print_log(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %77)
br label %84
79: ; preds = %69
%80 = load i32, i32* %10
%81 = call i32 @htons(i32 %80)
%82 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%83 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %82, i32 0, i32 0
store i32 %81, i32* %83
store i32 0, i32* %11
br label %84
84: ; preds = %79, %76, %65, %58, %43, %16
%85 = load i32, i32* %11
%86 = icmp eq i32 %85, 0
br i1 %86, label %87, label %93
87: ; preds = %84
%88 = load i8**, i8*** %6
%89 = icmp ne i8** %88, null
br i1 %89, label %90, label %93
90: ; preds = %87
%91 = load i8*, i8** %7
%92 = load i8**, i8*** %6
store i8* %91, i8** %92
br label %100
93: ; preds = %87, %84
%94 = load i8*, i8** %9
%95 = icmp ne i8* %94, null
br i1 %95, label %96, label %99
96: ; preds = %93
%97 = load i8*, i8** %9
%98 = call i32 @free(i8* %97)
br label %99
99: ; preds = %96, %93
br label %100
100: ; preds = %99, %90
%101 = load i32, i32* %11
ret i32 %101
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
@.str = [43 x i8] c"failed to allocate address buffer. len=%ld\00"
@.str.1 = [22 x i8] c"bad address string %s\00"
@errno = global i64 0
@.str.2 = [141 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/jam2in/arcus-misc/refs/heads/master/acp-c/common.c\00"
@.str.3 = [53 x i8] c"error while parsing port number (strtol). address=%s\00"
@.str.4 = [33 x i8] c"bad port %d in address string %s\00"
@.str.5 = [36 x i8] c"cannot find the address for host %s\00"
define i32 @parse_hostport(i8* %0, %struct.sockaddr_in* %1, i8** writeonly %2) {
%4 = tail call i8* @strdup(i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i8* @strlen(i8* %0)
%8 = tail call i32 (i8*, ...) @print_log(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %42
9: ; preds = %3, %12
%10 = phi i8* [ %13, %12 ], [ %4, %3 ]
%11 = load i8, i8* %10
switch i8 %11, label %12 [
i8 0, label %14
i8 58, label %16
]
12: ; preds = %9
%13 = getelementptr inbounds i8, i8* %10, i64 1
br label %9
14: ; preds = %9
%15 = tail call i32 (i8*, ...) @print_log(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %39
16: ; preds = %9
%17 = getelementptr inbounds i8, i8* %10, i64 1
store i8 0, i8* %10
store i64 0, i64* @errno
%18 = tail call i32 @strtol(i8* nocapture %17, i32* null, i32 10)
%19 = load i64, i64* @errno
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = tail call i32 @print_errlog(i8* getelementptr inbounds ([141 x i8], [141 x i8]* @.str.2, i64 0, i64 0), i32 99, i64 %19, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %39
23: ; preds = %16
%24 = add i32 %18, -65536
%25 = icmp ult i32 %24, -65535
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = tail call i32 (i8*, ...) @print_log(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %18, i8* %0)
br label %39
28: ; preds = %23
%29 = tail call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 4)
%30 = tail call i64 @fill_sockaddr(i8* %4, %struct.sockaddr_in* %1)
%31 = icmp eq i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %28
%33 = tail call i32 (i8*, ...) @print_log(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %4)
br label %39
34: ; preds = %28
%35 = tail call i32 @htons(i32 %18)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %35, i32* %36
%37 = icmp eq i8** %2, null
br i1 %37, label %39, label %38
38: ; preds = %34
store i8* %4, i8** %2
br label %42
39: ; preds = %34, %32, %26, %21, %14
%40 = phi i32 [ 0, %34 ], [ -1, %32 ], [ -1, %26 ], [ -1, %21 ], [ -1, %14 ]
%41 = tail call i32 @free(i8* %4)
br label %42
42: ; preds = %6, %39, %38
%43 = phi i32 [ %40, %39 ], [ 0, %38 ], [ -1, %6 ]
ret i32 %43
}