#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 */
typedef int ptrdiff_t;

/* Variables and functions */
char *char_replace(char *, char, char);
int /*<<< orphan*/ memmove(char *, char *, int);
char *strstr(char *, char *);
char *parse_ios(char *agent, int tlen) {
  char *p = NULL, *q = NULL;
  ptrdiff_t offset;
  if ((p = strstr(agent, " OS ")) == NULL)
    goto out;
  if ((offset = p - agent) <= 0)
    goto out;
  if ((q = strstr(p, " like Mac")) == NULL)
    goto out;
  *q = 0;
  memmove(agent + tlen, agent + offset, offset);
  return char_replace(agent, '_', '.');
out:
  agent[tlen] = 0;
  return agent;
}


x86 assembly (clang, -O0):
<parse_ios>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1172 <parse_ios+0x42>
jmp    11f2 <parse_ios+0xc2>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jg     118e <parse_ios+0x5e>
jmp    11f2 <parse_ios+0xc2>
mov    -0x20(%rbp),%rdi
lea    0xe6c(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11b1 <parse_ios+0x81>
jmp    11f2 <parse_ios+0xc2>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
mov    -0x2c(%rbp),%edx
call   1040 <memmove@plt>
mov    -0x10(%rbp),%rdi
mov    $0x5f,%esi
mov    $0x2e,%edx
call   1030 <char_replace@plt>
mov    %rax,-0x8(%rbp)
jmp    1206 <parse_ios+0xd6>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_ios>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xebf(%rip),%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     1195 <parse_ios+0x65>
mov    %rax,%rbp
sub    %rbx,%rbp
test   %ebp,%ebp
jle    1195 <parse_ios+0x65>
lea    0xea9(%rip),%rsi
mov    %rax,%rdi
call   1050 <strstr@plt>
test   %rax,%rax
je     1195 <parse_ios+0x65>
movb   $0x0,(%rax)
movslq %r14d,%rdi
add    %rbx,%rdi
movslq %ebp,%rsi
add    %rbx,%rsi
mov    %ebp,%edx
call   1040 <memmove@plt>
mov    %rbx,%rdi
mov    $0x5f,%esi
mov    $0x2e,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <char_replace@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c" OS \00"
@.str.1 = [10 x i8] c" like Mac\00"
define i8* @parse_ios(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* null, i8** %6
store i8* null, i8** %7
%9 = load i8*, i8** %4
%10 = call i8* @strstr(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
br label %41
13: ; preds = %2
%14 = load i8*, i8** %6
%15 = load i8*, i8** %4
%16 = ptrtoint i8* %14 to i64
%17 = ptrtoint i8* %15 to i64
%18 = sub i64 %16, %17
%19 = trunc i64 %18 to i32
store i32 %19, i32* %8
%20 = icmp sle i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %13
br label %41
22: ; preds = %13
%23 = load i8*, i8** %6
%24 = call i8* @strstr(i8* %23, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i8* %24, i8** %7
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %27
26: ; preds = %22
br label %41
27: ; preds = %22
%28 = load i8*, i8** %7
store i8 0, i8* %28
%29 = load i8*, i8** %4
%30 = load i32, i32* %5
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8*, i8** %4
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i32, i32* %8
%38 = call i32 @memmove(i8* %32, i8* %36, i32 %37)
%39 = load i8*, i8** %4
%40 = call i8* @char_replace(i8* %39, i8 signext 95, i8 signext 46)
store i8* %40, i8** %3
br label %47
41: ; preds = %26, %21, %12
%42 = load i8*, i8** %4
%43 = load i32, i32* %5
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** %4
store i8* %46, i8** %3
br label %47
47: ; preds = %41, %27
%48 = load i8*, i8** %3
ret i8* %48
}

LLVM IR (-O2):
@.str = [5 x i8] c" OS \00"
@.str.1 = [10 x i8] c" like Mac\00"
define i8* @parse_ios(i8* %0, i32 %1) {
%3 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %22, label %5
5: ; preds = %2
%6 = ptrtoint i8* %3 to i64
%7 = ptrtoint i8* %0 to i64
%8 = sub i64 %6, %7
%9 = trunc i64 %8 to i32
%10 = icmp slt i32 %9, 1
br i1 %10, label %22, label %11
11: ; preds = %5
%12 = tail call i8* @strstr(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %22, label %14
14: ; preds = %11
store i8 0, i8* %12
%15 = sext i32 %1 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = shl i64 %8, 32
%18 = ashr exact i64 %17, 32
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = tail call i32 @memmove(i8* %16, i8* %19, i32 %9)
%21 = tail call i8* @char_replace(i8* %0, i8 signext 95, i8 signext 46)
br label %25
22: ; preds = %11, %5, %2
%23 = sext i32 %1 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %22, %14
%26 = phi i8* [ %0, %22 ], [ %21, %14 ]
ret i8* %26
}