#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*/ scan_str;

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int sscanf(char *, char *, char *);
const char *cmdline_strvar(int argc, char **argv, const char *var) {
  enum { NUM_BUFS = 32, BUF_SIZE = 256 };
  static char buf[NUM_BUFS][BUF_SIZE];
  static int last_buf = 0;
  char scan_str[128];
  int i, next_buf;
  next_buf = (last_buf + 1) % NUM_BUFS;
  snprintf(scan_str, sizeof(scan_str), "-D%s=%%s", var);
  for (i = 1; i < argc; i++) {
    if (sscanf(argv[i], scan_str, buf[next_buf]) == 1) {
      last_buf = next_buf;
      return buf[last_buf];
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<cmdline_strvar>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x4f04(%rip),%eax
add    $0x1,%eax
mov    $0x20,%ecx
cltd
idiv   %ecx
mov    %edx,-0xa8(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x80,%esi
lea    0xe9c(%rip),%rdx
call   1030 <snprintf@plt>
movl   $0x1,-0xa4(%rbp)
mov    -0xa4(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11fe <cmdline_strvar+0xde>
mov    -0x18(%rbp),%rax
movslq -0xa4(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    -0xa0(%rbp),%rsi
movslq -0xa8(%rbp),%rax
lea    0x2e9a(%rip),%rdx
shl    $0x8,%rax
add    %rax,%rdx
call   1040 <sscanf@plt>
cmp    $0x1,%eax
jne    11e5 <cmdline_strvar+0xc5>
mov    -0xa8(%rbp),%eax
mov    %eax,0x4e79(%rip)
movslq 0x4e72(%rip),%rcx
lea    0x2e6b(%rip),%rax
shl    $0x8,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    1206 <cmdline_strvar+0xe6>
jmp    11ea <cmdline_strvar+0xca>
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    1173 <cmdline_strvar+0x53>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmdline_strvar>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rdx,%rcx
mov    %rsi,%r14
mov    %edi,%r12d
mov    0x4f00(%rip),%ebx
lea    0x1(%rbx),%eax
lea    0x20(%rbx),%ebp
test   %eax,%eax
cmovns %eax,%ebp
lea    0xeae(%rip),%rdx
mov    %rsp,%rdi
mov    $0x80,%esi
call   1030 <snprintf@plt>
cmp    $0x2,%r12d
jl     11ad <cmdline_strvar+0x8d>
and    $0xffffffe0,%ebp
neg    %ebp
lea    (%rbx,%rbp,1),%r15d
add    $0x1,%r15d
movslq %r15d,%rax
shl    $0x8,%rax
lea    0x2ec0(%rip),%rbx
add    %rax,%rbx
mov    %r12d,%r13d
mov    $0x1,%ebp
mov    %rsp,%r12
xchg   %ax,%ax
mov    (%r14,%rbp,8),%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <sscanf@plt>
cmp    $0x1,%eax
je     11b1 <cmdline_strvar+0x91>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1190 <cmdline_strvar+0x70>
xor    %ebx,%ebx
jmp    11b8 <cmdline_strvar+0x98>
mov    %r15d,0x4e88(%rip)
mov    %rbx,%rax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@cmdline_strvar.buf = internal global [32 x [256 x i8]] zeroinitializer
@cmdline_strvar.last_buf = internal global i32 0
@.str = [9 x i8] c"-D%s=%%s\00"
define i8* @cmdline_strvar(i32 %0, i8** %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca [128 x i8]
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8* %2, i8** %7
%11 = load i32, i32* @cmdline_strvar.last_buf
%12 = add nsw i32 %11, 1
%13 = srem i32 %12, 32
store i32 %13, i32* %10
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%15 = load i8*, i8** %7
%16 = call i32 @snprintf(i8* %14, i32 128, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %9
br label %17
17: ; preds = %41, %3
%18 = load i32, i32* %9
%19 = load i32, i32* %5
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %44
21: ; preds = %17
%22 = load i8**, i8*** %6
%23 = load i32, i32* %9
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %22, i64 %24
%26 = load i8*, i8** %25
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%28 = load i32, i32* %10
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [32 x [256 x i8]], [32 x [256 x i8]]* @cmdline_strvar.buf, i64 0, i64 %29
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %30, i64 0, i64 0
%32 = call i32 @sscanf(i8* %26, i8* %27, i8* %31)
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %40
34: ; preds = %21
%35 = load i32, i32* %10
store i32 %35, i32* @cmdline_strvar.last_buf
%36 = load i32, i32* @cmdline_strvar.last_buf
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [32 x [256 x i8]], [32 x [256 x i8]]* @cmdline_strvar.buf, i64 0, i64 %37
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %38, i64 0, i64 0
store i8* %39, i8** %4
br label %45
40: ; preds = %21
br label %41
41: ; preds = %40
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %17
44: ; preds = %17
store i8* null, i8** %4
br label %45
45: ; preds = %44, %34
%46 = load i8*, i8** %4
ret i8* %46
}

LLVM IR (-O2):
@cmdline_strvar.buf = internal global [32 x [256 x i8]] zeroinitializer
@cmdline_strvar.last_buf = internal unnamed_addr global i32 0
@.str = [9 x i8] c"-D%s=%%s\00"
define i8* @cmdline_strvar(i32 %0, i8** nocapture %1, i8* %2) {
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @cmdline_strvar.last_buf
%7 = add nsw i32 %6, 1
%8 = srem i32 %7, 32
%9 = call i32 @snprintf(i8* %5, i32 128, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %2)
%10 = icmp sgt i32 %0, 1
br i1 %10, label %11, label %25
11: ; preds = %3
%12 = sext i32 %8 to i64
%13 = getelementptr inbounds [32 x [256 x i8]], [32 x [256 x i8]]* @cmdline_strvar.buf, i64 0, i64 %12, i64 0
%14 = zext i32 %0 to i64
br label %17
15: ; preds = %17
%16 = icmp eq i64 %23, %14
br i1 %16, label %25, label %17
17: ; preds = %11, %15
%18 = phi i64 [ 1, %11 ], [ %23, %15 ]
%19 = getelementptr inbounds i8*, i8** %1, i64 %18
%20 = load i8*, i8** %19
%21 = call i32 @sscanf(i8* %20, i8* %5, i8* %13)
%22 = icmp eq i32 %21, 1
%23 = add nuw nsw i64 %18, 1
br i1 %22, label %24, label %15
24: ; preds = %17
store i32 %8, i32* @cmdline_strvar.last_buf
br label %25
25: ; preds = %15, %3, %24
%26 = phi i8* [ %13, %24 ], [ null, %3 ], [ null, %15 ]
ret i8* %26
}