#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 /*<<< orphan*/ errx(int, char *, int);
size_t snprintf(char *, size_t, char *, char *, char *);
void setup_cmdline(char *cmdline, int argc, char **argv) {
  size_t cmdline_free = 8192;
  cmdline[0] = 0;
  for (; *argv; argc--, argv++) {
    size_t alen =
        snprintf(cmdline, cmdline_free, "%s%s", *argv, (argc > 1) ? " " : "");
    if (alen >= cmdline_free) {
      errx(1, "Guest command line too long (max=%d characters)", 8192 - 1);
      break;
    }
    cmdline_free -= alen;
    cmdline += alen;
  }
}


x86 assembly (clang, -O0):
<setup_cmdline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x2000,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     11e5 <setup_cmdline+0xc5>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
mov    -0xc(%rbp),%edx
lea    0xe9d(%rip),%r8
lea    0xe95(%rip),%rax
cmp    $0x1,%edx
cmovg  %rax,%r8
lea    0xe82(%rip),%rdx
call   1040 <snprintf@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     11b0 <setup_cmdline+0x90>
mov    $0x1,%edi
lea    0xe66(%rip),%rsi
mov    $0x1fff,%edx
call   1030 <errx@plt>
jmp    11e5 <setup_cmdline+0xc5>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
jmp    1142 <setup_cmdline+0x22>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_cmdline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movb   $0x0,(%rdi)
mov    (%rdx),%rcx
test   %rcx,%rcx
je     1198 <setup_cmdline+0x78>
mov    %rdx,%r15
mov    %esi,%r14d
mov    %rdi,%rbp
add    $0x8,%r15
mov    $0x2000,%ebx
lea    0xeb7(%rip),%r13
lea    0xeaa(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
cmp    $0x2,%r14d
mov    %r13,%r8
lea    0xe97(%rip),%rax
cmovge %rax,%r8
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1040 <snprintf@plt>
sub    %rax,%rbx
jbe    11a7 <setup_cmdline+0x87>
add    %rax,%rbp
add    $0xffffffff,%r14d
mov    (%r15),%rcx
add    $0x8,%r15
test   %rcx,%rcx
jne    1160 <setup_cmdline+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe59(%rip),%rsi
mov    $0x1,%edi
mov    $0x1fff,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <errx@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [48 x i8] c"Guest command line too long (max=%d characters)\00"
define void @setup_cmdline(i8* %0, i32 %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8** %2, i8*** %6
store i64 8192, i64* %7
%9 = load i8*, i8** %4
%10 = getelementptr inbounds i8, i8* %9, i64 0
store i8 0, i8* %10
br label %11
11: ; preds = %37, %3
%12 = load i8**, i8*** %6
%13 = load i8*, i8** %12
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %42
15: ; preds = %11
%16 = load i8*, i8** %4
%17 = load i64, i64* %7
%18 = load i8**, i8*** %6
%19 = load i8*, i8** %18
%20 = load i32, i32* %5
%21 = icmp sgt i32 %20, 1
%22 = zext i1 %21 to i64
%23 = select i1 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%24 = call i64 @snprintf(i8* %16, i64 %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %23)
store i64 %24, i64* %8
%25 = load i64, i64* %8
%26 = load i64, i64* %7
%27 = icmp uge i64 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %15
%29 = call i32 @errx(i32 1, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 8191)
br label %42
30: ; preds = %15
%31 = load i64, i64* %8
%32 = load i64, i64* %7
%33 = sub i64 %32, %31
store i64 %33, i64* %7
%34 = load i64, i64* %8
%35 = load i8*, i8** %4
%36 = getelementptr inbounds i8, i8* %35, i64 %34
store i8* %36, i8** %4
br label %37
37: ; preds = %30
%38 = load i32, i32* %5
%39 = add nsw i32 %38, -1
store i32 %39, i32* %5
%40 = load i8**, i8*** %6
%41 = getelementptr inbounds i8*, i8** %40, i32 1
store i8** %41, i8*** %6
br label %11
42: ; preds = %28, %11
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [48 x i8] c"Guest command line too long (max=%d characters)\00"
define void @setup_cmdline(i8* %0, i32 %1, i8** nocapture %2) {
store i8 0, i8* %0
%4 = load i8*, i8** %2
%5 = icmp eq i8* %4, null
br i1 %5, label %25, label %6
6: ; preds = %3, %18
%7 = phi i8* [ %23, %18 ], [ %4, %3 ]
%8 = phi i8* [ %20, %18 ], [ %0, %3 ]
%9 = phi i64 [ %19, %18 ], [ 8192, %3 ]
%10 = phi i8** [ %22, %18 ], [ %2, %3 ]
%11 = phi i32 [ %21, %18 ], [ %1, %3 ]
%12 = icmp sgt i32 %11, 1
%13 = select i1 %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%14 = tail call i64 @snprintf(i8* %8, i64 %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %7, i8* %13)
%15 = icmp ugt i64 %9, %14
br i1 %15, label %18, label %16
16: ; preds = %6
%17 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 8191)
br label %25
18: ; preds = %6
%19 = sub i64 %9, %14
%20 = getelementptr inbounds i8, i8* %8, i64 %14
%21 = add nsw i32 %11, -1
%22 = getelementptr inbounds i8*, i8** %10, i64 1
%23 = load i8*, i8** %22
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %6
25: ; preds = %18, %3, %16
ret void
}