#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 EINVAL;
long strtol(char const *, char **, int /*<<< orphan*/);
int string_to_int(const char *string, int *i) {
  char *tailptr;
  long retval;
  retval = strtol(string, &tailptr, 0);
  if (tailptr == string || tailptr[0] != '\0')
    return -EINVAL;
  *i = retval;
  return 0;
}


x86 assembly (clang, -O0):
<string_to_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1030 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1151 <string_to_int+0x41>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1164 <string_to_int+0x54>
xor    %eax,%eax
mov    0x2e86(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1177 <string_to_int+0x67>
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_to_int>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
xor    %ebp,%ebp
xor    %edx,%edx
call   1030 <strtol@plt>
mov    0x8(%rsp),%rcx
cmp    %rbx,%rcx
je     113b <string_to_int+0x2b>
cmpb   $0x0,(%rcx)
je     1146 <string_to_int+0x36>
mov    0x2e9e(%rip),%rax
sub    (%rax),%ebp
jmp    114b <string_to_int+0x3b>
mov    %eax,(%r14)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
define i32 @string_to_int(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = call i64 @strtol(i8* %8, i8** %6, i32 0)
store i64 %9, i64* %7
%10 = load i8*, i8** %6
%11 = load i8*, i8** %4
%12 = icmp eq i8* %10, %11
br i1 %12, label %19, label %13
13: ; preds = %2
%14 = load i8*, i8** %6
%15 = getelementptr inbounds i8, i8* %14, i64 0
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13, %2
%20 = load i32, i32* @EINVAL
%21 = sub nsw i32 0, %20
store i32 %21, i32* %3
br label %26
22: ; preds = %13
%23 = load i64, i64* %7
%24 = trunc i64 %23 to i32
%25 = load i32*, i32** %5
store i32 %24, i32* %25
store i32 0, i32* %3
br label %26
26: ; preds = %22, %19
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
@EINVAL = global i32 0
define i32 @string_to_int(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = call i64 @strtol(i8* %0, i8** %3, i32 0)
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, %0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i8, i8* %6
%10 = icmp eq i8 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %8, %2
%12 = load i32, i32* @EINVAL
%13 = sub nsw i32 0, %12
br label %16
14: ; preds = %8
%15 = trunc i64 %5 to i32
store i32 %15, i32* %1
br label %16
16: ; preds = %14, %11
%17 = phi i32 [ %13, %11 ], [ 0, %14 ]
ret i32 %17
}