#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 */

/* Variables and functions */
int ccode_of_iso3(char const *, int);
int /*<<< orphan*/ convertTerritoryCodeToIsoName(int, int);
int getCountryOrParentCountry(int);
int /*<<< orphan*/ strcat(char *, char const *);
scalar_t__ strchr(char const *, char);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int strlen(char const *);
int convertTerritoryIsoNameToCode(const char *string, int optional_tc) {
  if (string == NULL)
    return -1;
  while (*string > 0 && *string <= 32) {
    string++;
  }
  int ccode = optional_tc - 1;
  if (ccode < 0 || strchr(string, '-') || strlen(string) > 3) {
    ccode = ccode_of_iso3(string, -1);
  } else {
    char tmp[12];
    int tc = getCountryOrParentCountry(optional_tc);
    strcpy(tmp, convertTerritoryCodeToIsoName(tc, 1));
    strcat(tmp, "-");
    strcat(tmp, string);
    ccode = ccode_of_iso3(tmp, -1);
  }
  if (ccode < 0)
    return -1;
  else
    return ccode + 1;
}


x86 assembly (clang, -O0):
<convertTerritoryIsoNameToCode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1196 <convertTerritoryIsoNameToCode+0x26>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <convertTerritoryIsoNameToCode+0x13c>
jmp    119b <convertTerritoryIsoNameToCode+0x2b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
jle    11c0 <convertTerritoryIsoNameToCode+0x50>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
setle  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11d0 <convertTerritoryIsoNameToCode+0x60>
jmp    11e1 <convertTerritoryIsoNameToCode+0x71>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    119b <convertTerritoryIsoNameToCode+0x2b>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     121e <convertTerritoryIsoNameToCode+0xae>
mov    -0x10(%rbp),%rdi
mov    $0x2d,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
jne    121e <convertTerritoryIsoNameToCode+0xae>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x3,%eax
jle    1234 <convertTerritoryIsoNameToCode+0xc4>
mov    -0x10(%rbp),%rdi
mov    $0xffffffff,%esi
call   1070 <ccode_of_iso3@plt>
mov    %eax,-0x18(%rbp)
jmp    128d <convertTerritoryIsoNameToCode+0x11d>
mov    -0x14(%rbp),%edi
call   1040 <getCountryOrParentCountry@plt>
mov    %eax,-0x28(%rbp)
lea    -0x24(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
mov    $0x1,%esi
call   1080 <convertTerritoryCodeToIsoName@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
lea    -0x24(%rbp),%rdi
lea    0xd96(%rip),%rsi
call   1090 <strcat@plt>
lea    -0x24(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcat@plt>
lea    -0x24(%rbp),%rdi
mov    $0xffffffff,%esi
call   1070 <ccode_of_iso3@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12a3 <convertTerritoryIsoNameToCode+0x133>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ac <convertTerritoryIsoNameToCode+0x13c>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convertTerritoryIsoNameToCode>:
test   %rdi,%rdi
je     11e2 <convertTerritoryIsoNameToCode+0x72>
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl 0x1(%rbx),%eax
add    $0x1,%rbx
add    $0xff,%al
cmp    $0x20,%al
jb     1190 <convertTerritoryIsoNameToCode+0x20>
test   %ebp,%ebp
jle    11c1 <convertTerritoryIsoNameToCode+0x51>
mov    %rbx,%rdi
mov    $0x2d,%esi
call   1060 <strchr@plt>
test   %rax,%rax
jne    11c1 <convertTerritoryIsoNameToCode+0x51>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    $0x4,%eax
jl     11e8 <convertTerritoryIsoNameToCode+0x78>
mov    %rbx,%rdi
mov    $0xffffffff,%esi
call   1070 <ccode_of_iso3@plt>
lea    0x1(%rax),%ecx
test   %eax,%eax
mov    $0xffffffff,%eax
cmovns %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0xffffffff,%eax
ret
mov    %ebp,%edi
call   1040 <getCountryOrParentCountry@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1080 <convertTerritoryCodeToIsoName@plt>
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
lea    0xdef(%rip),%rsi
mov    %rbp,%rdi
call   1090 <strcat@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
mov    %rbp,%rdi
jmp    11c4 <convertTerritoryIsoNameToCode+0x54>

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
define i32 @convertTerritoryIsoNameToCode(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [12 x i8]
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %65
12: ; preds = %2
br label %13
13: ; preds = %25, %12
%14 = load i8*, i8** %4
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp sle i32 %21, 32
br label %23
23: ; preds = %18, %13
%24 = phi i1 [ false, %13 ], [ %22, %18 ]
br i1 %24, label %25, label %28
25: ; preds = %23
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %4
br label %13
28: ; preds = %23
%29 = load i32, i32* %5
%30 = sub nsw i32 %29, 1
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %41, label %33
33: ; preds = %28
%34 = load i8*, i8** %4
%35 = call i64 @strchr(i8* %34, i8 signext 45)
%36 = icmp ne i64 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %33
%38 = load i8*, i8** %4
%39 = call i32 @strlen(i8* %38)
%40 = icmp sgt i32 %39, 3
br i1 %40, label %41, label %44
41: ; preds = %37, %33, %28
%42 = load i8*, i8** %4
%43 = call i32 @ccode_of_iso3(i8* %42, i32 -1)
store i32 %43, i32* %6
br label %58
44: ; preds = %37
%45 = load i32, i32* %5
%46 = call i32 @getCountryOrParentCountry(i32 %45)
store i32 %46, i32* %8
%47 = getelementptr inbounds [12 x i8], [12 x i8]* %7, i64 0, i64 0
%48 = load i32, i32* %8
%49 = call i32 @convertTerritoryCodeToIsoName(i32 %48, i32 1)
%50 = call i32 @strcpy(i8* %47, i32 %49)
%51 = getelementptr inbounds [12 x i8], [12 x i8]* %7, i64 0, i64 0
%52 = call i32 @strcat(i8* %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%53 = getelementptr inbounds [12 x i8], [12 x i8]* %7, i64 0, i64 0
%54 = load i8*, i8** %4
%55 = call i32 @strcat(i8* %53, i8* %54)
%56 = getelementptr inbounds [12 x i8], [12 x i8]* %7, i64 0, i64 0
%57 = call i32 @ccode_of_iso3(i8* %56, i32 -1)
store i32 %57, i32* %6
br label %58
58: ; preds = %44, %41
%59 = load i32, i32* %6
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %58
store i32 -1, i32* %3
br label %65
62: ; preds = %58
%63 = load i32, i32* %6
%64 = add nsw i32 %63, 1
store i32 %64, i32* %3
br label %65
65: ; preds = %62, %61, %11
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
define i32 @convertTerritoryIsoNameToCode(i8* %0, i32 %1) {
%3 = alloca [12 x i8]
%4 = icmp eq i8* %0, null
br i1 %4, label %34, label %5
5: ; preds = %2, %5
%6 = phi i8* [ %10, %5 ], [ %0, %2 ]
%7 = load i8, i8* %6
%8 = add i8 %7, -1
%9 = icmp ult i8 %8, 32
%10 = getelementptr inbounds i8, i8* %6, i64 1
br i1 %9, label %5, label %11
11: ; preds = %5
%12 = icmp slt i32 %1, 1
br i1 %12, label %19, label %13
13: ; preds = %11
%14 = tail call i64 @strchr(i8* %6, i8 signext 45)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = tail call i32 @strlen(i8* %6)
%18 = icmp sgt i32 %17, 3
br i1 %18, label %19, label %21
19: ; preds = %16, %13, %11
%20 = tail call i32 @ccode_of_iso3(i8* %6, i32 -1)
br label %29
21: ; preds = %16
%22 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%23 = tail call i32 @getCountryOrParentCountry(i32 %1)
%24 = tail call i32 @convertTerritoryCodeToIsoName(i32 %23, i32 1)
%25 = call i32 @strcpy(i8* %22, i32 %24)
%26 = call i32 @strcat(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @strcat(i8* %22, i8* %6)
%28 = call i32 @ccode_of_iso3(i8* %22, i32 -1)
br label %29
29: ; preds = %21, %19
%30 = phi i32 [ %20, %19 ], [ %28, %21 ]
%31 = add nsw i32 %30, 1
%32 = icmp sgt i32 %30, -1
%33 = select i1 %32, i32 %31, i32 -1
br label %34
34: ; preds = %2, %29
%35 = phi i32 [ %33, %29 ], [ -1, %2 ]
ret i32 %35
}