#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 /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
char *strstr(char *, char *);
int main() {
  char str[] = "This is a sample string.";
  char look[30 + 1];
  char *pos;
  pos = strstr(str, "sample");
  printf("'sample' found at position %d.\n\n", pos - str);
  printf("Specify string to look for (no spaces): ");
  scanf("%s", look);
  pos = strstr(str, look);
  if (pos == NULL)
    printf("'%s' could not be found.\n\n", look);
  else
    printf("'%s' was found at position %d.\n\n", look, pos - str);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xeba(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xeb7(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xeb4(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xeb2(%rip),%al
mov    %al,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rsi
lea    -0x20(%rbp),%rax
sub    %rax,%rsi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x40(%rbp),%rsi
lea    0xebb(%rip),%rdi
call   1040 <scanf@plt>
lea    -0x20(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11e7 <main+0xb7>
lea    -0x40(%rbp),%rsi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1204 <main+0xd4>
lea    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
lea    -0x20(%rbp),%rax
sub    %rax,%rdx
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x48,%rsp
movups 0xecb(%rip),%xmm0
movups %xmm0,0x9(%rsp)
movaps 0xeb6(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xec4(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strstr@plt>
sub    %r14,%rax
lea    0xeb6(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xec6(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xee1(%rip),%rdi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
call   1040 <scanf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     11c1 <main+0x91>
sub    %r14,%rax
lea    0xed8(%rip),%rdi
lea    0x20(%rsp),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11d4 <main+0xa4>
lea    0xea5(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.str = [25 x i8] c"This is a sample string.\00"
@.str = [7 x i8] c"sample\00"
@.str.1 = [33 x i8] c"'sample' found at position %d.\0A\0A\00"
@.str.2 = [41 x i8] c"Specify string to look for (no spaces): \00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [27 x i8] c"'%s' could not be found.\0A\0A\00"
@.str.5 = [33 x i8] c"'%s' was found at position %d.\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [25 x i8]
%3 = alloca [31 x i8]
%4 = alloca i8*
store i32 0, i32* %1
%5 = bitcast [25 x i8]* %2 to i8*
%6 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%7 = call i8* @strstr(i8* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%10 = ptrtoint i8* %8 to i64
%11 = ptrtoint i8* %9 to i64
%12 = sub i64 %10, %11
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i64 %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%15 = getelementptr inbounds [31 x i8], [31 x i8]* %3, i64 0, i64 0
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %15)
%17 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%18 = getelementptr inbounds [31 x i8], [31 x i8]* %3, i64 0, i64 0
%19 = call i8* @strstr(i8* %17, i8* %18)
store i8* %19, i8** %4
%20 = load i8*, i8** %4
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %0
%23 = getelementptr inbounds [31 x i8], [31 x i8]* %3, i64 0, i64 0
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %23)
br label %33
25: ; preds = %0
%26 = getelementptr inbounds [31 x i8], [31 x i8]* %3, i64 0, i64 0
%27 = load i8*, i8** %4
%28 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%29 = ptrtoint i8* %27 to i64
%30 = ptrtoint i8* %28 to i64
%31 = sub i64 %29, %30
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %26, i64 %31)
br label %33
33: ; preds = %25, %22
ret i32 0
}

LLVM IR (-O2):
@__const.main.str = [25 x i8] c"This is a sample string.\00"
@.str = [7 x i8] c"sample\00"
@.str.1 = [33 x i8] c"'sample' found at position %d.\0A\0A\00"
@.str.2 = [41 x i8] c"Specify string to look for (no spaces): \00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [27 x i8] c"'%s' could not be found.\0A\0A\00"
@.str.5 = [33 x i8] c"'%s' was found at position %d.\0A\0A\00"
define i32 @main() {
%1 = alloca [25 x i8]
%2 = alloca [31 x i8]
%3 = getelementptr inbounds [25 x i8], [25 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [31 x i8], [31 x i8]* %2, i64 0, i64 0
%5 = call i8* @strstr(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%6 = ptrtoint i8* %5 to i64
%7 = ptrtoint [25 x i8]* %1 to i64
%8 = sub i64 %6, %7
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i64 %8)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%12 = call i8* @strstr(i8* %3, i8* %4)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %4)
br label %20
16: ; preds = %0
%17 = ptrtoint i8* %12 to i64
%18 = sub i64 %17, %7
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %4, i64 %18)
br label %20
20: ; preds = %16, %14
ret i32 0
}