#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 atoi(char *);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
int strcspn(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
int parse_int(char *fitsheader, int *value, const char *key) {
  int i, length;
  char *temp;
  char empty[] = " ";
  char char_value[80];
  int key_length = 8;
  char mod_key[8 + 2];
  strcpy(mod_key, key);
  length = strlen(mod_key);
  for (i = length; i < key_length; i++)
    strcat(mod_key, " ");
  strcat(mod_key, "=");
  temp = strstr(fitsheader, mod_key);
  if (temp == NULL)
    return 1;
  temp = strchr(temp, '=');
  if (temp == NULL)
    return 1;
  while (*(++temp) == ' ')
    ;
  length = strcspn(temp, empty);
  if (length >= 80)
    return 1;
  strncpy(char_value, temp, length);
  char_value[length] = '\0';
  *value = atoi(char_value);
  return 0;
}


x86 assembly (clang, -O0):
<parse_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0xe62(%rip),%ax
mov    %ax,-0x32(%rbp)
movl   $0x8,-0x94(%rbp)
lea    -0x9e(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x9e(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x94(%rbp),%eax
jge    1201 <parse_int+0x81>
lea    -0x9e(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11d1 <parse_int+0x51>
lea    -0x9e(%rbp),%rdi
lea    0xdf3(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    -0x9e(%rbp),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    123f <parse_int+0xbf>
movl   $0x1,-0x4(%rbp)
jmp    12ee <parse_int+0x16e>
mov    -0x30(%rbp),%rdi
mov    $0x3d,%esi
call   1060 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1268 <parse_int+0xe8>
movl   $0x1,-0x4(%rbp)
jmp    12ee <parse_int+0x16e>
jmp    126d <parse_int+0xed>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movsbl 0x1(%rax),%eax
cmp    $0x20,%eax
jne    128e <parse_int+0x10e>
jmp    126d <parse_int+0xed>
mov    -0x30(%rbp),%rdi
lea    -0x32(%rbp),%rsi
call   1070 <strcspn@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x50,-0x28(%rbp)
jl     12b4 <parse_int+0x134>
movl   $0x1,-0x4(%rbp)
jmp    12ee <parse_int+0x16e>
lea    -0x90(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <strncpy@plt>
movslq -0x28(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
lea    -0x90(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_int>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x60,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x6(%rsp),%rbx
mov    %rbx,%rdi
mov    %rdx,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    $0x7,%eax
jg     11cf <parse_int+0x4f>
mov    %eax,%ebx
add    $0xfffffff8,%ebx
lea    0xe45(%rip),%r12
lea    0x6(%rsp),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   1090 <strcat@plt>
inc    %ebx
jne    11c0 <parse_int+0x40>
lea    0xe2c(%rip),%rsi
lea    0x6(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10a0 <strstr@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     1258 <parse_int+0xd8>
mov    %rax,%rdi
mov    $0x3d,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1258 <parse_int+0xd8>
mov    %rax,%rbx
nopl   (%rax)
cmpb   $0x20,0x1(%rbx)
lea    0x1(%rbx),%rbx
je     1210 <parse_int+0x90>
lea    0xddf(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcspn@plt>
cmp    $0x4f,%eax
jg     1258 <parse_int+0xd8>
mov    %eax,%r15d
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
movslq %r15d,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %rbp,%rdi
call   1080 <atoi@plt>
mov    %eax,(%r14)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.parse_int.empty = [2 x i8] c" \00"
@.str = [2 x i8] c" \00"
@.str.1 = [2 x i8] c"=\00"
define i32 @parse_int(i8* %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [2 x i8]
%12 = alloca [80 x i8]
%13 = alloca i32
%14 = alloca [10 x i8]
store i8* %0, i8** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
%15 = bitcast [2 x i8]* %11 to i8*
store i32 8, i32* %13
%16 = getelementptr inbounds [10 x i8], [10 x i8]* %14, i64 0, i64 0
%17 = load i8*, i8** %7
%18 = call i32 @strcpy(i8* %16, i8* %17)
%19 = getelementptr inbounds [10 x i8], [10 x i8]* %14, i64 0, i64 0
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
store i32 %21, i32* %8
br label %22
22: ; preds = %29, %3
%23 = load i32, i32* %8
%24 = load i32, i32* %13
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = getelementptr inbounds [10 x i8], [10 x i8]* %14, i64 0, i64 0
%28 = call i32 @strcat(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %26
%30 = load i32, i32* %8
%31 = add nsw i32 %30, 1
store i32 %31, i32* %8
br label %22
32: ; preds = %22
%33 = getelementptr inbounds [10 x i8], [10 x i8]* %14, i64 0, i64 0
%34 = call i32 @strcat(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%35 = load i8*, i8** %5
%36 = getelementptr inbounds [10 x i8], [10 x i8]* %14, i64 0, i64 0
%37 = call i8* @strstr(i8* %35, i8* %36)
store i8* %37, i8** %10
%38 = load i8*, i8** %10
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %41
40: ; preds = %32
store i32 1, i32* %4
br label %73
41: ; preds = %32
%42 = load i8*, i8** %10
%43 = call i8* @strchr(i8* %42, i8 signext 61)
store i8* %43, i8** %10
%44 = load i8*, i8** %10
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %47
46: ; preds = %41
store i32 1, i32* %4
br label %73
47: ; preds = %41
br label %48
48: ; preds = %54, %47
%49 = load i8*, i8** %10
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %10
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %52, 32
br i1 %53, label %54, label %55
54: ; preds = %48
br label %48
55: ; preds = %48
%56 = load i8*, i8** %10
%57 = getelementptr inbounds [2 x i8], [2 x i8]* %11, i64 0, i64 0
%58 = call i32 @strcspn(i8* %56, i8* %57)
store i32 %58, i32* %9
%59 = load i32, i32* %9
%60 = icmp sge i32 %59, 80
br i1 %60, label %61, label %62
61: ; preds = %55
store i32 1, i32* %4
br label %73
62: ; preds = %55
%63 = getelementptr inbounds [80 x i8], [80 x i8]* %12, i64 0, i64 0
%64 = load i8*, i8** %10
%65 = load i32, i32* %9
%66 = call i32 @strncpy(i8* %63, i8* %64, i32 %65)
%67 = load i32, i32* %9
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [80 x i8], [80 x i8]* %12, i64 0, i64 %68
store i8 0, i8* %69
%70 = getelementptr inbounds [80 x i8], [80 x i8]* %12, i64 0, i64 0
%71 = call i32 @atoi(i8* %70)
%72 = load i32*, i32** %6
store i32 %71, i32* %72
store i32 0, i32* %4
br label %73
73: ; preds = %62, %61, %46, %40
%74 = load i32, i32* %4
ret i32 %74
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
@.str.1 = [2 x i8] c"=\00"
define i32 @parse_int(i8* %0, i32* nocapture writeonly %1, i8* %2) {
%4 = alloca [80 x i8]
%5 = alloca [10 x i8]
%6 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%8 = call i32 @strcpy(i8* %7, i8* %2)
%9 = call i32 @strlen(i8* %7)
%10 = icmp slt i32 %9, 8
br i1 %10, label %11, label %16
11: ; preds = %3, %11
%12 = phi i32 [ %14, %11 ], [ %9, %3 ]
%13 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = add i32 %12, 1
%15 = icmp eq i32 %14, 8
br i1 %15, label %16, label %11
16: ; preds = %11, %3
%17 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = call i8* @strstr(i8* dereferenceable(1) %0, i8* %7)
%19 = icmp eq i8* %18, null
br i1 %19, label %36, label %20
20: ; preds = %16
%21 = call i8* @strchr(i8* %18, i8 signext 61)
%22 = icmp eq i8* %21, null
br i1 %22, label %36, label %23
23: ; preds = %20, %23
%24 = phi i8* [ %25, %23 ], [ %21, %20 ]
%25 = getelementptr inbounds i8, i8* %24, i64 1
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 32
br i1 %27, label %23, label %28
28: ; preds = %23
%29 = call i32 @strcspn(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%30 = icmp sgt i32 %29, 79
br i1 %30, label %36, label %31
31: ; preds = %28
%32 = call i32 @strncpy(i8* %6, i8* %25, i32 %29)
%33 = sext i32 %29 to i64
%34 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 %33
store i8 0, i8* %34
%35 = call i32 @atoi(i8* %6)
store i32 %35, i32* %1
br label %36
36: ; preds = %28, %20, %16, %31
%37 = phi i32 [ 0, %31 ], [ 1, %16 ], [ 1, %20 ], [ 1, %28 ]
ret i32 %37
}