#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*/ exit(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, ...);
int rand();
char *random_string(int, int);
char *strchr(char *, char);
int main(void) {
  int i, char_pos;
  for (i = 1; i < 50000; i += 2000) {
    char *sample_str;
    char sample_char_1;
    char sample_char_2 = '\0';
    char sample_char_3 = 'n';
    char_pos = rand() % i;
    sample_str = random_string(i, char_pos);
    sample_char_1 = sample_str[char_pos];
    char *ret_str_1 = strchr(sample_str, sample_char_1);
    char *ret_str_2 = strchr(sample_str, sample_char_2);
    char *ret_str_3 = strchr(sample_str, sample_char_3);
    if (ret_str_1 != &sample_str[char_pos]) {
      printf("strchr/1-1.c"
             " Test Failed, Failed to return pointer to the byte, when matching"
             " character is found. Expected: %p, but returned: %p\n",
             &sample_str[char_pos], ret_str_1);
      exit(1);
    } else if (ret_str_2 != &sample_str[i]) {
      printf("strchr/1-1.c"
             " Test Failed, Failed to consider NUL character as a part of the"
             " string,\n\t\t\t\t   so failed to return pointer to the NUL "
             "character,"
             " when matching character is found.\n\t\t\t\t   Expected: %p,"
             " but returned: %p\n",
             &sample_str[i - 1], ret_str_2);
      exit(1);
    } else if (ret_str_3 != NULL) {
      printf("strchr/1-1.c"
             " Test Failed, Failed to return NULL when character is not found"
             " in the string. Expected: NULL, but returned: %p\n",
             ret_str_3);
      exit(1);
    }
    free(sample_str);
  }
  printf("strchr/1-1.c"
         " Test Passed, First case: character is found, "
         "so returned pointer to the byte.\n\t\t\t  Second case: The"
         " terminating NUL character is considered as part of the string,"
         " \n\t\t\t\t\tso returned pointer to the NUL character.\n\t\t\t  "
         "Third case: character is not found, so"
         " NULL pointer is returned.\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
cmpl   $0xc350,-0x8(%rbp)
jge    12b5 <main+0x155>
movb   $0x0,-0x1a(%rbp)
movb   $0x6e,-0x1b(%rbp)
mov    $0x0,%al
call   1080 <rand@plt>
cltd
idivl  -0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <random_string@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x19(%rbp)
mov    -0x18(%rbp),%rdi
movsbl -0x19(%rbp),%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
movsbl -0x1a(%rbp),%esi
call   1050 <strchr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
movsbl -0x1b(%rbp),%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0xc(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
je     1228 <main+0xc8>
mov    -0x18(%rbp),%rsi
movslq -0xc(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%rdx
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0x8(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
je     126b <main+0x10b>
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
cltq
add    %rax,%rsi
mov    -0x30(%rbp),%rdx
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x38(%rbp)
je     1292 <main+0x132>
mov    -0x38(%rbp),%rsi
lea    0xed7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1297 <main+0x137>
jmp    129c <main+0x13c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%eax
add    $0x7d0,%eax
mov    %eax,-0x8(%rbp)
jmp    1176 <main+0x16>
lea    0xf19(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%r14d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   10a0 <rand@plt>
cltd
idiv   %r14d
mov    %edx,%ebx
mov    %r14d,%edi
mov    %edx,%esi
call   1050 <random_string@plt>
mov    %rax,%r12
movslq %ebx,%rax
lea    (%r12,%rax,1),%r15
movsbl (%r12,%rax,1),%esi
mov    %r12,%rdi
call   1070 <strchr@plt>
mov    %rax,%r13
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    $0x6e,%esi
call   1070 <strchr@plt>
cmp    %r15,%r13
jne    1238 <main+0xb8>
add    %r12,%rbx
lea    (%r12,%r14,1),%rcx
cmp    %rbx,%rcx
jne    1247 <main+0xc7>
test   %rax,%rax
jne    1268 <main+0xe8>
mov    %r12,%rdi
call   1030 <free@plt>
lea    0x7d0(%r14),%rax
add    $0xfffffffffffff830,%rbp
cmp    $0xbb80,%r14
mov    %rax,%r14
jb     11a0 <main+0x20>
lea    0xfa9(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xdc1(%rip),%rdi
mov    %r15,%rsi
mov    %r13,%rdx
jmp    1257 <main+0xd7>
sub    %rbp,%r12
lea    0xe31(%rip),%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xee9(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [130 x i8] c"strchr/1-1.c Test Failed, Failed to return pointer to the byte, when matching character is found. Expected: %p, but returned: %p\0A\00"
@.str.1 = [214 x i8] c"strchr/1-1.c Test Failed, Failed to consider NUL character as a part of the string,\0A\09\09\09\09 so failed to return pointer to the NUL character, when matching character is found.\0A\09\09\09\09 Expected: %p, but returned: %p\0A\00"
@.str.2 = [125 x i8] c"strchr/1-1.c Test Failed, Failed to return NULL when character is not found in the string. Expected: NULL, but returned: %p\0A\00"
@.str.3 = [295 x i8] c"strchr/1-1.c Test Passed, First case: character is found, so returned pointer to the byte.\0A\09\09\09 Second case: The terminating NUL character is considered as part of the string, \0A\09\09\09\09\09so returned pointer to the NUL character.\0A\09\09\09 Third case: character is not found, so NULL pointer is returned.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i32 0, i32* %1
store i32 1, i32* %2
br label %11
11: ; preds = %77, %0
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 50000
br i1 %13, label %14, label %80
14: ; preds = %11
store i8 0, i8* %6
store i8 110, i8* %7
%15 = call i32 (...) @rand()
%16 = load i32, i32* %2
%17 = srem i32 %15, %16
store i32 %17, i32* %3
%18 = load i32, i32* %2
%19 = load i32, i32* %3
%20 = call i8* @random_string(i32 %18, i32 %19)
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = load i32, i32* %3
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
store i8 %25, i8* %5
%26 = load i8*, i8** %4
%27 = load i8, i8* %5
%28 = call i8* @strchr(i8* %26, i8 signext %27)
store i8* %28, i8** %8
%29 = load i8*, i8** %4
%30 = load i8, i8* %6
%31 = call i8* @strchr(i8* %29, i8 signext %30)
store i8* %31, i8** %9
%32 = load i8*, i8** %4
%33 = load i8, i8* %7
%34 = call i8* @strchr(i8* %32, i8 signext %33)
store i8* %34, i8** %10
%35 = load i8*, i8** %8
%36 = load i8*, i8** %4
%37 = load i32, i32* %3
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = icmp ne i8* %35, %39
br i1 %40, label %41, label %49
41: ; preds = %14
%42 = load i8*, i8** %4
%43 = load i32, i32* %3
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8*, i8** %8
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([130 x i8], [130 x i8]* @.str, i64 0, i64 0), i8* %45, i8* %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %14
%50 = load i8*, i8** %9
%51 = load i8*, i8** %4
%52 = load i32, i32* %2
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
%55 = icmp ne i8* %50, %54
br i1 %55, label %56, label %65
56: ; preds = %49
%57 = load i8*, i8** %4
%58 = load i32, i32* %2
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %57, i64 %60
%62 = load i8*, i8** %9
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([214 x i8], [214 x i8]* @.str.1, i64 0, i64 0), i8* %61, i8* %62)
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %49
%66 = load i8*, i8** %10
%67 = icmp ne i8* %66, null
br i1 %67, label %68, label %72
68: ; preds = %65
%69 = load i8*, i8** %10
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([125 x i8], [125 x i8]* @.str.2, i64 0, i64 0), i8* %69)
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %65
br label %73
73: ; preds = %72
br label %74
74: ; preds = %73
%75 = load i8*, i8** %4
%76 = call i32 @free(i8* %75)
br label %77
77: ; preds = %74
%78 = load i32, i32* %2
%79 = add nsw i32 %78, 2000
store i32 %79, i32* %2
br label %11
80: ; preds = %11
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([295 x i8], [295 x i8]* @.str.3, i64 0, i64 0))
%82 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [130 x i8] c"strchr/1-1.c Test Failed, Failed to return pointer to the byte, when matching character is found. Expected: %p, but returned: %p\0A\00"
@.str.1 = [214 x i8] c"strchr/1-1.c Test Failed, Failed to consider NUL character as a part of the string,\0A\09\09\09\09 so failed to return pointer to the NUL character, when matching character is found.\0A\09\09\09\09 Expected: %p, but returned: %p\0A\00"
@.str.2 = [125 x i8] c"strchr/1-1.c Test Failed, Failed to return NULL when character is not found in the string. Expected: NULL, but returned: %p\0A\00"
@str = [294 x i8] c"strchr/1-1.c Test Passed, First case: character is found, so returned pointer to the byte.\0A\09\09\09 Second case: The terminating NUL character is considered as part of the string, \0A\09\09\09\09\09so returned pointer to the NUL character.\0A\09\09\09 Third case: character is not found, so NULL pointer is returned.\00"
define i32 @main() {
br label %1
1: ; preds = %0, %32
%2 = phi i64 [ 1, %0 ], [ %34, %32 ]
%3 = tail call i32 (...) @rand()
%4 = trunc i64 %2 to i32
%5 = srem i32 %3, %4
%6 = tail call i8* @random_string(i32 %4, i32 %5)
%7 = sext i32 %5 to i64
%8 = getelementptr inbounds i8, i8* %6, i64 %7
%9 = load i8, i8* %8
%10 = tail call i8* @strchr(i8* dereferenceable(1) %6, i8 signext %9)
%11 = tail call i64 @strlen(i8* dereferenceable(1) %6)
%12 = getelementptr i8, i8* %6, i64 %11
%13 = tail call i8* @strchr(i8* dereferenceable(1) %6, i8 signext 110)
%14 = icmp eq i8* %10, %8
br i1 %14, label %18, label %15
15: ; preds = %1
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([130 x i8], [130 x i8]* @.str, i64 0, i64 0), i8* %8, i8* %10)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %1
%19 = getelementptr inbounds i8, i8* %6, i64 %2
%20 = icmp eq i8* %12, %19
br i1 %20, label %27, label %21
21: ; preds = %18
%22 = add nuw i64 %2, 4294967295
%23 = and i64 %22, 4294967295
%24 = getelementptr inbounds i8, i8* %6, i64 %23
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([214 x i8], [214 x i8]* @.str.1, i64 0, i64 0), i8* %24, i8* %12)
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = icmp eq i8* %13, null
br i1 %28, label %32, label %29
29: ; preds = %27
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([125 x i8], [125 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %27
%33 = tail call i32 @free(i8* %6)
%34 = add nuw nsw i64 %2, 2000
%35 = icmp ult i64 %2, 48000
br i1 %35, label %1, label %36
36: ; preds = %32
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([294 x i8], [294 x i8]* @str, i64 0, i64 0))
%38 = tail call i32 @exit(i32 0)
unreachable
}