#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 */
char *malloc(int);
char *send_buffer(char *, char const *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
char *get_country(const char *ip) {
  if (!ip)
    return NULL;
  char *res = send_buffer("getinfo ip-to-country/", ip);
  char *st = strrchr(res, '=');
  if (!st++)
    return NULL;
  char *country = malloc(3);
  strncpy(country, st, 2);
  country[2] = '\0';
  return country;
}


x86 assembly (clang, -O0):
<get_country>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1164 <get_country+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <get_country+0xa0>
mov    -0x10(%rbp),%rsi
lea    0xe91(%rip),%rdi
call   1040 <send_buffer@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x3d,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
cmp    $0x0,%rax
jne    11b0 <get_country+0x70>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <get_country+0xa0>
mov    $0x3,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x2,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
movb   $0x0,0x2(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_country>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1194 <get_country+0x54>
mov    %rdi,%rsi
lea    0xead(%rip),%rdi
call   1040 <send_buffer@plt>
mov    %rax,%rdi
mov    $0x3d,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1194 <get_country+0x54>
mov    %rax,%r14
add    $0x1,%r14
mov    $0x3,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0x2,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x2(%rbx)
jmp    1196 <get_country+0x56>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [23 x i8] c"getinfo ip-to-country/\00"
define i8* @get_country(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store i8* null, i8** %2
br label %27
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i8* @send_buffer(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %11)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = call i8* @strrchr(i8* %13, i8 signext 61)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %5
%17 = icmp ne i8* %15, null
br i1 %17, label %19, label %18
18: ; preds = %10
store i8* null, i8** %2
br label %27
19: ; preds = %10
%20 = call i8* @malloc(i32 3)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = load i8*, i8** %5
%23 = call i32 @strncpy(i8* %21, i8* %22, i32 2)
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i64 2
store i8 0, i8* %25
%26 = load i8*, i8** %6
store i8* %26, i8** %2
br label %27
27: ; preds = %19, %18, %9
%28 = load i8*, i8** %2
ret i8* %28
}

LLVM IR (-O2):
@.str = [23 x i8] c"getinfo ip-to-country/\00"
define i8* @get_country(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %12, label %3
3: ; preds = %1
%4 = tail call i8* @send_buffer(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = tail call i8* @strrchr(i8* dereferenceable(1) %4, i8 signext 61)
%6 = icmp eq i8* %5, null
br i1 %6, label %12, label %7
7: ; preds = %3
%8 = getelementptr inbounds i8, i8* %5, i64 1
%9 = tail call dereferenceable_or_null(3) i8* @malloc(i32 3)
%10 = tail call i32 @strncpy(i8* %9, i8* %8, i32 2)
%11 = getelementptr inbounds i8, i8* %9, i64 2
store i8 0, i8* %11
br label %12
12: ; preds = %7, %3, %1
%13 = phi i8* [ null, %1 ], [ %9, %7 ], [ null, %3 ]
ret i8* %13
}