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

/* Variables and functions */
int EOF;
int _doscan(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fdmem(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ strlen(char const *);
int vsscanf(const char *str, const char *fmt, va_list ap) {
  FILE *fp;
  int fd, x;
  fd = fdmem(str, strlen(str));
  if (fd < 0) {
    return (EOF);
  }
  fp = fdopen(fd, "r");
  if (fp == 0) {
    close(fd);
    return (EOF);
  }
  x = _doscan(fp, fmt, ap);
  fclose(fp);
  return (x);
}


x86 assembly (clang, -O0):
<vsscanf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
call   1060 <fdmem@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11ad <vsscanf+0x4d>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1206 <vsscanf+0xa6>
mov    -0x2c(%rbp),%edi
lea    0xe49(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e4 <vsscanf+0x84>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1206 <vsscanf+0xa6>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <_doscan@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vsscanf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1060 <fdmem@plt>
test   %eax,%eax
js     11be <vsscanf+0x5e>
mov    %eax,%ebp
lea    0xe74(%rip),%rsi
mov    %eax,%edi
call   1070 <fdopen@plt>
test   %rax,%rax
je     11b7 <vsscanf+0x57>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1080 <_doscan@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
jmp    11c7 <vsscanf+0x67>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
@.str = [2 x i8] c"r\00"
define i32 @vsscanf(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = load i8*, i8** %5
%13 = call i32 @strlen(i8* %12)
%14 = call i32 @fdmem(i8* %11, i32 %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i32, i32* @EOF
store i32 %18, i32* %4
br label %36
19: ; preds = %3
%20 = load i32, i32* %9
%21 = call i32* @fdopen(i32 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %8
%22 = load i32*, i32** %8
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i32, i32* %9
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* @EOF
store i32 %27, i32* %4
br label %36
28: ; preds = %19
%29 = load i32*, i32** %8
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = call i32 @_doscan(i32* %29, i8* %30, i32 %31)
store i32 %32, i32* %10
%33 = load i32*, i32** %8
%34 = call i32 @fclose(i32* %33)
%35 = load i32, i32* %10
store i32 %35, i32* %4
br label %36
36: ; preds = %28, %24, %17
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@EOF = global i32 0
@.str = [2 x i8] c"r\00"
define i32 @vsscanf(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @fdmem(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = load i32, i32* @EOF
br label %18
9: ; preds = %3
%10 = tail call i32* @fdopen(i32 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i32 @close(i32 %5)
%14 = load i32, i32* @EOF
br label %18
15: ; preds = %9
%16 = tail call i32 @_doscan(i32* %10, i8* %1, i32 %2)
%17 = tail call i32 @fclose(i32* %10)
br label %18
18: ; preds = %15, %12, %7
%19 = phi i32 [ %8, %7 ], [ %14, %12 ], [ %16, %15 ]
ret i32 %19
}