#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 passwd {
  char *pw_gecos;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ escape_display_name(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct passwd *getpwuid(scalar_t__);
scalar_t__ getuid();
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char *argv[]) {
  struct passwd *pwd;
  char buf[BUFSIZ], *p;
  if (argc > 2) {
    fprintf(stderr, "usage: %s [name]\n", argv[0]);
    return 1;
  }
  if (argc < 2) {
    pwd = getpwuid(getuid());
    if (!pwd) {
      fprintf(stderr,
              "Unable to retrieve user info for "
              "userid %ld\n",
              (long)getuid());
      exit(1);
    }
    strncpy(buf, pwd->pw_gecos, sizeof(buf));
    buf[sizeof(buf) - 1] = '\0';
  } else
    strncpy(buf, argv[1], sizeof(buf));
  if ((p = strchr(buf, ',')))
    *p = '\0';
  escape_display_name(buf, sizeof(buf));
  puts(buf);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    1204 <main+0x84>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret
cmpl   $0x2,-0x8(%rbp)
jge    1288 <main+0x108>
mov    $0x0,%al
call   1060 <getuid@plt>
mov    %rax,%rdi
call   1050 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    125d <main+0xdd>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   1060 <getuid@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xdc6(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rcx
mov    -0x38(%rbp),%rax
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    129f <main+0x11f>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x38(%rbp),%rdi
mov    $0x2c,%esi
call   1070 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     12c2 <main+0x142>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
call   1090 <escape_display_name@plt>
mov    -0x38(%rbp),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    0x2e4a(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x3,%edi
jl     11cf <main+0x4f>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe46(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
cmp    $0x2,%edi
jne    11e5 <main+0x65>
mov    0x8(%rsi),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1030 <strncpy@plt>
jmp    123a <main+0xba>
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %rax,%rdi
call   1050 <getpwuid@plt>
test   %rax,%rax
jne    1226 <main+0xa6>
mov    0x2de0(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1060 <getuid@plt>
lea    0xe02(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    (%rax),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r14,%rbx,1)
mov    %rbx,%rdi
mov    $0x2c,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     124f <main+0xcf>
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    %r14d,%esi
call   1090 <escape_display_name@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@BUFSIZ = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"usage: %s [name]\0A\00"
@.str.1 = [45 x i8] c"Unable to retrieve user info for userid %ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.passwd*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %7
%13 = alloca i8, i64 %11
store i64 %11, i64* %8
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 2
br i1 %15, label %16, label %24
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 1, i32* %3
%22 = load i8*, i8** %7
%23 = load i32, i32* %3
ret i32 %23
24: ; preds = %2
%25 = load i32, i32* %4
%26 = icmp slt i32 %25, 2
br i1 %26, label %27, label %45
27: ; preds = %24
%28 = call i64 (...) @getuid()
%29 = call %struct.passwd* @getpwuid(i64 %28)
store %struct.passwd* %29, %struct.passwd** %6
%30 = load %struct.passwd*, %struct.passwd** %6
%31 = icmp ne %struct.passwd* %30, null
br i1 %31, label %37, label %32
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = call i64 (...) @getuid()
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i64 %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %27
%38 = load %struct.passwd*, %struct.passwd** %6
%39 = getelementptr inbounds %struct.passwd, %struct.passwd* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = trunc i64 %11 to i32
%42 = call i32 @strncpy(i8* %13, i8* %40, i32 %41)
%43 = sub i64 %11, 1
%44 = getelementptr inbounds i8, i8* %13, i64 %43
store i8 0, i8* %44
br label %51
45: ; preds = %24
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = trunc i64 %11 to i32
%50 = call i32 @strncpy(i8* %13, i8* %48, i32 %49)
br label %51
51: ; preds = %45, %37
%52 = call i8* @strchr(i8* %13, i8 signext 44)
store i8* %52, i8** %9
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = load i8*, i8** %9
store i8 0, i8* %55
br label %56
56: ; preds = %54, %51
%57 = trunc i64 %11 to i32
%58 = call i32 @escape_display_name(i8* %13, i32 %57)
%59 = call i32 @puts(i8* %13)
%60 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@BUFSIZ = global i32 0
@stderr = global i32 0
@.str = [18 x i8] c"usage: %s [name]\0A\00"
@.str.1 = [45 x i8] c"Unable to retrieve user info for userid %ld\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = icmp sgt i32 %0, 2
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %9)
ret i32 1
11: ; preds = %2
%12 = icmp eq i32 %0, 2
br i1 %12, label %28, label %13
13: ; preds = %11
%14 = tail call i64 (...) @getuid()
%15 = tail call %struct.passwd* @getpwuid(i64 %14)
%16 = icmp eq %struct.passwd* %15, null
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = tail call i64 (...) @getuid()
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i64 %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %15, i64 0, i32 0
%24 = load i8*, i8** %23
%25 = call i32 @strncpy(i8* %5, i8* %24, i32 %3)
%26 = add nsw i64 %4, -1
%27 = getelementptr inbounds i8, i8* %5, i64 %26
store i8 0, i8* %27
br label %32
28: ; preds = %11
%29 = getelementptr inbounds i8*, i8** %1, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @strncpy(i8* %5, i8* %30, i32 %3)
br label %32
32: ; preds = %28, %22
%33 = call i8* @strchr(i8* %5, i8 signext 44)
%34 = icmp eq i8* %33, null
br i1 %34, label %36, label %35
35: ; preds = %32
store i8 0, i8* %33
br label %36
36: ; preds = %35, %32
%37 = call i32 @escape_display_name(i8* %5, i32 %3)
%38 = call i32 @puts(i8* %5)
%39 = call i32 @exit(i32 0)
unreachable
}