#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*/ __NR_unshare;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ sethostname(char *, scalar_t__);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(char *);
scalar_t__ syscall(int /*<<< orphan*/, int);
int /*<<< orphan*/ usage();
int main(int argc, char **argv) {
  if (argc < 3) {
    usage();
    exit(1);
  }
  if (syscall(__NR_unshare, 0x04000000)) {
    fprintf(stderr, "chname: New utsname namespace failed: %s\n",
            strerror(errno));
    exit(1);
  }
  if (sethostname(argv[1], strlen(argv[1]) + 1)) {
    fprintf(stderr, "chname: Set hostname failed: %s\n", strerror(errno));
    exit(1);
  }
  execvp(argv[2], &argv[2]);
  fprintf(stderr, "chname: exec failed: %s\n", strerror(errno));
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11b1 <main+0x31>
mov    $0x0,%al
call   1070 <usage@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    $0x4000000,%esi
call   1060 <syscall@plt>
cmp    $0x0,%rax
je     1204 <main+0x84>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe0b(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
call   1050 <sethostname@plt>
cmp    $0x0,%rax
je     126d <main+0xed>
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xdcc(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   1080 <execvp@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xda2(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jle    1206 <main+0x86>
mov    %rsi,%r14
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    $0x4000000,%esi
call   1060 <syscall@plt>
test   %rax,%rax
jne    1217 <main+0x97>
mov    0x8(%r14),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %rbx,%rdi
call   1050 <sethostname@plt>
test   %rax,%rax
jne    1237 <main+0xb7>
mov    0x10(%r14),%rdi
add    $0x10,%r14
mov    %r14,%rsi
call   1080 <execvp@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%ebx
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe5c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1070 <usage@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%ebx
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdcb(%rip),%rsi
jmp    1255 <main+0xd5>
mov    0x2da2(%rip),%rax
mov    (%rax),%ebx
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdd5(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@__NR_unshare = global i32 0
@stderr = global i32 0
@.str = [42 x i8] c"chname: New utsname namespace failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [33 x i8] c"chname: Set hostname failed: %s\0A\00"
@.str.2 = [25 x i8] c"chname: exec failed: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 3
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = call i32 (...) @usage()
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = load i32, i32* @__NR_unshare
%13 = call i64 @syscall(i32 %12, i32 67108864)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %11
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i64 @strlen(i8* %27)
%29 = add nsw i64 %28, 1
%30 = call i64 @sethostname(i8* %24, i64 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %21
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %21
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 2
%41 = load i8*, i8** %40
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = call i32 @execvp(i8* %41, i8** %43)
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %47)
ret i32 1
}

LLVM IR (-O2):
@__NR_unshare = global i32 0
@stderr = global i32 0
@.str = [42 x i8] c"chname: New utsname namespace failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [33 x i8] c"chname: Set hostname failed: %s\0A\00"
@.str.2 = [25 x i8] c"chname: exec failed: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 (...) @usage()
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = load i32, i32* @__NR_unshare
%9 = tail call i64 @syscall(i32 %8, i32 67108864)
%10 = icmp eq i64 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %7
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i64 @strlen(i8* dereferenceable(1) %19)
%21 = add nsw i64 %20, 1
%22 = tail call i64 @sethostname(i8* %19, i64 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %17
%31 = getelementptr inbounds i8*, i8** %1, i64 2
%32 = load i8*, i8** %31
%33 = tail call i32 @execvp(i8* %32, i8** %31)
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = tail call i8* @strerror(i32 %35)
%37 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %36)
ret i32 1
}