#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 */
struct socket_gen {
  int dummy;
};
typedef int /*<<< orphan*/ retbuf;
typedef int /*<<< orphan*/ cmd;

/* Variables and functions */
int gen_dosockcmd(struct socket_gen *, char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int, unsigned int);
int sscanf(char *, char *, unsigned int *, unsigned int *, unsigned int *,
           unsigned int *, unsigned int *, unsigned int *);
int socket_simgen_getvalue(void *gen, unsigned int mode, unsigned int PID,
                           unsigned int *A, unsigned int *B, unsigned int *C,
                           unsigned int *D) {
  char cmd[32];
  snprintf(cmd, sizeof(cmd), "%02X %02X\n", mode, PID);
  char retbuf[1024];
  if (-1 ==
      gen_dosockcmd((struct socket_gen *)gen, cmd, retbuf, sizeof(retbuf))) {
    return -1;
  }
  unsigned int response;
  unsigned int retmode;
  int count = sscanf(retbuf, "%2x %2x %2x %2x %2x %2x", &response, &retmode, A,
                     B, C, D);
  return (count < 2 ? 0 : count - 2);
}


x86 assembly (clang, -O0):
<socket_simgen_getvalue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
mov    $0x20,%esi
lea    0xe94(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
lea    -0x450(%rbp),%rdx
mov    $0x400,%ecx
call   1050 <gen_dosockcmd@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11a5 <socket_simgen_getvalue+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <socket_simgen_getvalue+0xe7>
lea    -0x450(%rbp),%rdi
mov    -0x20(%rbp),%r8
mov    -0x28(%rbp),%r9
mov    -0x30(%rbp),%r10
mov    0x10(%rbp),%rax
lea    0xe48(%rip),%rsi
lea    -0x454(%rbp),%rdx
lea    -0x458(%rbp),%rcx
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1040 <sscanf@plt>
mov    %eax,-0x45c(%rbp)
cmpl   $0x2,-0x45c(%rbp)
jge    11ff <socket_simgen_getvalue+0xcf>
xor    %eax,%eax
mov    %eax,-0x460(%rbp)
jmp    120e <socket_simgen_getvalue+0xde>
mov    -0x45c(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x460(%rbp)
mov    -0x460(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_simgen_getvalue>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x430,%rsp
mov    %r9,%r14
mov    %r8,%r15
mov    %rcx,%r12
mov    %edx,%r8d
mov    %esi,%ecx
mov    %rdi,%rbx
lea    0xea8(%rip),%rdx
lea    0x10(%rsp),%r13
mov    %r13,%rdi
mov    $0x20,%esi
call   1030 <snprintf@plt>
lea    0x30(%rsp),%rdx
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0x400,%ecx
call   1050 <gen_dosockcmd@plt>
cmp    $0xffffffff,%eax
je     11c1 <socket_simgen_getvalue+0x91>
lea    0xe80(%rip),%rsi
lea    0x30(%rsp),%rdi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %r12,%r8
mov    %r15,%r9
push   0x460(%rsp)
push   %r14
call   1040 <sscanf@plt>
add    $0x10,%rsp
mov    %eax,%ecx
lea    -0x2(%rcx),%edx
xor    %eax,%eax
cmp    $0x2,%ecx
cmovge %edx,%eax
jmp    11c6 <socket_simgen_getvalue+0x96>
mov    $0xffffffff,%eax
add    $0x430,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.socket_gen = type { i32 }
@.str = [11 x i8] c"%02X %02X\0A\00"
@.str.1 = [24 x i8] c"%2x %2x %2x %2x %2x %2x\00"
define i32 @socket_simgen_getvalue(i8* %0, i32 %1, i32 %2, i32* %3, i32* %4, i32* %5, i32* %6) {
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca [32 x i8]
%17 = alloca [1024 x i8]
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
store i8* %0, i8** %9
store i32 %1, i32* %10
store i32 %2, i32* %11
store i32* %3, i32** %12
store i32* %4, i32** %13
store i32* %5, i32** %14
store i32* %6, i32** %15
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %16, i64 0, i64 0
%22 = load i32, i32* %10
%23 = load i32, i32* %11
%24 = call i32 @snprintf(i8* %21, i32 32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %23)
%25 = load i8*, i8** %9
%26 = bitcast i8* %25 to %struct.socket_gen*
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %16, i64 0, i64 0
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %17, i64 0, i64 0
%29 = call i32 @gen_dosockcmd(%struct.socket_gen* %26, i8* %27, i8* %28, i32 1024)
%30 = icmp eq i32 -1, %29
br i1 %30, label %31, label %32
31: ; preds = %7
store i32 -1, i32* %8
br label %47
32: ; preds = %7
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %17, i64 0, i64 0
%34 = load i32*, i32** %12
%35 = load i32*, i32** %13
%36 = load i32*, i32** %14
%37 = load i32*, i32** %15
%38 = call i32 @sscanf(i8* %33, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32* %18, i32* %19, i32* %34, i32* %35, i32* %36, i32* %37)
store i32 %38, i32* %20
%39 = load i32, i32* %20
%40 = icmp slt i32 %39, 2
br i1 %40, label %41, label %42
41: ; preds = %32
br label %45
42: ; preds = %32
%43 = load i32, i32* %20
%44 = sub nsw i32 %43, 2
br label %45
45: ; preds = %42, %41
%46 = phi i32 [ 0, %41 ], [ %44, %42 ]
store i32 %46, i32* %8
br label %47
47: ; preds = %45, %31
%48 = load i32, i32* %8
ret i32 %48
}

LLVM IR (-O2):
%struct.socket_gen = type { i32 }
@.str = [11 x i8] c"%02X %02X\0A\00"
@.str.1 = [24 x i8] c"%2x %2x %2x %2x %2x %2x\00"
define i32 @socket_simgen_getvalue(i8* %0, i32 %1, i32 %2, i32* %3, i32* %4, i32* %5, i32* %6) {
%8 = alloca [32 x i8]
%9 = alloca [1024 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%13 = call i32 @snprintf(i8* %12, i32 32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%15 = bitcast i8* %0 to %struct.socket_gen*
%16 = call i32 @gen_dosockcmd(%struct.socket_gen* %15, i8* %12, i8* %14, i32 1024)
%17 = icmp eq i32 %16, -1
br i1 %17, label %25, label %18
18: ; preds = %7
%19 = bitcast i32* %10 to i8*
%20 = bitcast i32* %11 to i8*
%21 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32* %10, i32* %11, i32* %3, i32* %4, i32* %5, i32* %6)
%22 = icmp slt i32 %21, 2
%23 = add nsw i32 %21, -2
%24 = select i1 %22, i32 0, i32 %23
br label %25
25: ; preds = %7, %18
%26 = phi i32 [ %24, %18 ], [ -1, %7 ]
ret i32 %26
}