#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcpy(char *, char *);
char *getusername(int userid) {
  int colons;
  int c, i;
  char token[80];
  static char name[80];
  char *retval = NULL;
  FILE *f;
  f = fopen("/etc/passwd", "r");
  if (f) {
    i = 0;
    colons = 0;
    while ((c = getc(f)) != EOF) {
      switch (c) {
      case ':':
        token[i] = '\0';
        colons++;
        if (colons == 3) {
          if (userid == atoi(token)) {
            retval = name;
            goto endloop;
          }
        } else if (colons == 1) {
          strcpy(name, token);
        }
        i = 0;
        break;
      case '\n':
        colons = 0;
        i = 0;
        break;
      default:
        token[i++] = c;
        break;
      }
    }
  endloop:
    fclose(f);
  }
  return retval;
}


x86 assembly (clang, -O0):
<getusername>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x68(%rbp)
lea    0xe93(%rip),%rdi
lea    0xe98(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
je     128a <getusername+0x13a>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x70(%rbp),%rdi
call   1070 <getc@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%eax
je     127c <getusername+0x12c>
mov    -0xc(%rbp),%eax
mov    %eax,-0x74(%rbp)
sub    $0xa,%eax
je     124e <getusername+0xfe>
jmp    11c5 <getusername+0x75>
mov    -0x74(%rbp),%eax
sub    $0x3a,%eax
jne    1261 <getusername+0x111>
jmp    11d6 <getusername+0x86>
movslq -0x10(%rbp),%rax
movb   $0x0,-0x60(%rbp,%rax,1)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    1223 <getusername+0xd3>
mov    -0x4(%rbp),%eax
mov    %eax,-0x78(%rbp)
lea    -0x60(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,%ecx
mov    -0x78(%rbp),%eax
cmp    %ecx,%eax
jne    121e <getusername+0xce>
lea    0x2e4b(%rip),%rax
mov    %rax,-0x68(%rbp)
jmp    1281 <getusername+0x131>
jmp    1242 <getusername+0xf2>
cmpl   $0x1,-0x8(%rbp)
jne    123d <getusername+0xed>
lea    -0x60(%rbp),%rsi
lea    0x2e28(%rip),%rdi
call   1030 <strcpy@plt>
jmp    1242 <getusername+0xf2>
movl   $0x0,-0x10(%rbp)
jmp    1277 <getusername+0x127>
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
jmp    1277 <getusername+0x127>
mov    -0xc(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x10(%rbp)
cltq
mov    %cl,-0x60(%rbp,%rax,1)
jmp    1196 <getusername+0x46>
jmp    1281 <getusername+0x131>
mov    -0x70(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x68(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getusername>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %edi,%r14d
lea    0xe98(%rip),%rdi
lea    0xe9d(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     119e <getusername+0x4e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <getc@plt>
mov    0x2e5d(%rip),%r13
cmp    0x0(%r13),%eax
jne    11b3 <getusername+0x63>
xor    %r12d,%r12d
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11a1 <getusername+0x51>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x2ea6(%rip),%r12
xor    %r15d,%r15d
xor    %ecx,%ecx
jmp    11e6 <getusername+0x96>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x1(%rcx),%ebp
mov    %al,(%rsp,%rcx,1)
mov    %rbx,%rdi
call   1070 <getc@plt>
mov    %ebp,%ecx
cmp    0x0(%r13),%eax
je     1191 <getusername+0x41>
xor    %ebp,%ebp
cmp    $0xa,%eax
je     1210 <getusername+0xc0>
movslq %ecx,%rcx
cmp    $0x3a,%eax
jne    11d0 <getusername+0x80>
movb   $0x0,(%rsp,%rcx,1)
lea    0x1(%r15),%eax
cmp    $0x3,%eax
je     1215 <getusername+0xc5>
test   %r15d,%r15d
je     122d <getusername+0xdd>
mov    %eax,%r15d
jmp    11d6 <getusername+0x86>
nopl   0x0(%rax)
xor    %r15d,%r15d
jmp    11d6 <getusername+0x86>
mov    %rsp,%rdi
call   1060 <atoi@plt>
mov    $0x3,%r15d
cmp    %r14d,%eax
jne    11d6 <getusername+0x86>
jmp    1194 <getusername+0x44>
mov    %r12,%rdi
mov    %rsp,%rsi
call   1030 <strcpy@plt>
mov    $0x1,%r15d
jmp    11d6 <getusername+0x86>

LLVM IR (-O0):
@getusername.name = internal global [80 x i8] zeroinitializer
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
define i8* @getusername(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [80 x i8]
%7 = alloca i8*
%8 = alloca i32*
store i32 %0, i32* %2
store i8* null, i8** %7
%9 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %8
%10 = load i32*, i32** %8
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %56
12: ; preds = %1
store i32 0, i32* %5
store i32 0, i32* %3
br label %13
13: ; preds = %51, %12
%14 = load i32*, i32** %8
%15 = call i32 @getc(i32* %14)
store i32 %15, i32* %4
%16 = load i32, i32* @EOF
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %52
18: ; preds = %13
%19 = load i32, i32* %4
switch i32 %19, label %44 [
i32 58, label %20
i32 10, label %43
]
20: ; preds = %18
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %22
store i8 0, i8* %23
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 1
store i32 %25, i32* %3
%26 = load i32, i32* %3
%27 = icmp eq i32 %26, 3
br i1 %27, label %28, label %35
28: ; preds = %20
%29 = load i32, i32* %2
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%31 = call i32 @atoi(i8* %30)
%32 = icmp eq i32 %29, %31
br i1 %32, label %33, label %34
33: ; preds = %28
store i8* getelementptr inbounds ([80 x i8], [80 x i8]* @getusername.name, i64 0, i64 0), i8** %7
br label %53
34: ; preds = %28
br label %42
35: ; preds = %20
%36 = load i32, i32* %3
%37 = icmp eq i32 %36, 1
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%40 = call i32 @strcpy(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @getusername.name, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %38, %35
br label %42
42: ; preds = %41, %34
store i32 0, i32* %5
br label %51
43: ; preds = %18
store i32 0, i32* %3
store i32 0, i32* %5
br label %51
44: ; preds = %18
%45 = load i32, i32* %4
%46 = trunc i32 %45 to i8
%47 = load i32, i32* %5
%48 = add nsw i32 %47, 1
store i32 %48, i32* %5
%49 = sext i32 %47 to i64
%50 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %49
store i8 %46, i8* %50
br label %51
51: ; preds = %44, %43, %42
br label %13
52: ; preds = %13
br label %53
53: ; preds = %52, %33
%54 = load i32*, i32** %8
%55 = call i32 @fclose(i32* %54)
br label %56
56: ; preds = %53, %1
%57 = load i8*, i8** %7
ret i8* %57
}

LLVM IR (-O2):
@getusername.name = internal global [80 x i8] zeroinitializer
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
define i8* @getusername(i32 %0) {
%2 = alloca [80 x i8]
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %40, label %6
6: ; preds = %1
%7 = call i32 @getc(i32* %4)
%8 = load i32, i32* @EOF
%9 = icmp eq i32 %7, %8
br i1 %9, label %37, label %10
10: ; preds = %6, %31
%11 = phi i32 [ %34, %31 ], [ %7, %6 ]
%12 = phi i32 [ %33, %31 ], [ 0, %6 ]
%13 = phi i32 [ %32, %31 ], [ 0, %6 ]
switch i32 %11, label %26 [
i32 58, label %14
i32 10, label %31
]
14: ; preds = %10
%15 = sext i32 %13 to i64
%16 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %15
store i8 0, i8* %16
%17 = add nsw i32 %12, 1
%18 = icmp eq i32 %17, 3
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = call i32 @atoi(i8* %3)
%21 = icmp eq i32 %20, %0
br i1 %21, label %37, label %31
22: ; preds = %14
%23 = icmp eq i32 %12, 0
br i1 %23, label %24, label %31
24: ; preds = %22
%25 = call i32 @strcpy(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @getusername.name, i64 0, i64 0), i8* %3)
br label %31
26: ; preds = %10
%27 = trunc i32 %11 to i8
%28 = add nsw i32 %13, 1
%29 = sext i32 %13 to i64
%30 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %29
store i8 %27, i8* %30
br label %31
31: ; preds = %10, %19, %24, %22, %26
%32 = phi i32 [ %28, %26 ], [ 0, %22 ], [ 0, %24 ], [ 0, %19 ], [ 0, %10 ]
%33 = phi i32 [ %12, %26 ], [ %17, %22 ], [ 1, %24 ], [ 3, %19 ], [ 0, %10 ]
%34 = call i32 @getc(i32* %4)
%35 = load i32, i32* @EOF
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %10
37: ; preds = %31, %19, %6
%38 = phi i8* [ null, %6 ], [ getelementptr inbounds ([80 x i8], [80 x i8]* @getusername.name, i64 0, i64 0), %19 ], [ null, %31 ]
%39 = call i32 @fclose(i32* %4)
br label %40
40: ; preds = %37, %1
%41 = phi i8* [ %38, %37 ], [ null, %1 ]
ret i8* %41
}