#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 ssize_t;
typedef int /*<<< orphan*/ fstr;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, double);
int sprintf(char *, char *, int);
double strtod(char *, int /*<<< orphan*/ *);
int test_strtod(void) {
  char buf[300];
  int cnt;
  int result = 0;
  for (cnt = 0; cnt < 200; ++cnt) {
    ssize_t n;
    double f;
    n = sprintf(buf, "%d", cnt);
    memset(buf + n, '0', cnt);
    sprintf(buf + n + cnt, ".000e-%d", cnt);
    f = strtod(buf, NULL);
    if (f != (double)cnt) {
      char fstr[128];
      char fcntstr[128];
      snprintf(fstr, sizeof(fstr),
               "%"
               ""
               "g",
               f);
      snprintf(fcntstr, sizeof(fstr),
               "%"
               ""
               "g",
               (double)cnt);
      printf("strto"
             "d"
             "(\"%s\") "
             "failed for cnt == %d (%s instead of %s)\n",
             buf, cnt, fstr, fcntstr);
      result = 1;
    } else
      printf("strto"
             "d"
             "() fine for cnt == %d\n",
             cnt);
  }
  return result;
}


x86 assembly (clang, -O0):
<test_strtod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x138(%rbp)
movl   $0x0,-0x134(%rbp)
cmpl   $0xc8,-0x134(%rbp)
jge    12ce <test_strtod+0x17e>
lea    -0x130(%rbp),%rdi
mov    -0x134(%rbp),%edx
lea    0xe73(%rip),%rsi
call   1070 <sprintf@plt>
mov    %eax,-0x13c(%rbp)
lea    -0x130(%rbp),%rdi
movslq -0x13c(%rbp),%rax
add    %rax,%rdi
mov    -0x134(%rbp),%edx
mov    $0x30,%esi
call   1060 <memset@plt>
lea    -0x130(%rbp),%rdi
movslq -0x13c(%rbp),%rax
add    %rax,%rdi
movslq -0x134(%rbp),%rax
add    %rax,%rdi
mov    -0x134(%rbp),%edx
lea    0xe19(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x130(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <strtod@plt>
movsd  %xmm0,-0x148(%rbp)
ff
movsd  -0x148(%rbp),%xmm0
ff
cvtsi2sdl -0x134(%rbp),%xmm1
ff
ucomisd %xmm1,%xmm0
jne    1229 <test_strtod+0xd9>
jp     1229 <test_strtod+0xd9>
jmp    12a1 <test_strtod+0x151>
lea    -0x1d0(%rbp),%rdi
movsd  -0x148(%rbp),%xmm0
ff
mov    $0x80,%esi
lea    0xdc5(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x250(%rbp),%rdi
cvtsi2sdl -0x134(%rbp),%xmm0
ff
mov    $0x80,%esi
lea    0xda5(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x130(%rbp),%rsi
mov    -0x134(%rbp),%edx
lea    -0x1d0(%rbp),%rcx
lea    -0x250(%rbp),%r8
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x138(%rbp)
jmp    12b5 <test_strtod+0x165>
mov    -0x134(%rbp),%esi
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ba <test_strtod+0x16a>
mov    -0x134(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x134(%rbp)
jmp    116f <test_strtod+0x1f>
mov    -0x138(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_strtod>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x248,%rsp
lea    0x10(%rsp),%r13
xor    %r14d,%r14d
xor    %r15d,%r15d
jmp    1192 <test_strtod+0x42>
xchg   %ax,%ax
lea    0xecb(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r14
cmp    $0xc8,%r14
je     1258 <test_strtod+0x108>
mov    %r13,%rdi
lea    0xe6a(%rip),%rsi
mov    %r14d,%edx
call   1070 <sprintf@plt>
movslq %eax,%r12
lea    (%rsp,%r12,1),%rdi
add    $0x10,%rdi
mov    $0x30,%esi
mov    %r14d,%edx
call   1060 <memset@plt>
add    %r14,%r12
lea    (%rsp,%r12,1),%rdi
add    $0x10,%rdi
lea    0xe32(%rip),%rsi
mov    %r14d,%edx
call   1070 <sprintf@plt>
mov    %r13,%rdi
xor    %esi,%esi
call   1030 <strtod@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %r14d,%xmm1
ucomisd %xmm1,%xmm0
jne    11f0 <test_strtod+0xa0>
jnp    1170 <test_strtod+0x20>
lea    0x1c0(%rsp),%r15
mov    %r15,%rdi
mov    $0x80,%esi
lea    0xe02(%rip),%rbp
mov    %rbp,%rdx
movsd  %xmm1,0x8(%rsp)
call   1050 <snprintf@plt>
lea    0x140(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %rbp,%rdx
movsd  0x8(%rsp),%xmm0
call   1050 <snprintf@plt>
lea    0xdd2(%rip),%rdi
mov    %r13,%rsi
mov    %r14d,%edx
mov    %r15,%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%r15d
jmp    1181 <test_strtod+0x31>
mov    %r15d,%eax
add    $0x248,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [9 x i8] c".000e-%d\00"
@.str.2 = [3 x i8] c"%g\00"
@.str.3 = [54 x i8] c"strtod(\22%s\22) failed for cnt == %d (%s instead of %s)\0A\00"
@.str.4 = [29 x i8] c"strtod() fine for cnt == %d\0A\00"
define i32 @test_strtod() {
%1 = alloca [300 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
%6 = alloca [128 x i8]
%7 = alloca [128 x i8]
store i32 0, i32* %3
store i32 0, i32* %2
br label %8
8: ; preds = %53, %0
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 200
br i1 %10, label %11, label %56
11: ; preds = %8
%12 = getelementptr inbounds [300 x i8], [300 x i8]* %1, i64 0, i64 0
%13 = load i32, i32* %2
%14 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %4
%15 = getelementptr inbounds [300 x i8], [300 x i8]* %1, i64 0, i64 0
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i32, i32* %2
%20 = call i32 @memset(i8* %18, i32 48, i32 %19)
%21 = getelementptr inbounds [300 x i8], [300 x i8]* %1, i64 0, i64 0
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i32, i32* %2
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i32, i32* %2
%29 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = getelementptr inbounds [300 x i8], [300 x i8]* %1, i64 0, i64 0
%31 = call double @strtod(i8* %30, i32* null)
store double %31, double* %5
%32 = load double, double* %5
%33 = load i32, i32* %2
%34 = sitofp i32 %33 to double
%35 = fcmp une double %32, %34
br i1 %35, label %36, label %49
36: ; preds = %11
%37 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%38 = load double, double* %5
%39 = call i32 @snprintf(i8* %37, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), double %38)
%40 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%41 = load i32, i32* %2
%42 = sitofp i32 %41 to double
%43 = call i32 @snprintf(i8* %40, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), double %42)
%44 = getelementptr inbounds [300 x i8], [300 x i8]* %1, i64 0, i64 0
%45 = load i32, i32* %2
%46 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i8* %44, i32 %45, i8* %46, i8* %47)
store i32 1, i32* %3
br label %52
49: ; preds = %11
%50 = load i32, i32* %2
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %50)
br label %52
52: ; preds = %49, %36
br label %53
53: ; preds = %52
%54 = load i32, i32* %2
%55 = add nsw i32 %54, 1
store i32 %55, i32* %2
br label %8
56: ; preds = %8
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [9 x i8] c".000e-%d\00"
@.str.2 = [3 x i8] c"%g\00"
@.str.3 = [54 x i8] c"strtod(\22%s\22) failed for cnt == %d (%s instead of %s)\0A\00"
@.str.4 = [29 x i8] c"strtod() fine for cnt == %d\0A\00"
define i32 @test_strtod() {
%1 = alloca [300 x i8]
%2 = alloca [128 x i8]
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [300 x i8], [300 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
br label %7
7: ; preds = %0, %26
%8 = phi i64 [ 0, %0 ], [ %28, %26 ]
%9 = phi i32 [ 0, %0 ], [ %27, %26 ]
%10 = trunc i64 %8 to i32
%11 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [300 x i8], [300 x i8]* %1, i64 0, i64 %12
%14 = call i32 @memset(i8* %13, i32 48, i32 %10)
%15 = getelementptr inbounds i8, i8* %13, i64 %8
%16 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%17 = call double @strtod(i8* nocapture %4, i32* null)
%18 = sitofp i32 %10 to double
%19 = fcmp une double %17, %18
br i1 %19, label %20, label %24
20: ; preds = %7
%21 = call i32 @snprintf(i8* %5, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), double %17)
%22 = call i32 @snprintf(i8* %6, i32 128, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), double %18)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i8* %4, i32 %10, i8* %5, i8* %6)
br label %26
24: ; preds = %7
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %10)
br label %26
26: ; preds = %24, %20
%27 = phi i32 [ 1, %20 ], [ %9, %24 ]
%28 = add nuw nsw i64 %8, 1
%29 = icmp eq i64 %28, 200
br i1 %29, label %30, label %7
30: ; preds = %26
ret i32 %27
}