#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 katcp_dispatch {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ add_version_katcp(struct katcp_dispatch *, char *, int,
                                     char *, int /*<<< orphan*/ *);
scalar_t__ expand_modes_katcp(struct katcp_dispatch *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
int mode_version_katcp(struct katcp_dispatch *d, int mode, char *subsystem,
                       int major, int minor) {
  char buffer[20];
  ;
  if (expand_modes_katcp(d, mode) < 0) {
    return -1;
  }
  if (subsystem) {
    snprintf(buffer, 20, "%d.%d", major, minor);
    buffer[20 - 1] = '\0';
    add_version_katcp(d, subsystem, mode, buffer, NULL);
  }
  return 0;
}


x86 assembly (clang, -O0):
<mode_version_katcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <expand_modes_katcp@plt>
cmp    $0x0,%rax
jge    116c <mode_version_katcp+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b7 <mode_version_katcp+0x87>
cmpq   $0x0,-0x20(%rbp)
je     11b0 <mode_version_katcp+0x80>
lea    -0x40(%rbp),%rdi
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%r8d
mov    $0x14,%esi
lea    0xe72(%rip),%rdx
call   1040 <snprintf@plt>
movb   $0x0,-0x2d(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x40(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1030 <add_version_katcp@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mode_version_katcp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r15d
mov    %ecx,%r12d
mov    %rdx,%r14
mov    %esi,%r13d
mov    %rdi,%rbx
call   1050 <expand_modes_katcp@plt>
test   %rax,%rax
js     1193 <mode_version_katcp+0x63>
test   %r14,%r14
je     119a <mode_version_katcp+0x6a>
lea    0xe9d(%rip),%rdx
mov    %rsp,%rdi
mov    $0x14,%esi
mov    %r12d,%ecx
mov    %r15d,%r8d
call   1040 <snprintf@plt>
movb   $0x0,0x13(%rsp)
xor    %ebp,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
mov    %rsp,%rcx
xor    %r8d,%r8d
call   1030 <add_version_katcp@plt>
jmp    119c <mode_version_katcp+0x6c>
mov    $0xffffffff,%ebp
jmp    119c <mode_version_katcp+0x6c>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.katcp_dispatch = type { i32 }
@.str = [6 x i8] c"%d.%d\00"
define i32 @mode_version_katcp(%struct.katcp_dispatch* %0, i32 %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.katcp_dispatch*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [20 x i8]
store %struct.katcp_dispatch* %0, %struct.katcp_dispatch** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%13 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %7
%14 = load i32, i32* %8
%15 = call i64 @expand_modes_katcp(%struct.katcp_dispatch* %13, i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %5
store i32 -1, i32* %6
br label %33
18: ; preds = %5
%19 = load i8*, i8** %9
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = getelementptr inbounds [20 x i8], [20 x i8]* %12, i64 0, i64 0
%23 = load i32, i32* %10
%24 = load i32, i32* %11
%25 = call i32 @snprintf(i8* %22, i32 20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %24)
%26 = getelementptr inbounds [20 x i8], [20 x i8]* %12, i64 0, i64 19
store i8 0, i8* %26
%27 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %7
%28 = load i8*, i8** %9
%29 = load i32, i32* %8
%30 = getelementptr inbounds [20 x i8], [20 x i8]* %12, i64 0, i64 0
%31 = call i32 @add_version_katcp(%struct.katcp_dispatch* %27, i8* %28, i32 %29, i8* %30, i32* null)
br label %32
32: ; preds = %21, %18
store i32 0, i32* %6
br label %33
33: ; preds = %32, %17
%34 = load i32, i32* %6
ret i32 %34
}

LLVM IR (-O2):
%struct.katcp_dispatch = type { i32 }
@.str = [6 x i8] c"%d.%d\00"
define i32 @mode_version_katcp(%struct.katcp_dispatch* %0, i32 %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca [20 x i8]
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%8 = tail call i64 @expand_modes_katcp(%struct.katcp_dispatch* %0, i32 %1)
%9 = icmp slt i64 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %5
%11 = icmp eq i8* %2, null
br i1 %11, label %16, label %12
12: ; preds = %10
%13 = call i32 @snprintf(i8* %7, i32 20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %4)
%14 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 19
store i8 0, i8* %14
%15 = call i32 @add_version_katcp(%struct.katcp_dispatch* %0, i8* %2, i32 %1, i8* %7, i32* null)
br label %16
16: ; preds = %10, %12, %5
%17 = phi i32 [ -1, %5 ], [ 0, %12 ], [ 0, %10 ]
ret i32 %17
}