#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 /*<<< orphan*/ cmd_line_buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
unsigned int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char const *);
char *strstr(char *, char const *);
unsigned int strtoul(char *, char **, int);
int main(int argc, char **argv) {
  const char *cmd_line =
      "root=ramfs rw sysboot imgdev=/dev/mtd10 console=ttyS0,9600 "
      "post=0x00011800 card_index=0x2b03 card_rev=0x0002 ubootver=1.12 "
      "sysmgr_cmdline=-RSto autoboot lock_bootflash ubootlog=0xfffcf5c";
  char cmd_line_buf[300];
  const char *card_str = "card_index=";
  char *p_str, *p_space;
  unsigned int card_index, read_size;
  FILE *fp;
  fp = fopen("./tmp", "r");
  if (fp == NULL) {
    printf("Could not open the file for read\n");
    exit(1);
  }
  read_size = fread(cmd_line_buf, 1, sizeof(cmd_line_buf), fp);
  printf("Read %d bytes from the file, Buf max %d\n", read_size,
         sizeof(cmd_line_buf));
  p_str = strstr(cmd_line_buf, card_str);
  if (p_str) {
    printf("Found Card string: %s, of length %d\n", p_str, strlen(p_str));
    p_str += strlen(card_str);
    card_index = strtoul(p_str, &p_space, 16);
    printf("Card Index: %s, of value 0x%x\n", p_str, card_index);
  } else {
    printf("No Card string found:\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe70(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xf20(%rip),%rax
mov    %rax,-0x158(%rbp)
lea    0xf1e(%rip),%rdi
lea    0xf1d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x178(%rbp)
cmpq   $0x0,-0x178(%rbp)
jne    11e2 <main+0x72>
lea    0xefe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x150(%rbp),%rdi
mov    -0x178(%rbp),%rcx
mov    $0x1,%esi
mov    $0x12c,%edx
call   1030 <fread@plt>
mov    %eax,-0x170(%rbp)
mov    -0x170(%rbp),%esi
lea    0xedf(%rip),%rdi
mov    $0x12c,%edx
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x150(%rbp),%rdi
mov    -0x158(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x160(%rbp)
cmpq   $0x0,-0x160(%rbp)
je     12d7 <main+0x167>
mov    -0x160(%rbp),%rax
mov    %rax,-0x180(%rbp)
mov    -0x160(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x180(%rbp),%rsi
mov    %eax,%edx
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x158(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x160(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x160(%rbp)
mov    -0x160(%rbp),%rdi
lea    -0x168(%rbp),%rsi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %eax,-0x16c(%rbp)
mov    -0x160(%rbp),%rsi
mov    -0x16c(%rbp),%edx
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12e5 <main+0x175>
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x148,%rsp
lea    0xe7b(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1259 <main+0xd9>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x12c,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
lea    0xe4d(%rip),%rdi
mov    $0x12c,%edx
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe24(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strstr@plt>
test   %rax,%rax
je     1240 <main+0xc0>
mov    %rax,%r14
mov    %rax,%rdi
call   1050 <strlen@plt>
lea    0xe42(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdf2(%rip),%rdi
call   1050 <strlen@plt>
movslq %eax,%rbx
add    %r14,%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x10,%edx
call   1080 <strtoul@plt>
lea    0xe30(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    124c <main+0xcc>
lea    0xe3a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x148,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe37(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [187 x i8] c"root=ramfs rw sysboot imgdev=/dev/mtd10 console=ttyS0,9600 post=0x00011800 card_index=0x2b03 card_rev=0x0002 ubootver=1.12 sysmgr_cmdline=-RSto autoboot lock_bootflash ubootlog=0xfffcf5c\00"
@.str.1 = [12 x i8] c"card_index=\00"
@.str.2 = [6 x i8] c"./tmp\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [34 x i8] c"Could not open the file for read\0A\00"
@.str.5 = [41 x i8] c"Read %d bytes from the file, Buf max %d\0A\00"
@.str.6 = [37 x i8] c"Found Card string: %s, of length %d\0A\00"
@.str.7 = [31 x i8] c"Card Index: %s, of value 0x%x\0A\00"
@.str.8 = [23 x i8] c"No Card string found:\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [300 x i8]
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([187 x i8], [187 x i8]* @.str, i64 0, i64 0), i8** %6
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8** %8
%14 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %14, i32** %13
%15 = load i32*, i32** %13
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = getelementptr inbounds [300 x i8], [300 x i8]* %7, i64 0, i64 0
%22 = load i32*, i32** %13
%23 = call i32 @fread(i8* %21, i32 1, i32 300, i32* %22)
store i32 %23, i32* %12
%24 = load i32, i32* %12
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %24, i64 300)
%26 = getelementptr inbounds [300 x i8], [300 x i8]* %7, i64 0, i64 0
%27 = load i8*, i8** %8
%28 = call i8* @strstr(i8* %26, i8* %27)
store i8* %28, i8** %9
%29 = load i8*, i8** %9
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %46
31: ; preds = %20
%32 = load i8*, i8** %9
%33 = load i8*, i8** %9
%34 = call i32 @strlen(i8* %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i8* %32, i32 %34)
%36 = load i8*, i8** %8
%37 = call i32 @strlen(i8* %36)
%38 = load i8*, i8** %9
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8* %40, i8** %9
%41 = load i8*, i8** %9
%42 = call i32 @strtoul(i8* %41, i8** %10, i32 16)
store i32 %42, i32* %11
%43 = load i8*, i8** %9
%44 = load i32, i32* %11
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i8* %43, i32 %44)
br label %48
46: ; preds = %20
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0))
br label %48
48: ; preds = %46, %31
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"card_index=\00"
@.str.2 = [6 x i8] c"./tmp\00"
@.str.3 = [2 x i8] c"r\00"
@.str.5 = [41 x i8] c"Read %d bytes from the file, Buf max %d\0A\00"
@.str.6 = [37 x i8] c"Found Card string: %s, of length %d\0A\00"
@.str.7 = [31 x i8] c"Card Index: %s, of value 0x%x\0A\00"
@str = [22 x i8] c"No Card string found:\00"
@str.9 = [33 x i8] c"Could not open the file for read\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [300 x i8]
%4 = alloca i8*
%5 = getelementptr inbounds [300 x i8], [300 x i8]* %3, i64 0, i64 0
%6 = bitcast i8** %4 to i8*
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.9, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = call i32 @fread(i8* %5, i32 1, i32 300, i32* %7)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %13, i64 300)
%15 = call i8* @strstr(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %25, label %17
17: ; preds = %12
%18 = call i32 @strlen(i8* %15)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i8* %15, i32 %18)
%20 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %15, i64 %21
%23 = call i32 @strtoul(i8* %22, i8** %4, i32 16)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0), i8* %22, i32 %23)
br label %27
25: ; preds = %12
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %27
27: ; preds = %25, %17
ret i32 0
}