#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 utsname {
  char *nodename;
};

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, ...);
char *strchr(char *, char);
char *strdup(char const *);
int strlen(char *);
scalar_t__ uname(struct utsname *);
char *nv_standardize_screen_name(const char *orig, int screen) {
  char *display_name, *screen_name, *colon, *dot, *tmp;
  struct utsname uname_buf;
  int len;
  if (!orig)
    return NULL;
  display_name = strdup(orig);
  if (!display_name)
    return NULL;
  colon = strchr(display_name, ':');
  if (!colon)
    return NULL;
  if (display_name == colon) {
    if (uname(&uname_buf) == 0) {
      len = strlen(display_name) + strlen(uname_buf.nodename) + 1;
      tmp = malloc(len);
      snprintf(tmp, len, "%s%s", uname_buf.nodename, display_name);
      free(display_name);
      display_name = tmp;
      colon = strchr(display_name, ':');
      if (!colon)
        return NULL;
    }
  }
  if (screen == -1) {
    dot = strchr(colon, '.');
    if (dot) {
      screen = atoi(dot + 1);
    } else {
      screen = 0;
    }
  }
  dot = strchr(colon, '.');
  if (dot)
    *dot = '\0';
  if (screen == -2) {
    screen_name = display_name;
  } else {
    len = strlen(display_name) + 8;
    screen_name = malloc(len);
    snprintf(screen_name, len, "%s.%d", display_name, screen);
    free(display_name);
  }
  return (screen_name);
}


x86 assembly (clang, -O0):
<nv_standardize_screen_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a7 <nv_standardize_screen_name+0x27>
movq   $0x0,-0x8(%rbp)
jmp    1373 <nv_standardize_screen_name+0x1f3>
mov    -0x10(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cc <nv_standardize_screen_name+0x4c>
movq   $0x0,-0x8(%rbp)
jmp    1373 <nv_standardize_screen_name+0x1f3>
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f6 <nv_standardize_screen_name+0x76>
movq   $0x0,-0x8(%rbp)
jmp    1373 <nv_standardize_screen_name+0x1f3>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jne    12a7 <nv_standardize_screen_name+0x127>
lea    -0x48(%rbp),%rdi
call   1070 <uname@plt>
cmp    $0x0,%rax
jne    12a2 <nv_standardize_screen_name+0x122>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x4c(%rbp),%esi
mov    -0x48(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xda5(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    129d <nv_standardize_screen_name+0x11d>
movq   $0x0,-0x8(%rbp)
jmp    1373 <nv_standardize_screen_name+0x1f3>
jmp    12a2 <nv_standardize_screen_name+0x122>
jmp    12a7 <nv_standardize_screen_name+0x127>
cmpl   $0xffffffff,-0x14(%rbp)
jne    12ef <nv_standardize_screen_name+0x16f>
mov    -0x30(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12e3 <nv_standardize_screen_name+0x163>
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
jmp    12ea <nv_standardize_screen_name+0x16a>
movl   $0x0,-0x14(%rbp)
jmp    12ef <nv_standardize_screen_name+0x16f>
mov    -0x30(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1313 <nv_standardize_screen_name+0x193>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
cmpl   $0xfffffffe,-0x14(%rbp)
jne    132a <nv_standardize_screen_name+0x1aa>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    136b <nv_standardize_screen_name+0x1eb>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x8,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x4c(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    -0x14(%rbp),%r8d
lea    0xcaa(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nv_standardize_screen_name>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     127f <nv_standardize_screen_name+0xff>
mov    %esi,%r14d
call   10a0 <strdup@plt>
test   %rax,%rax
je     127f <nv_standardize_screen_name+0xff>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     127f <nv_standardize_screen_name+0xff>
mov    %rax,%rbp
cmp    %rax,%rbx
je     11d6 <nv_standardize_screen_name+0x56>
cmp    $0xffffffff,%r14d
jne    1294 <nv_standardize_screen_name+0x114>
jmp    125c <nv_standardize_screen_name+0xdc>
lea    0x8(%rsp),%rdi
call   1070 <uname@plt>
test   %rax,%rax
je     11f4 <nv_standardize_screen_name+0x74>
mov    %rbx,%rbp
cmp    $0xffffffff,%r14d
jne    1294 <nv_standardize_screen_name+0x114>
jmp    125c <nv_standardize_screen_name+0xdc>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    0x8(%rsp),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
lea    0xddc(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r12,%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     127f <nv_standardize_screen_name+0xff>
mov    %rax,%rbp
mov    %r15,%rbx
cmp    $0xffffffff,%r14d
jne    1294 <nv_standardize_screen_name+0x114>
mov    %rbp,%rdi
mov    $0x2e,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1291 <nv_standardize_screen_name+0x111>
add    $0x1,%rax
mov    %rax,%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
jmp    1294 <nv_standardize_screen_name+0x114>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
mov    %rbp,%rdi
mov    $0x2e,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     12a9 <nv_standardize_screen_name+0x129>
movb   $0x0,(%rax)
cmp    $0xfffffffe,%r14d
je     1281 <nv_standardize_screen_name+0x101>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x8(%rax),%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
lea    0xd3a(%rip),%rdx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %rbx,%rcx
mov    %r14d,%r8d
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rbx
jmp    1281 <nv_standardize_screen_name+0x101>

LLVM IR (-O0):
%struct.utsname = type { i8* }
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [6 x i8] c"%s.%d\00"
define i8* @nv_standardize_screen_name(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.utsname
%12 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i8* null, i8** %3
br label %102
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call i8* @strdup(i8* %17)
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %16
store i8* null, i8** %3
br label %102
22: ; preds = %16
%23 = load i8*, i8** %6
%24 = call i8* @strchr(i8* %23, i8 signext 58)
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = icmp ne i8* %25, null
br i1 %26, label %28, label %27
27: ; preds = %22
store i8* null, i8** %3
br label %102
28: ; preds = %22
%29 = load i8*, i8** %6
%30 = load i8*, i8** %8
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %61
32: ; preds = %28
%33 = call i64 @uname(%struct.utsname* %11)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %60
35: ; preds = %32
%36 = load i8*, i8** %6
%37 = call i32 @strlen(i8* %36)
%38 = getelementptr inbounds %struct.utsname, %struct.utsname* %11, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 @strlen(i8* %39)
%41 = add nsw i32 %37, %40
%42 = add nsw i32 %41, 1
store i32 %42, i32* %12
%43 = load i32, i32* %12
%44 = call i8* @malloc(i32 %43)
store i8* %44, i8** %10
%45 = load i8*, i8** %10
%46 = load i32, i32* %12
%47 = getelementptr inbounds %struct.utsname, %struct.utsname* %11, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = load i8*, i8** %6
%50 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %45, i32 %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %48, i8* %49)
%51 = load i8*, i8** %6
%52 = call i32 @free(i8* %51)
%53 = load i8*, i8** %10
store i8* %53, i8** %6
%54 = load i8*, i8** %6
%55 = call i8* @strchr(i8* %54, i8 signext 58)
store i8* %55, i8** %8
%56 = load i8*, i8** %8
%57 = icmp ne i8* %56, null
br i1 %57, label %59, label %58
58: ; preds = %35
store i8* null, i8** %3
br label %102
59: ; preds = %35
br label %60
60: ; preds = %59, %32
br label %61
61: ; preds = %60, %28
%62 = load i32, i32* %5
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %75
64: ; preds = %61
%65 = load i8*, i8** %8
%66 = call i8* @strchr(i8* %65, i8 signext 46)
store i8* %66, i8** %9
%67 = load i8*, i8** %9
%68 = icmp ne i8* %67, null
br i1 %68, label %69, label %73
69: ; preds = %64
%70 = load i8*, i8** %9
%71 = getelementptr inbounds i8, i8* %70, i64 1
%72 = call i32 @atoi(i8* %71)
store i32 %72, i32* %5
br label %74
73: ; preds = %64
store i32 0, i32* %5
br label %74
74: ; preds = %73, %69
br label %75
75: ; preds = %74, %61
%76 = load i8*, i8** %8
%77 = call i8* @strchr(i8* %76, i8 signext 46)
store i8* %77, i8** %9
%78 = load i8*, i8** %9
%79 = icmp ne i8* %78, null
br i1 %79, label %80, label %82
80: ; preds = %75
%81 = load i8*, i8** %9
store i8 0, i8* %81
br label %82
82: ; preds = %80, %75
%83 = load i32, i32* %5
%84 = icmp eq i32 %83, -2
br i1 %84, label %85, label %87
85: ; preds = %82
%86 = load i8*, i8** %6
store i8* %86, i8** %7
br label %100
87: ; preds = %82
%88 = load i8*, i8** %6
%89 = call i32 @strlen(i8* %88)
%90 = add nsw i32 %89, 8
store i32 %90, i32* %12
%91 = load i32, i32* %12
%92 = call i8* @malloc(i32 %91)
store i8* %92, i8** %7
%93 = load i8*, i8** %7
%94 = load i32, i32* %12
%95 = load i8*, i8** %6
%96 = load i32, i32* %5
%97 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %93, i32 %94, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %95, i32 %96)
%98 = load i8*, i8** %6
%99 = call i32 @free(i8* %98)
br label %100
100: ; preds = %87, %85
%101 = load i8*, i8** %7
store i8* %101, i8** %3
br label %102
102: ; preds = %100, %58, %27, %21, %15
%103 = load i8*, i8** %3
ret i8* %103
}

LLVM IR (-O2):
%struct.utsname = type { i8* }
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [6 x i8] c"%s.%d\00"
define i8* @nv_standardize_screen_name(i8* %0, i32 %1) {
%3 = alloca %struct.utsname
%4 = bitcast %struct.utsname* %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %52, label %6
6: ; preds = %2
%7 = tail call i8* @strdup(i8* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %52, label %9
9: ; preds = %6
%10 = tail call i8* @strchr(i8* %7, i8 signext 58)
%11 = icmp eq i8* %10, null
br i1 %11, label %52, label %12
12: ; preds = %9
%13 = icmp eq i8* %7, %10
br i1 %13, label %14, label %29
14: ; preds = %12
%15 = call i64 @uname(%struct.utsname* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %14
%18 = tail call i32 @strlen(i8* %7)
%19 = getelementptr inbounds %struct.utsname, %struct.utsname* %3, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = tail call i32 @strlen(i8* %20)
%22 = add i32 %18, 1
%23 = add i32 %22, %21
%24 = tail call i8* @malloc(i32 %23)
%25 = tail call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %24, i32 %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %7)
%26 = tail call i32 @free(i8* %7)
%27 = tail call i8* @strchr(i8* dereferenceable(1) %24, i8 signext 58)
%28 = icmp eq i8* %27, null
br i1 %28, label %52, label %29
29: ; preds = %14, %17, %12
%30 = phi i8* [ %24, %17 ], [ %7, %14 ], [ %7, %12 ]
%31 = phi i8* [ %27, %17 ], [ %7, %14 ], [ %10, %12 ]
%32 = icmp eq i32 %1, -1
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = tail call i8* @strchr(i8* dereferenceable(1) %31, i8 signext 46)
%35 = icmp eq i8* %34, null
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = getelementptr inbounds i8, i8* %34, i64 1
%38 = tail call i32 @atoi(i8* %37)
br label %39
39: ; preds = %33, %36, %29
%40 = phi i32 [ %38, %36 ], [ %1, %29 ], [ 0, %33 ]
%41 = tail call i8* @strchr(i8* dereferenceable(1) %31, i8 signext 46)
%42 = icmp eq i8* %41, null
br i1 %42, label %44, label %43
43: ; preds = %39
store i8 0, i8* %41
br label %44
44: ; preds = %43, %39
%45 = icmp eq i32 %40, -2
br i1 %45, label %52, label %46
46: ; preds = %44
%47 = tail call i32 @strlen(i8* %30)
%48 = add nsw i32 %47, 8
%49 = tail call i8* @malloc(i32 %48)
%50 = tail call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %49, i32 %48, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %30, i32 %40)
%51 = tail call i32 @free(i8* %30)
br label %52
52: ; preds = %46, %44, %17, %9, %6, %2
%53 = phi i8* [ null, %2 ], [ null, %6 ], [ null, %9 ], [ null, %17 ], [ %49, %46 ], [ %30, %44 ]
ret i8* %53
}